|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | +package com.facebook.react.uiapp |
| 8 | + |
| 9 | +import android.app.Application |
| 10 | +import com.facebook.fbreact.specs.SampleLegacyModule |
| 11 | +import com.facebook.fbreact.specs.SampleTurboModule |
| 12 | +import com.facebook.react.JSEngineResolutionAlgorithm |
| 13 | +import com.facebook.react.ReactApplication |
| 14 | +import com.facebook.react.ReactNativeHost |
| 15 | +import com.facebook.react.ReactPackage |
| 16 | +import com.facebook.react.TurboReactPackage |
| 17 | +import com.facebook.react.bridge.NativeModule |
| 18 | +import com.facebook.react.bridge.ReactApplicationContext |
| 19 | +import com.facebook.react.common.annotations.UnstableReactNativeAPI |
| 20 | +import com.facebook.react.common.assets.ReactFontManager |
| 21 | +import com.facebook.react.common.mapbuffer.ReadableMapBuffer |
| 22 | +import com.facebook.react.config.ReactFeatureFlags |
| 23 | +import com.facebook.react.defaults.DefaultComponentsRegistry.Companion.register |
| 24 | +import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load |
| 25 | +import com.facebook.react.defaults.DefaultReactNativeHost |
| 26 | +import com.facebook.react.fabric.ComponentFactory |
| 27 | +import com.facebook.react.flipper.ReactNativeFlipper.initializeFlipper |
| 28 | +import com.facebook.react.interfaces.ReactHost |
| 29 | +import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler |
| 30 | +import com.facebook.react.module.model.ReactModuleInfo |
| 31 | +import com.facebook.react.module.model.ReactModuleInfoProvider |
| 32 | +import com.facebook.react.runtime.ReactHostImpl |
| 33 | +import com.facebook.react.shell.MainReactPackage |
| 34 | +import com.facebook.react.uiapp.component.MyLegacyViewManager |
| 35 | +import com.facebook.react.uiapp.component.MyNativeViewManager |
| 36 | +import com.facebook.react.uimanager.ViewManager |
| 37 | +import com.facebook.soloader.SoLoader |
| 38 | + |
| 39 | +class RNTesterApplication : Application(), ReactApplication { |
| 40 | + override val reactNativeHost: ReactNativeHost by lazy { |
| 41 | + if (ReactFeatureFlags.enableBridgelessArchitecture) { |
| 42 | + throw RuntimeException("Should not use ReactNativeHost when Bridgeless enabled") |
| 43 | + } |
| 44 | + object : DefaultReactNativeHost(this) { |
| 45 | + public override fun getJSMainModuleName(): String = "js/RNTesterApp.android" |
| 46 | + |
| 47 | + public override fun getBundleAssetName(): String = "RNTesterApp.android.bundle" |
| 48 | + |
| 49 | + override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG |
| 50 | + |
| 51 | + public override fun getPackages(): List<ReactPackage> { |
| 52 | + return listOf( |
| 53 | + MainReactPackage(), |
| 54 | + object : TurboReactPackage() { |
| 55 | + override fun getModule( |
| 56 | + name: String, |
| 57 | + reactContext: ReactApplicationContext |
| 58 | + ): NativeModule? { |
| 59 | + if (!ReactFeatureFlags.useTurboModules) { |
| 60 | + return null |
| 61 | + } |
| 62 | + if (SampleTurboModule.NAME == name) { |
| 63 | + return SampleTurboModule(reactContext) |
| 64 | + } |
| 65 | + if (SampleLegacyModule.NAME == name) { |
| 66 | + return SampleLegacyModule(reactContext) |
| 67 | + } |
| 68 | + return null |
| 69 | + } |
| 70 | + |
| 71 | + // Note: Specialized annotation processor for @ReactModule isn't configured in OSS |
| 72 | + // yet. For now, hardcode this information, though it's not necessary for most |
| 73 | + // modules. |
| 74 | + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = |
| 75 | + ReactModuleInfoProvider { |
| 76 | + if (ReactFeatureFlags.useTurboModules) { |
| 77 | + mapOf( |
| 78 | + SampleTurboModule.NAME to |
| 79 | + ReactModuleInfo( |
| 80 | + SampleTurboModule.NAME, |
| 81 | + "SampleTurboModule", |
| 82 | + false, // canOverrideExistingModule |
| 83 | + false, // needsEagerInit |
| 84 | + false, // isCxxModule |
| 85 | + true // isTurboModule |
| 86 | + ), |
| 87 | + SampleLegacyModule.NAME to |
| 88 | + ReactModuleInfo( |
| 89 | + SampleLegacyModule.NAME, |
| 90 | + "SampleLegacyModule", |
| 91 | + false, // canOverrideExistingModule |
| 92 | + false, // needsEagerInit |
| 93 | + false, // isCxxModule |
| 94 | + false // isTurboModule |
| 95 | + )) |
| 96 | + } else { |
| 97 | + emptyMap() |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + object : ReactPackage { |
| 102 | + override fun createNativeModules( |
| 103 | + reactContext: ReactApplicationContext |
| 104 | + ): List<NativeModule> { |
| 105 | + return emptyList() |
| 106 | + } |
| 107 | + |
| 108 | + override fun createViewManagers( |
| 109 | + reactContext: ReactApplicationContext |
| 110 | + ): List<ViewManager<*, *>> = |
| 111 | + listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext)) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED |
| 116 | + override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + override fun onCreate() { |
| 121 | + ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik) |
| 122 | + super.onCreate() |
| 123 | + SoLoader.init(this, /* native exopackage */ false) |
| 124 | + if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { |
| 125 | + load() |
| 126 | + } |
| 127 | + if (ReactFeatureFlags.enableBridgelessArchitecture) { |
| 128 | + // TODO: initialize Flipper for Bridgeless |
| 129 | + } else { |
| 130 | + initializeFlipper(this, reactNativeHost.reactInstanceManager) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @UnstableReactNativeAPI |
| 135 | + override val reactHost: ReactHost by lazy { |
| 136 | + // Create an instance of ReactHost to manager the instance of ReactInstance, |
| 137 | + // which is similar to how we use ReactNativeHost to manager instance of ReactInstanceManager |
| 138 | + val reactHostDelegate = RNTesterReactHostDelegate(applicationContext) |
| 139 | + val reactJsExceptionHandler = RNTesterReactJsExceptionHandler() |
| 140 | + val componentFactory = ComponentFactory() |
| 141 | + register(componentFactory) |
| 142 | + ReactHostImpl( |
| 143 | + this.applicationContext, |
| 144 | + reactHostDelegate, |
| 145 | + componentFactory, |
| 146 | + true, |
| 147 | + reactJsExceptionHandler, |
| 148 | + true) |
| 149 | + .apply { |
| 150 | + jsEngineResolutionAlgorithm = |
| 151 | + if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) { |
| 152 | + JSEngineResolutionAlgorithm.HERMES |
| 153 | + } else { |
| 154 | + JSEngineResolutionAlgorithm.JSC |
| 155 | + } |
| 156 | + reactHostDelegate.reactHost = this |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @UnstableReactNativeAPI |
| 161 | + class RNTesterReactJsExceptionHandler : ReactJsExceptionHandler { |
| 162 | + override fun reportJsException(errorMap: ReadableMapBuffer?) {} |
| 163 | + } |
| 164 | +} |
0 commit comments