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;
}
}
package com.your.package.name.here
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.modules.core.DeviceEventManagerModule
import nz.co.electricbolt.appfiguratelibrary.Appfigurate
import nz.co.electricbolt.appfiguratelibrary.Configuration
class AppfigurateModule internal constructor(context: ReactApplicationContext?) :
ReactContextBaseJavaModule(context) {
private var cachedConfiguration: Map<String, Any> =
Configuration.sharedConfiguration()._dictionaryFromConfiguration()
private var listenerCount = 0
init {
Appfigurate.addConfigurationUpdatedListener { action: String? ->
cachedConfiguration = Configuration.sharedConfiguration()._dictionaryFromConfiguration()
if (listenerCount > 0) {
val params = Arguments.createMap()
params.putString("APLConfigurationUpdatedAction", action)
reactApplicationContext.getJSModule(
DeviceEventManagerModule.RCTDeviceEventEmitter::class.java
).emit("APLConfigurationUpdated", params)
}
}
}
override fun getName(): String {
return "AppfigurateModule"
}
@ReactMethod
fun nativeValue(propertyName: String, result: Promise) {
result.resolve(cachedConfiguration[propertyName])
}
@ReactMethod
fun description(result: Promise) {
result.resolve(Configuration.sharedConfiguration().description())
}
@ReactMethod
fun modifications(result: Promise) {
result.resolve(Configuration.sharedConfiguration().modifications())
}
@ReactMethod
fun version(result: Promise) {
result.resolve(Appfigurate.version())
}
@ReactMethod
fun setLogging(logging: Boolean, result: Promise) {
Appfigurate.setLogging(logging)
result.resolve(null)
}
@ReactMethod
fun saveConfiguration(result: Promise) {
Appfigurate.saveConfiguration()
result.resolve(null)
}
@ReactMethod
fun restoreConfiguration(result: Promise) {
Appfigurate.restoreConfiguration()
result.resolve(null)
}
@ReactMethod
fun addListener(eventName: String?) {
listenerCount += 1
}
@ReactMethod
fun removeListeners(count: Int) {
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;
}
}
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
class AppfiguratePackage : ReactPackage {
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
override fun createNativeModules(
reactContext: ReactApplicationContext
): List<NativeModule> {
val modules: MutableList<NativeModule> = ArrayList()
modules.add(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;
}
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
add(AppfiguratePackage())
}
The Android native module is now integrated. Now jump to JavaScript integration.
Last updated