1

I've written native module for React Native, everything was done according to official documentation, but something went wrong, and Native Modules returns only empty object.

importing Native Modules in Component Home.js

All Module code was writen according to official docs

import {
  NativeModules
} from 'react-native';

const { OptionsPackage } = NativeModules;

console.log(NativeModules) // {}

MainApplication.java file

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
            packages.add(new OptionsPackage());
          return packages;
        }

OptionsPackage.java file

package package.name;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OptionsPackage implements ReactPackage {

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(
            ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new OptionsModule(reactContext));

        return modules;
    }

}

OptionsModule.java

package com.downloadmanager;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.io.InputStream;

... 
public class OptionsModule extends ReactContextBaseJavaModule {

    ActivityManager activityManager;
    int currentSDK;
    ReactApplicationContext context;

    OptionsModule(ReactApplicationContext context) {
        super(context);
        this.activityManager = ((ActivityManager) context.getSystemService(ReactApplicationContext.ACTIVITY_SERVICE));
        context = context;
        currentSDK = android.os.Build.VERSION.SDK_INT;
    }

    @Override
    public String getName() {
        return "123";
    }

    private Map packageInfoToMap(PackageInfo pi) {
        PackageManager pm = this.context.getPackageManager();

        long longVersionCode;
        Map<String, Object> AppInfo = new HashMap<String, Object>();

        if(Build.VERSION.SDK_INT >= 28) {
            longVersionCode = pi.getLongVersionCode();
        } else
        {
            longVersionCode = pi.versionCode; }

        AppInfo.put("packageName", pi.packageName);
        AppInfo.put("versionName", pi.versionName);
        AppInfo.put("getLongVersionCode", longVersionCode);
        AppInfo.put("lastUpdateTime", (pi.lastUpdateTime));
        AppInfo.put("firstInstallTime", pi.firstInstallTime);

        String title = pi.applicationInfo.loadLabel(pm).toString();
        String description =  pi.applicationInfo.loadDescription(pm).toString();
        Drawable icon = pi.applicationInfo.loadIcon(pm);


        
        AppInfo.put("title", title);
        AppInfo.put("description", description);
        AppInfo.put("icon", icon);

        //get folder

        String apkDir = pi.applicationInfo.publicSourceDir;
        File file = new File(apkDir);
        double size = file.length();

        AppInfo.put("filePath", apkDir);
        AppInfo.put("size", size);

        return AppInfo;
    }

    @ReactMethod
    public List listOfApps(String type) {
        List<PackageInfo> packages = this.context.getPackageManager().getInstalledPackages(0);
        List<Map<String, Object>> listOfApps = new ArrayList();

        for (PackageInfo isPackage : packages) {
            boolean isSystem = (isPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM;


            if(type == "systemOnly" && isSystem) {
                listOfApps.add(this.packageInfoToMap(isPackage));
            } else if (type == "nonSystem" && !isSystem) {
                listOfApps.add(this.packageInfoToMap(isPackage));
            } else {
                listOfApps.add(this.packageInfoToMap(isPackage));
            }
        }

        return listOfApps;

    }
}


3
  • Hey I am also facing a similar kind of issue did you solve this? If then please post the answer Commented Mar 26, 2021 at 11:27
  • Having a similar issue? did you find the answer? Commented Feb 18, 2022 at 23:58
  • Same issue - did anyone make any progress? stackoverflow.com/questions/71613550/… Commented Mar 25, 2022 at 11:27

1 Answer 1

0

Your getName() override method in OptionsModule is returning "123"

@Override
public String getName() {
    return "123";
}

This is how react native gets the name of your module that should show up on react native side.

Try changing it to "OptionsModule" and then in your Home.js do

const {OptionsModule} = NativeModules

Also try calling your @ReactMethod in js to see if it works, don't rely on console logging NativeModules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.