From 92aba40a3dd0ea0bc78bff79ae8ab0de654eaea9 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 16 Nov 2023 16:17:59 -0800 Subject: [PATCH] Make default classes load their own so's Summary: Make the DefaultTurboModuleManagerDelegate and the DefaultComponentRegistry load their own so's when they're created. **Motivation:** We are going to use these two classes in Meta apps. And Meta apps will not invoke DefaultNewArchitectureEntryPoint.load. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D51036133 --- .../defaults/DefaultComponentsRegistry.kt | 4 +++ .../DefaultNewArchitectureEntryPoint.kt | 10 +++---- .../react/defaults/DefaultSoLoader.kt | 27 +++++++++++++++++++ .../DefaultTurboModuleManagerDelegate.kt | 4 +++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultSoLoader.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultComponentsRegistry.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultComponentsRegistry.kt index 131804140ee901..2cf0b6908e4936 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultComponentsRegistry.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultComponentsRegistry.kt @@ -27,6 +27,10 @@ private constructor(componentFactory: ComponentFactory) { @DoNotStrip private external fun initHybrid(componentFactory: ComponentFactory): HybridData + init { + DefaultSoLoader.maybeLoadSoLibrary() + } + companion object { @JvmStatic @DoNotStrip diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultNewArchitectureEntryPoint.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultNewArchitectureEntryPoint.kt index fa6ff875dda211..ca6cf331a103d4 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultNewArchitectureEntryPoint.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultNewArchitectureEntryPoint.kt @@ -8,7 +8,6 @@ package com.facebook.react.defaults import com.facebook.react.config.ReactFeatureFlags -import com.facebook.soloader.SoLoader /** * A utility class that serves as an entry point for users setup the New Architecture. @@ -27,8 +26,7 @@ object DefaultNewArchitectureEntryPoint { fun load( turboModulesEnabled: Boolean = true, fabricEnabled: Boolean = true, - bridgelessEnabled: Boolean = false, - dynamicLibraryName: String = "appmodules", + bridgelessEnabled: Boolean = false ) { ReactFeatureFlags.useTurboModules = turboModulesEnabled ReactFeatureFlags.enableFabricRenderer = fabricEnabled @@ -42,8 +40,7 @@ object DefaultNewArchitectureEntryPoint { this.privateConcurrentReactEnabled = fabricEnabled this.privateBridgelessEnabled = bridgelessEnabled - SoLoader.loadLibrary("react_newarchdefaults") - SoLoader.loadLibrary(dynamicLibraryName) + DefaultSoLoader.maybeLoadSoLibrary() } @Deprecated( @@ -56,9 +53,8 @@ object DefaultNewArchitectureEntryPoint { fabricEnabled: Boolean = true, bridgelessEnabled: Boolean = false, @Suppress("UNUSED_PARAMETER") concurrentReactEnabled: Boolean = true, - dynamicLibraryName: String = "appmodules", ) { - load(turboModulesEnabled, fabricEnabled, bridgelessEnabled, dynamicLibraryName) + load(turboModulesEnabled, fabricEnabled, bridgelessEnabled) } private var privateFabricEnabled: Boolean = false diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultSoLoader.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultSoLoader.kt new file mode 100644 index 00000000000000..99c9ebdf4f0b44 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultSoLoader.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.defaults + +import com.facebook.soloader.SoLoader +import com.facebook.soloader.SoLoaderDSONotFoundError + +internal class DefaultSoLoader { + companion object { + @Synchronized + @JvmStatic + fun maybeLoadSoLibrary() { + SoLoader.loadLibrary("react_newarchdefaults") + try { + SoLoader.loadLibrary("appmodules") + } catch (e: SoLoaderDSONotFoundError) { + // ignore: DefaultTurboModuleManagerDelegate is still used in apps that don't have + // appmodules.so + } + } + } +} 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 ac1fd1a4c7d90c..9ea51d49f4f2a3 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 @@ -30,4 +30,8 @@ private constructor(context: ReactApplicationContext, packages: List) = DefaultTurboModuleManagerDelegate(context, packages) } + + override fun maybeLoadOtherSoLibraries() { + DefaultSoLoader.maybeLoadSoLibrary() + } }