Further simplify the New App Template by don't requiring the dynamic library name (#34671)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34671

I'm simplifying the template further by:
- Do not expose a configurable dynamic library name. Let's use `appmodules`
and still allow the users to configure it, if needed.
- Move all the initialization logic inside the `JNI_OnLoad` method
- Cleanup the `DefaultReactNativeHost` to don't require a dynamic library
name but just a boolean.

Changelog:
[Android] [Changed] - Further simplify the New App Template by don't requiring the dynamic library name

Reviewed By: cipolleschi

Differential Revision: D39462948

fbshipit-source-id: 737733fc263162a0baf3b7a451e48b8616679d3b
This commit is contained in:
Nicola Corti
2022-09-13 06:42:37 -07:00
committed by Facebook GitHub Bot
parent 6bdcb49966
commit 59ae0487ce
12 changed files with 53 additions and 144 deletions
@@ -16,7 +16,7 @@ import com.facebook.react.fabric.ComponentFactory
* implementation of its native methods.
*
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framwork
* hosted inside the React Native framework
*/
@DoNotStrip
class DefaultComponentsRegistry
@@ -7,8 +7,6 @@
package com.facebook.react.defaults
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.soloader.SoLoader
/**
@@ -18,22 +16,14 @@ import com.facebook.soloader.SoLoader
* This class needs to be invoked as `DefaultNativeEntryPoint.load("...")` by passing the name of
* the dynamic library to load.
*
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framework
* By default it loads a library called `appmodules`. `appmodules` is a convention used to refer to
* the application dynamic library. If changed here should be updated also inside the template.
*/
@DoNotStrip
class DefaultNativeEntryPoint @DoNotStrip private constructor() {
@DoNotStrip private val hybridData: HybridData = initHybrid()
@DoNotStrip private external fun initHybrid(): HybridData
companion object {
@JvmStatic
fun load(dynamicLibraryName: String) {
SoLoader.loadLibrary("react_newarchdefaults")
SoLoader.loadLibrary(dynamicLibraryName)
DefaultNativeEntryPoint()
}
object DefaultNativeEntryPoint {
@JvmStatic
@JvmOverloads
fun load(dynamicLibraryName: String = "appmodules") {
SoLoader.loadLibrary("react_newarchdefaults")
SoLoader.loadLibrary(dynamicLibraryName)
}
}
@@ -25,29 +25,27 @@ abstract class DefaultReactNativeHost protected constructor(application: Applica
override fun getReactPackageTurboModuleManagerDelegateBuilder():
ReactPackageTurboModuleManagerDelegate.Builder? =
dynamicLibraryName?.let {
// If the user provided a dynamic library name, we assume they want to load
// the default ReactPackageTurboModuleManagerDelegate
if (isNewArchEnabled) {
DefaultTurboModuleManagerDelegate.Builder()
} else {
null
}
override fun getJSIModulePackage(): JSIModulePackage? =
dynamicLibraryName?.let {
// If the user provided a dynamic library name, we assume they want to load
// the default JSIModulePackage
if (isNewArchEnabled) {
DefaultJSIModulePackage(this)
} else {
null
}
/**
* Returns the name of the dynamic library used by app on the New Architecture. This is generally
* "<applicationname>_appmodules" or just "appmodules"
* Returns whether the user wants to use the New Architecture or not.
*
* If null, we will assume you're not using the New Architecture and will not attempt to load any
* dynamic library at runtime.
* If true, we will load the default JSI Module Package and TurboModuleManagerDelegate needed to
* enable the New Architecture
*
* If set, we'll take care of create a TurboModuleManagerDelegate that will load the library you
* specified.
* If false, the app will not attempt to load the New Architecture modules.
*/
protected open val dynamicLibraryName: String?
get() = null
protected open val isNewArchEnabled: Boolean
get() = false
}
@@ -18,7 +18,7 @@ import com.facebook.react.bridge.ReactApplicationContext
* [ReactPackageTurboModuleManagerDelegate] for new apps in Open Source.
*
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framwork
* hosted inside the React Native framework
*/
class DefaultTurboModuleManagerDelegate
private constructor(context: ReactApplicationContext, packages: List<ReactPackage>) :