From f6d2e811cbae8a77db398170704fa41a906fb482 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Tue, 7 Dec 2021 14:47:37 +0000 Subject: [PATCH] [TurboModule] Register a new TurboModulePackage --- .../rnnewarchitectureapp/MainApplication.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/java/com/rnnewarchitectureapp/MainApplication.java b/android/app/src/main/java/com/rnnewarchitectureapp/MainApplication.java index 717df1fe..225bbfe3 100644 --- a/android/app/src/main/java/com/rnnewarchitectureapp/MainApplication.java +++ b/android/app/src/main/java/com/rnnewarchitectureapp/MainApplication.java @@ -4,6 +4,7 @@ import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; @@ -11,10 +12,18 @@ import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.ReactPackageTurboModuleManagerDelegate; +import com.facebook.react.TurboReactPackage; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.module.model.ReactModuleInfo; +import com.facebook.react.module.model.ReactModuleInfoProvider; import com.facebook.soloader.SoLoader; import com.rnnewarchitectureapp.modules.MainApplicationTurboModuleManagerDelegate; +import com.rnnewarchitectureapp.modules.NativeAnswerSolver; import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class MainApplication extends Application implements ReactApplication { @@ -27,10 +36,40 @@ public boolean getUseDeveloperSupport() { @Override protected List getPackages() { - @SuppressWarnings("UnnecessaryLocalVariable") List 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 TurboReactPackage() { + @Nullable + @Override + public NativeModule getModule(String name, ReactApplicationContext reactContext) { + if (name.equals(NativeAnswerSolver.NAME)) { + return new NativeAnswerSolver(reactContext); + } else { + return null; + } + } + + @Override + public ReactModuleInfoProvider getReactModuleInfoProvider() { + return () -> { + final Map moduleInfos = new HashMap<>(); + moduleInfos.put( + NativeAnswerSolver.NAME, + new ReactModuleInfo( + NativeAnswerSolver.NAME, + "NativeAnswerSolver", + false, // canOverrideExistingModule + false, // needsEagerInit + true, // hasConstants + false, // isCxxModule + true // isTurboModule + ) + ); + return moduleInfos; + }; + } + }); return packages; }