From a4f12a2eee3838e02f0c5c318b5463e124b5668e Mon Sep 17 00:00:00 2001 From: Oleksii Date: Wed, 20 Sep 2023 15:01:49 +0300 Subject: [PATCH 1/5] rewrite RNTesterApplication to kotlin --- .../annotations/UnstableReactNativeAPI.kt | 2 +- .../react/uiapp/RNTesterApplication.java | 214 ------------------ .../react/uiapp/RNTesterApplication.kt | 175 ++++++++++++++ 3 files changed, 176 insertions(+), 215 deletions(-) delete mode 100644 packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java create mode 100644 packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/annotations/UnstableReactNativeAPI.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/annotations/UnstableReactNativeAPI.kt index 3996580c313e4d..5ae0b1779a3b24 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/annotations/UnstableReactNativeAPI.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/annotations/UnstableReactNativeAPI.kt @@ -8,7 +8,7 @@ package com.facebook.react.common.annotations @Retention(AnnotationRetention.RUNTIME) -@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) +@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) @RequiresOptIn( level = RequiresOptIn.Level.ERROR, message = "This API is experimental and is likely to change or to be removed in the future") diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java deleted file mode 100644 index 8bf2493c5a8945..00000000000000 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * 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.uiapp; - -import android.app.Application; -import androidx.annotation.NonNull; -import com.facebook.fbreact.specs.SampleLegacyModule; -import com.facebook.fbreact.specs.SampleTurboModule; -import com.facebook.react.JSEngineResolutionAlgorithm; -import com.facebook.react.ReactApplication; -import com.facebook.react.ReactNativeHost; -import com.facebook.react.ReactPackage; -import com.facebook.react.TurboReactPackage; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.common.annotations.UnstableReactNativeAPI; -import com.facebook.react.common.assets.ReactFontManager; -import com.facebook.react.common.mapbuffer.ReadableMapBuffer; -import com.facebook.react.config.ReactFeatureFlags; -import com.facebook.react.defaults.DefaultComponentsRegistry; -import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint; -import com.facebook.react.defaults.DefaultReactNativeHost; -import com.facebook.react.fabric.ComponentFactory; -import com.facebook.react.flipper.ReactNativeFlipper; -import com.facebook.react.interfaces.ReactHost; -import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler; -import com.facebook.react.module.model.ReactModuleInfo; -import com.facebook.react.module.model.ReactModuleInfoProvider; -import com.facebook.react.runtime.ReactHostImpl; -import com.facebook.react.shell.MainReactPackage; -import com.facebook.react.uiapp.component.MyLegacyViewManager; -import com.facebook.react.uiapp.component.MyNativeViewManager; -import com.facebook.react.uimanager.ViewManager; -import com.facebook.soloader.SoLoader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class RNTesterApplication extends Application implements ReactApplication { - - private ReactHostImpl mReactHost; - - private final ReactNativeHost mReactNativeHost = - new DefaultReactNativeHost(this) { - @Override - public String getJSMainModuleName() { - return "js/RNTesterApp.android"; - } - - @Override - public String getBundleAssetName() { - return "RNTesterApp.android.bundle"; - } - - @Override - public boolean getUseDeveloperSupport() { - return BuildConfig.DEBUG; - } - - @Override - public List getPackages() { - return Arrays.asList( - new MainReactPackage(), - new TurboReactPackage() { - public NativeModule getModule( - final String name, final ReactApplicationContext reactContext) { - if (!ReactFeatureFlags.useTurboModules) { - return null; - } - - if (SampleTurboModule.NAME.equals(name)) { - return new SampleTurboModule(reactContext); - } - - if (SampleLegacyModule.NAME.equals(name)) { - return new SampleLegacyModule(reactContext); - } - - return null; - } - - // Note: Specialized annotation processor for @ReactModule isn't configured in OSS - // yet. For now, hardcode this information, though it's not necessary for most - // modules. - public ReactModuleInfoProvider getReactModuleInfoProvider() { - return new ReactModuleInfoProvider() { - public Map getReactModuleInfos() { - final Map moduleInfos = new HashMap<>(); - if (ReactFeatureFlags.useTurboModules) { - moduleInfos.put( - SampleTurboModule.NAME, - new ReactModuleInfo( - SampleTurboModule.NAME, - "SampleTurboModule", - false, // canOverrideExistingModule - false, // needsEagerInit - false, // isCxxModule - true // isTurboModule - )); - - moduleInfos.put( - SampleLegacyModule.NAME, - new ReactModuleInfo( - SampleLegacyModule.NAME, - "SampleLegacyModule", - false, // canOverrideExistingModule - false, // needsEagerInit - false, // isCxxModule - false // isTurboModule - )); - } - return moduleInfos; - } - }; - } - }, - new ReactPackage() { - @NonNull - @Override - public List createNativeModules( - @NonNull ReactApplicationContext reactContext) { - return Collections.emptyList(); - } - - @NonNull - @Override - public List createViewManagers( - @NonNull ReactApplicationContext reactContext) { - List viewManagers = new ArrayList<>(); - viewManagers.add(new MyNativeViewManager()); - viewManagers.add(new MyLegacyViewManager(reactContext)); - return viewManagers; - } - }); - } - - @Override - protected boolean isNewArchEnabled() { - return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; - } - - @Override - protected Boolean isHermesEnabled() { - return BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR; - } - }; - - @Override - public void onCreate() { - ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik); - super.onCreate(); - SoLoader.init(this, /* native exopackage */ false); - if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { - DefaultNewArchitectureEntryPoint.load(); - } - if (ReactFeatureFlags.enableBridgelessArchitecture) { - // TODO: initialize Flipper for Bridgeless - } else { - ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); - } - } - - @Override - public ReactNativeHost getReactNativeHost() { - if (ReactFeatureFlags.enableBridgelessArchitecture) { - throw new RuntimeException("Should not use ReactNativeHost when Bridgeless enabled"); - } - return mReactNativeHost; - } - - @Override - @UnstableReactNativeAPI - public ReactHost getReactHost() { - if (mReactHost == null) { - // Create an instance of ReactHost to manager the instance of ReactInstance, - // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager - RNTesterReactHostDelegate reactHostDelegate = - new RNTesterReactHostDelegate(getApplicationContext()); - RNTesterReactJsExceptionHandler reactJsExceptionHandler = - new RNTesterReactJsExceptionHandler(); - - ComponentFactory componentFactory = new ComponentFactory(); - DefaultComponentsRegistry.register(componentFactory); - mReactHost = - new ReactHostImpl( - this.getApplicationContext(), - reactHostDelegate, - componentFactory, - true, - reactJsExceptionHandler, - true); - if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { - mReactHost.setJSEngineResolutionAlgorithm(JSEngineResolutionAlgorithm.HERMES); - } else { - mReactHost.setJSEngineResolutionAlgorithm(JSEngineResolutionAlgorithm.JSC); - } - reactHostDelegate.setReactHost(mReactHost); - } - return mReactHost; - } - - @UnstableReactNativeAPI - public static class RNTesterReactJsExceptionHandler implements ReactJsExceptionHandler { - public void reportJsException(ReadableMapBuffer errorMap) {} - } -}; diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt new file mode 100644 index 00000000000000..958d89a8575804 --- /dev/null +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -0,0 +1,175 @@ +/* + * 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.uiapp + +import android.app.Application +import com.facebook.fbreact.specs.SampleLegacyModule +import com.facebook.fbreact.specs.SampleTurboModule +import com.facebook.react.JSEngineResolutionAlgorithm +import com.facebook.react.ReactApplication +import com.facebook.react.ReactNativeHost +import com.facebook.react.ReactPackage +import com.facebook.react.TurboReactPackage +import com.facebook.react.bridge.NativeModule +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.common.annotations.UnstableReactNativeAPI +import com.facebook.react.common.assets.ReactFontManager +import com.facebook.react.common.mapbuffer.ReadableMapBuffer +import com.facebook.react.config.ReactFeatureFlags +import com.facebook.react.defaults.DefaultComponentsRegistry.Companion.register +import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load +import com.facebook.react.defaults.DefaultReactNativeHost +import com.facebook.react.fabric.ComponentFactory +import com.facebook.react.flipper.ReactNativeFlipper.initializeFlipper +import com.facebook.react.interfaces.ReactHost +import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler +import com.facebook.react.module.model.ReactModuleInfo +import com.facebook.react.module.model.ReactModuleInfoProvider +import com.facebook.react.runtime.ReactHostImpl +import com.facebook.react.shell.MainReactPackage +import com.facebook.react.uiapp.component.MyLegacyViewManager +import com.facebook.react.uiapp.component.MyNativeViewManager +import com.facebook.react.uimanager.ViewManager +import com.facebook.soloader.SoLoader + +class RNTesterApplication : Application(), ReactApplication { + private var mReactHost: ReactHostImpl? = null + private val mReactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) { + public override fun getJSMainModuleName(): String { + return "js/RNTesterApp.android" + } + + public override fun getBundleAssetName(): String? { + return "RNTesterApp.android.bundle" + } + + override fun getUseDeveloperSupport(): Boolean { + return BuildConfig.DEBUG + } + + public override fun getPackages(): List { + return listOf( + MainReactPackage(), + object : TurboReactPackage() { + override fun getModule( + name: String, reactContext: ReactApplicationContext): NativeModule? { + if (!ReactFeatureFlags.useTurboModules) { + return null + } + if (SampleTurboModule.NAME == name) { + return SampleTurboModule(reactContext) + } + if (SampleLegacyModule.NAME == name) { + return SampleLegacyModule(reactContext) + } + return null; + } + + // Note: Specialized annotation processor for @ReactModule isn't configured in OSS + // yet. For now, hardcode this information, though it's not necessary for most + // modules. + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider { + if (ReactFeatureFlags.useTurboModules) { + mapOf( + SampleTurboModule.NAME to + ReactModuleInfo( + SampleTurboModule.NAME, + "SampleTurboModule", + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + true // isTurboModule + ), + SampleLegacyModule.NAME to + ReactModuleInfo( + SampleLegacyModule.NAME, + "SampleLegacyModule", + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + false // isTurboModule + ) + ) + } else { + emptyMap() + } + } + + }, + object : ReactPackage { + override fun createNativeModules( + reactContext: ReactApplicationContext): List { + return emptyList() + } + + override fun createViewManagers( + reactContext: ReactApplicationContext + ): List> = + listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext)); + }) + } + + override val isNewArchEnabled: Boolean + protected get() = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED + override val isHermesEnabled: Boolean + protected get() = BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR + } + + override fun onCreate() { + ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik) + super.onCreate() + SoLoader.init(this, /* native exopackage */false) + if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { + load() + } + if (ReactFeatureFlags.enableBridgelessArchitecture) { + // TODO: initialize Flipper for Bridgeless + } else { + initializeFlipper(this, reactNativeHost.reactInstanceManager) + } + } + + override val reactNativeHost: ReactNativeHost + get() { + if (ReactFeatureFlags.enableBridgelessArchitecture) { + throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") + } + return mReactNativeHost + } + + @UnstableReactNativeAPI + override val reactHostInterface: ReactHost + get() { + if (mReactHost == null) { + // Create an instance of ReactHost to manager the instance of ReactInstance, + // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager + val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) + val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() + val componentFactory = ComponentFactory() + register(componentFactory) + mReactHost = ReactHostImpl( + this.applicationContext, + reactHostDelegate, + componentFactory, + true, + reactJsExceptionHandler, + true) + if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { + mReactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES + } else { + mReactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC + } + reactHostDelegate.reactHost = mReactHost + } + return mReactHost!! + } + + @UnstableReactNativeAPI + class RNTesterReactJsExceptionHandler : ReactJsExceptionHandler { + override fun reportJsException(errorMap: ReadableMapBuffer?) {} + } +} From d4d111a3bb96d4b7da0000b7c043be2aaaf5c89f Mon Sep 17 00:00:00 2001 From: Oleksii Date: Wed, 20 Sep 2023 20:54:49 +0300 Subject: [PATCH 2/5] refactor, change namings and getter functions --- .../react/uiapp/RNTesterApplication.kt | 161 +++++++++--------- 1 file changed, 76 insertions(+), 85 deletions(-) diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt index 958d89a8575804..cd9dd5343a0f68 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -37,87 +37,86 @@ import com.facebook.react.uimanager.ViewManager import com.facebook.soloader.SoLoader class RNTesterApplication : Application(), ReactApplication { - private var mReactHost: ReactHostImpl? = null - private val mReactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) { - public override fun getJSMainModuleName(): String { - return "js/RNTesterApp.android" + private var reactHost: ReactHostImpl? = null + override val reactNativeHost: ReactNativeHost + get() { + if (ReactFeatureFlags.enableBridgelessArchitecture) { + throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") } + return object : DefaultReactNativeHost(this) { + public override fun getJSMainModuleName(): String = "js/RNTesterApp.android" - public override fun getBundleAssetName(): String? { - return "RNTesterApp.android.bundle" - } + public override fun getBundleAssetName(): String = "RNTesterApp.android.bundle" - override fun getUseDeveloperSupport(): Boolean { - return BuildConfig.DEBUG - } + override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG - public override fun getPackages(): List { + public override fun getPackages(): List { return listOf( - MainReactPackage(), - object : TurboReactPackage() { - override fun getModule( - name: String, reactContext: ReactApplicationContext): NativeModule? { - if (!ReactFeatureFlags.useTurboModules) { - return null - } - if (SampleTurboModule.NAME == name) { - return SampleTurboModule(reactContext) - } - if (SampleLegacyModule.NAME == name) { - return SampleLegacyModule(reactContext) - } - return null; - } + MainReactPackage(), + object : TurboReactPackage() { + override fun getModule( + name: String, reactContext: ReactApplicationContext): NativeModule? { + if (!ReactFeatureFlags.useTurboModules) { + return null + } + if (SampleTurboModule.NAME == name) { + return SampleTurboModule(reactContext) + } + if (SampleLegacyModule.NAME == name) { + return SampleLegacyModule(reactContext) + } + return null; + } + + // Note: Specialized annotation processor for @ReactModule isn't configured in OSS + // yet. For now, hardcode this information, though it's not necessary for most + // modules. + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider { + if (ReactFeatureFlags.useTurboModules) { + mapOf( + SampleTurboModule.NAME to + ReactModuleInfo( + SampleTurboModule.NAME, + "SampleTurboModule", + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + true // isTurboModule + ), + SampleLegacyModule.NAME to + ReactModuleInfo( + SampleLegacyModule.NAME, + "SampleLegacyModule", + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + false // isTurboModule + ) + ) + } else { + emptyMap() + } + } - // Note: Specialized annotation processor for @ReactModule isn't configured in OSS - // yet. For now, hardcode this information, though it's not necessary for most - // modules. - override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider { - if (ReactFeatureFlags.useTurboModules) { - mapOf( - SampleTurboModule.NAME to - ReactModuleInfo( - SampleTurboModule.NAME, - "SampleTurboModule", - false, // canOverrideExistingModule - false, // needsEagerInit - false, // isCxxModule - true // isTurboModule - ), - SampleLegacyModule.NAME to - ReactModuleInfo( - SampleLegacyModule.NAME, - "SampleLegacyModule", - false, // canOverrideExistingModule - false, // needsEagerInit - false, // isCxxModule - false // isTurboModule - ) - ) - } else { - emptyMap() - } - } + }, + object : ReactPackage { + override fun createNativeModules( + reactContext: ReactApplicationContext): List { + return emptyList() + } - }, - object : ReactPackage { - override fun createNativeModules( - reactContext: ReactApplicationContext): List { - return emptyList() - } + override fun createViewManagers( + reactContext: ReactApplicationContext + ): List> = + listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext)); + }) + } - override fun createViewManagers( - reactContext: ReactApplicationContext - ): List> = - listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext)); - }) + override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED + override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR } + } - override val isNewArchEnabled: Boolean - protected get() = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED - override val isHermesEnabled: Boolean - protected get() = BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR - } override fun onCreate() { ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik) @@ -133,25 +132,17 @@ class RNTesterApplication : Application(), ReactApplication { } } - override val reactNativeHost: ReactNativeHost - get() { - if (ReactFeatureFlags.enableBridgelessArchitecture) { - throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") - } - return mReactNativeHost - } - @UnstableReactNativeAPI override val reactHostInterface: ReactHost get() { - if (mReactHost == null) { + if (reactHost == null) { // Create an instance of ReactHost to manager the instance of ReactInstance, // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() val componentFactory = ComponentFactory() register(componentFactory) - mReactHost = ReactHostImpl( + reactHost = ReactHostImpl( this.applicationContext, reactHostDelegate, componentFactory, @@ -159,13 +150,13 @@ class RNTesterApplication : Application(), ReactApplication { reactJsExceptionHandler, true) if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { - mReactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES + reactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES } else { - mReactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC + reactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC } - reactHostDelegate.reactHost = mReactHost + reactHostDelegate.reactHost = reactHost } - return mReactHost!! + return reactHost!! } @UnstableReactNativeAPI From b44cdf29557087ce114ba5d066c9335ea3db7915 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Tue, 26 Sep 2023 13:09:45 +0300 Subject: [PATCH 3/5] refactor reactHost to be lateinit, reactNativeHost to be lazy --- .../facebook/react/uiapp/RNTesterApplication.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt index cd9dd5343a0f68..c38585dbf9a39e 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -37,13 +37,12 @@ import com.facebook.react.uimanager.ViewManager import com.facebook.soloader.SoLoader class RNTesterApplication : Application(), ReactApplication { - private var reactHost: ReactHostImpl? = null - override val reactNativeHost: ReactNativeHost - get() { + private lateinit var reactHost: ReactHostImpl + override val reactNativeHost: ReactNativeHost by lazy { if (ReactFeatureFlags.enableBridgelessArchitecture) { throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") } - return object : DefaultReactNativeHost(this) { + object : DefaultReactNativeHost(this) { public override fun getJSMainModuleName(): String = "js/RNTesterApp.android" public override fun getBundleAssetName(): String = "RNTesterApp.android.bundle" @@ -135,7 +134,7 @@ class RNTesterApplication : Application(), ReactApplication { @UnstableReactNativeAPI override val reactHostInterface: ReactHost get() { - if (reactHost == null) { + if (!::reactHost.isInitialized) { // Create an instance of ReactHost to manager the instance of ReactInstance, // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) @@ -150,13 +149,13 @@ class RNTesterApplication : Application(), ReactApplication { reactJsExceptionHandler, true) if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { - reactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES + reactHost.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES } else { - reactHost!!.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC + reactHost.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC } reactHostDelegate.reactHost = reactHost } - return reactHost!! + return reactHost } @UnstableReactNativeAPI From fd600be5d9529c47f426b4e5ddae17c1b158ef8e Mon Sep 17 00:00:00 2001 From: Oleksii Date: Tue, 26 Sep 2023 21:57:44 +0300 Subject: [PATCH 4/5] refactor reactHost --- .../react/uiapp/RNTesterApplication.kt | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt index c38585dbf9a39e..4c4938ce02a550 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -37,7 +37,6 @@ import com.facebook.react.uimanager.ViewManager import com.facebook.soloader.SoLoader class RNTesterApplication : Application(), ReactApplication { - private lateinit var reactHost: ReactHostImpl override val reactNativeHost: ReactNativeHost by lazy { if (ReactFeatureFlags.enableBridgelessArchitecture) { throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") @@ -132,31 +131,28 @@ class RNTesterApplication : Application(), ReactApplication { } @UnstableReactNativeAPI - override val reactHostInterface: ReactHost - get() { - if (!::reactHost.isInitialized) { - // Create an instance of ReactHost to manager the instance of ReactInstance, - // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager - val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) - val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() - val componentFactory = ComponentFactory() - register(componentFactory) - reactHost = ReactHostImpl( - this.applicationContext, - reactHostDelegate, - componentFactory, - true, - reactJsExceptionHandler, - true) - if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { - reactHost.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES - } else { - reactHost.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC - } - reactHostDelegate.reactHost = reactHost - } - return reactHost + override val reactHost: ReactHost by lazy { + // Create an instance of ReactHost to manager the instance of ReactInstance, + // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager + val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) + val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() + val componentFactory = ComponentFactory() + register(componentFactory) + val reactHostImpl = ReactHostImpl( + this.applicationContext, + reactHostDelegate, + componentFactory, + true, + reactJsExceptionHandler, + true) + if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { + reactHostImpl.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES + } else { + reactHostImpl.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC } + reactHostDelegate.reactHost = reactHostImpl + reactHostImpl + } @UnstableReactNativeAPI class RNTesterReactJsExceptionHandler : ReactJsExceptionHandler { From 2fb6a88dc3aec4c6a6e21b157777d5ca99c93c24 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Wed, 27 Sep 2023 01:24:38 +0300 Subject: [PATCH 5/5] refactor reactHost body --- .../react/uiapp/RNTesterApplication.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt index 4c4938ce02a550..bd3f14d1e6fc2c 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -138,20 +138,20 @@ class RNTesterApplication : Application(), ReactApplication { val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() val componentFactory = ComponentFactory() register(componentFactory) - val reactHostImpl = ReactHostImpl( - this.applicationContext, - reactHostDelegate, - componentFactory, - true, - reactJsExceptionHandler, - true) - if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { - reactHostImpl.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.HERMES + ReactHostImpl( + this.applicationContext, + reactHostDelegate, + componentFactory, + true, + reactJsExceptionHandler, + true).apply { + jsEngineResolutionAlgorithm = if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { + JSEngineResolutionAlgorithm.HERMES } else { - reactHostImpl.jsEngineResolutionAlgorithm = JSEngineResolutionAlgorithm.JSC + JSEngineResolutionAlgorithm.JSC } - reactHostDelegate.reactHost = reactHostImpl - reactHostImpl + reactHostDelegate.reactHost = this + } } @UnstableReactNativeAPI