diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.java index 30ef9e019b3783..949276ec20b195 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.java @@ -10,6 +10,7 @@ import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; +import com.facebook.jni.HybridData; import com.facebook.react.bridge.CxxModuleWrapper; import com.facebook.react.bridge.ModuleSpec; import com.facebook.react.bridge.NativeModule; @@ -53,13 +54,22 @@ interface ModuleProvider { private List mPackages; private ReactApplicationContext mReactContext; - protected ReactPackageTurboModuleManagerDelegate() { + protected ReactPackageTurboModuleManagerDelegate( + ReactApplicationContext reactApplicationContext, List packages) { super(); + initialize(reactApplicationContext, packages); } protected ReactPackageTurboModuleManagerDelegate( + ReactApplicationContext reactApplicationContext, + List packages, + HybridData hybridData) { + super(hybridData); + initialize(reactApplicationContext, packages); + } + + private void initialize( ReactApplicationContext reactApplicationContext, List packages) { - super(); if (mIsLazy) { mPackages = packages; mReactContext = reactApplicationContext; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultTurboModuleManagerDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultTurboModuleManagerDelegate.kt index 10a7dc7c5d2f75..ad38bb31f10e61 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultTurboModuleManagerDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultTurboModuleManagerDelegate.kt @@ -28,12 +28,13 @@ private constructor( context: ReactApplicationContext, packages: List, private val eagerlyInitializedModules: List, - private val cxxReactPackage: CxxReactPackage?, -) : ReactPackageTurboModuleManagerDelegate(context, packages) { + cxxReactPackage: CxxReactPackage?, +) : ReactPackageTurboModuleManagerDelegate(context, packages, initHybrid(cxxReactPackage)) { - @DoNotStrip override fun initHybrid() = initHybrid(cxxReactPackage) - - external fun initHybrid(cxxReactPackage: CxxReactPackage?): HybridData? + override fun initHybrid(): HybridData? { + throw UnsupportedOperationException( + "DefaultTurboModuleManagerDelegate.initHybrid() must never be called!") + } override fun getEagerInitModuleNames(): List { if (unstable_isLazyTurboModuleDelegate()) { @@ -62,8 +63,11 @@ private constructor( DefaultTurboModuleManagerDelegate(context, packages, eagerInitModuleNames, cxxReactPackage) } - @Synchronized - override fun maybeLoadOtherSoLibraries() { - DefaultSoLoader.maybeLoadSoLibrary() + companion object { + init { + DefaultSoLoader.maybeLoadSoLibrary() + } + + @DoNotStrip @JvmStatic external fun initHybrid(cxxReactPackage: CxxReactPackage?): HybridData? } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.java index 2740fa7208ae7d..7cb82679a3aefa 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.java @@ -31,6 +31,11 @@ protected TurboModuleManagerDelegate() { mHybridData = initHybrid(); } + protected TurboModuleManagerDelegate(HybridData hybridData) { + maybeLoadOtherSoLibraries(); + mHybridData = hybridData; + } + /** * Create and return a TurboModule Java object with name `moduleName`. If `moduleName` isn't a * TurboModule, return null. @@ -77,5 +82,7 @@ public boolean unstable_enableSyncVoidMethods() { return false; } + // TODO(T171231381): Consider removing this method: could we just use the static initializer + // of derived classes instead? protected synchronized void maybeLoadOtherSoLibraries() {} } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp index 86944f2e6c4a20..fc480af328b6ee 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp @@ -27,7 +27,7 @@ std::function( jni::local_ref DefaultTurboModuleManagerDelegate::initHybrid( - jni::alias_ref, + jni::alias_ref jClass, jni::alias_ref cxxReactPackage) { return makeCxxInstance(cxxReactPackage); } @@ -43,9 +43,12 @@ std::shared_ptr DefaultTurboModuleManagerDelegate::getTurboModule( const std::string& name, const std::shared_ptr& jsInvoker) { if (cxxReactPackage_) { - auto module = cxxReactPackage_->cthis()->getModule(name, jsInvoker); - if (module) { - return module; + auto cppPart = cxxReactPackage_->cthis(); + if (cppPart) { + auto module = cppPart->getModule(name, jsInvoker); + if (module) { + return module; + } } } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.h b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.h index e175a574bc394b..687a23b0c29062 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.h @@ -24,7 +24,7 @@ class DefaultTurboModuleManagerDelegate : public jni::HybridClass< "Lcom/facebook/react/defaults/DefaultTurboModuleManagerDelegate;"; static jni::local_ref initHybrid( - jni::alias_ref, + jni::alias_ref, jni::alias_ref); static void registerNatives(); @@ -53,7 +53,7 @@ class DefaultTurboModuleManagerDelegate : public jni::HybridClass< jni::global_ref cxxReactPackage_; DefaultTurboModuleManagerDelegate( - jni::alias_ref); + jni::alias_ref cxxReactPackage); }; } // namespace facebook::react