Android native module integration

Create module

In your Android Studio project create a new Java or Kotlin class named AppfiguateModule .

package com.your.package.name.here;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.Map;

import nz.co.electricbolt.appfiguratelibrary.Appfigurate;
import nz.co.electricbolt.appfiguratelibrary.Configuration;

public class AppfigurateModule extends ReactContextBaseJavaModule {

    private Map<String, Object> cachedConfiguration = Configuration.sharedConfiguration()._dictionaryFromConfiguration();

    private int listenerCount = 0;

    AppfigurateModule(ReactApplicationContext context) {
        super(context);
        Appfigurate.addConfigurationUpdatedListener(action -> {
            cachedConfiguration = Configuration.sharedConfiguration()._dictionaryFromConfiguration();
            if (listenerCount > 0) {
                WritableMap params = Arguments.createMap();
                params.putString("APLConfigurationUpdatedAction", action);
                getReactApplicationContext().getJSModule(DeviceEventManagerModule.
                        RCTDeviceEventEmitter.class).emit("APLConfigurationUpdated", params);
            }
        });
    }

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

    @ReactMethod
    public void nativeValue(String propertyName, Promise result) {
        result.resolve(cachedConfiguration.get(propertyName));
    }

    @ReactMethod
    public void description(Promise result) {
        result.resolve(Configuration.sharedConfiguration().description());
    }

    @ReactMethod
    public void modifications(Promise result) {
        result.resolve(Configuration.sharedConfiguration().modifications());
    }

    @ReactMethod
    public void version(Promise result) {
        result.resolve(Appfigurate.version());
    }

    @ReactMethod
    public void setLogging(boolean logging, Promise result) {
        Appfigurate.setLogging(logging);
        result.resolve(null);
    }

    @ReactMethod
    public void saveConfiguration(Promise result) {
        Appfigurate.saveConfiguration();
        result.resolve(null);
    }

    @ReactMethod
    public void restoreConfiguration(Promise result) {
        Appfigurate.restoreConfiguration();
        result.resolve(null);
    }

    @ReactMethod
    public void addListener(String eventName) {
        listenerCount += 1;
    }

    @ReactMethod
    public void removeListeners(Integer count) {
        listenerCount -= count;
    }

}

Create package

In your Android Studio project create a new Java or Kotlin class named AppfiguatePackage .

package com.your.package.name.here;

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 AppfiguratePackage 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 AppfigurateModule(reactContext));
        return modules;
    }

}

Register package

Locate ReactNativeHost’s getPackages() method and add AppfiguratePackage to the packages list getPackages() returns:

@Override
protected List<ReactPackage> getPackages() {
    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 AppfiguratePackage());
    return packages;
}

The Android native module is now integrated. Now jump to JavaScript integration.

Last updated