Migrate ReactNativeHost from Java to Kotlin (#56848)

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

Mechanical migration of the deprecated `ReactNativeHost` class from Java to Kotlin. All methods retain their exact JVM signatures to preserve API compatibility.

Key migration decisions:
- Class remains `abstract` (implicitly open)
- All non-final, non-abstract methods marked `open` to match Java virtual-by-default
- `getApplication()` remains `final` (no `open`)
- Methods kept as explicit `fun` (not Kotlin properties) to preserve exact JVM getter names like `getJSMainModuleName()`, `getJSBundleFile()`, `getUIManagerProvider()`
- `Nullable` return types converted to Kotlin nullable types (`?`)
- `synchronized` methods use `Synchronized` annotation
- `Nullsafe` annotation removed (redundant in Kotlin)
- Static initializer converted to companion object `init` block

Changelog:
[Android][Changed] - Migrate `ReactNativeHost` from Java to Kotlin (no behavioral changes)

Reviewed By: javache

Differential Revision: D105013283

fbshipit-source-id: 3561cad5e7cdff52d21da29f521f8a563f5dc077
This commit is contained in:
Nicola Corti authored and meta-codesync[bot] committed 2026-05-21 11:09:24 -07:00
1 parent 11f47848d9
commit 7eb11331b6
4 files changed
+221 -259

No files matched your search

@@ -249,7 +249,7 @@ public open class ReactDelegate {
((ReactNativeNewArchitectureFeatureFlags.enableBridgelessArchitecture() &&
reactHost?.devSupportManager != null) ||
(reactNativeHost?.hasInstance() == true &&
reactNativeHost?.useDeveloperSupport == true))
reactNativeHost?.getUseDeveloperSupport() == true))
) {
event.startTracking()
return true
@@ -270,7 +270,8 @@ public open class ReactDelegate {
}
} else {
if (
reactNativeHost?.hasInstance() == true && reactNativeHost?.useDeveloperSupport == true
reactNativeHost?.hasInstance() == true &&
reactNativeHost?.getUseDeveloperSupport() == true
) {
reactNativeHost?.reactInstanceManager?.showDevOptionsDialog()
return true
@@ -1,251 +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;
import android.app.Application;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.JSExceptionHandler;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.bridge.UIManagerProvider;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.common.SurfaceDelegate;
import com.facebook.react.common.SurfaceDelegateFactory;
import com.facebook.react.common.annotations.internal.LegacyArchitecture;
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel;
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger;
import com.facebook.react.devsupport.DevSupportManagerFactory;
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.internal.ChoreographerProvider;
import java.util.List;
/**
* Simple class that holds an instance of {@link ReactInstanceManager}. This can be used in your
* {@link Application class} (see {@link ReactApplication}), or as a static field.
*
* @deprecated This class will be replaced by com.facebook.react.ReactHost in the New Architecture.
*/
@Deprecated(
since = "This class is part of Legacy Architecture and will be removed in a future release")
@LegacyArchitecture(logLevel = LegacyArchitectureLogLevel.ERROR)
@Nullsafe(Nullsafe.Mode.LOCAL)
public abstract class ReactNativeHost {
static {
LegacyArchitectureLogger.assertLegacyArchitecture(
"ReactNativeHost", LegacyArchitectureLogLevel.ERROR);
}
private final Application mApplication;
private @Nullable ReactInstanceManager mReactInstanceManager;
protected ReactNativeHost(Application application) {
mApplication = application;
}
/**
* Get the current {@link ReactInstanceManager} instance, or create one.
*
* <p>NOTE: Care must be taken when storing this reference outside of the ReactNativeHost
* lifecycle. The ReactInstanceManager will be invalidated during {@link #clear()}, and may not be
* used again afterwards.
*/
public synchronized ReactInstanceManager getReactInstanceManager() {
if (mReactInstanceManager == null) {
ReactMarker.logMarker(ReactMarkerConstants.INIT_REACT_RUNTIME_START);
ReactMarker.logMarker(ReactMarkerConstants.GET_REACT_INSTANCE_MANAGER_START);
mReactInstanceManager = createReactInstanceManager();
ReactMarker.logMarker(ReactMarkerConstants.GET_REACT_INSTANCE_MANAGER_END);
}
return mReactInstanceManager;
}
/**
* Get whether this holder contains a {@link ReactInstanceManager} instance, or not. I.e. if
* {@link #getReactInstanceManager()} has been called at least once since this object was created
* or {@link #clear()} was called.
*/
public synchronized boolean hasInstance() {
return mReactInstanceManager != null;
}
/**
* Destroy the current instance and invalidate the internal ReactInstanceManager, reclaiming its
* resources and preventing it from being reused.
*/
public synchronized void clear() {
if (mReactInstanceManager != null) {
mReactInstanceManager.invalidate();
mReactInstanceManager = null;
}
}
protected ReactInstanceManager createReactInstanceManager() {
ReactMarker.logMarker(ReactMarkerConstants.BUILD_REACT_INSTANCE_MANAGER_START);
ReactInstanceManagerBuilder builder = getBaseReactInstanceManagerBuilder();
ReactMarker.logMarker(ReactMarkerConstants.BUILD_REACT_INSTANCE_MANAGER_END);
return builder.build();
}
protected ReactInstanceManagerBuilder getBaseReactInstanceManagerBuilder() {
ReactInstanceManagerBuilder builder =
ReactInstanceManager.builder()
.setApplication(mApplication)
.setJSMainModulePath(getJSMainModuleName())
.setUseDeveloperSupport(getUseDeveloperSupport())
.setDevSupportManagerFactory(getDevSupportManagerFactory())
.setDevLoadingViewManager(getDevLoadingViewManager())
.setRequireActivity(getShouldRequireActivity())
.setSurfaceDelegateFactory(getSurfaceDelegateFactory())
.setJSExceptionHandler(getJSExceptionHandler())
.setLazyViewManagersEnabled(getLazyViewManagersEnabled())
.setRedBoxHandler(getRedBoxHandler())
.setJavaScriptExecutorFactory(getJavaScriptExecutorFactory())
.setUIManagerProvider(getUIManagerProvider())
.setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
.setReactPackageTurboModuleManagerDelegateBuilder(
getReactPackageTurboModuleManagerDelegateBuilder())
.setChoreographerProvider(getChoreographerProvider())
.setPausedInDebuggerOverlayManager(getPausedInDebuggerOverlayManager());
for (ReactPackage reactPackage : getPackages()) {
builder.addPackage(reactPackage);
}
String jsBundleFile = getJSBundleFile();
if (jsBundleFile != null) {
builder.setJSBundleFile(jsBundleFile);
} else {
builder.setBundleAssetName(Assertions.assertNotNull(getBundleAssetName()));
}
return builder;
}
/** Get the {@link RedBoxHandler} to send RedBox-related callbacks to. */
protected @Nullable RedBoxHandler getRedBoxHandler() {
return null;
}
protected @Nullable JSExceptionHandler getJSExceptionHandler() {
return null;
}
/** Get the {@link JavaScriptExecutorFactory}. Override this to use a custom Executor. */
protected @Nullable JavaScriptExecutorFactory getJavaScriptExecutorFactory() {
return null;
}
protected @Nullable ReactPackageTurboModuleManagerDelegate.Builder
getReactPackageTurboModuleManagerDelegateBuilder() {
return null;
}
protected final Application getApplication() {
return mApplication;
}
protected @Nullable UIManagerProvider getUIManagerProvider() {
return reactApplicationContext -> null;
}
/** Returns whether or not to treat it as normal if Activity is null. */
public boolean getShouldRequireActivity() {
return true;
}
/**
* Returns whether view managers should be created lazily. See {@link
* ViewManagerOnDemandReactPackage} for details.
*
* @experimental
*/
public boolean getLazyViewManagersEnabled() {
return false;
}
/**
* Return the {@link SurfaceDelegateFactory} used by NativeModules to get access to a {@link
* SurfaceDelegate} to interact with a surface. By default in the mobile platform the {@link
* SurfaceDelegate} it returns is null, and the NativeModule needs to implement its own {@link
* SurfaceDelegate} to decide how it would interact with its own container surface.
*/
public SurfaceDelegateFactory getSurfaceDelegateFactory() {
return new SurfaceDelegateFactory() {
@Override
public @Nullable SurfaceDelegate createSurfaceDelegate(String moduleName) {
return null;
}
};
}
/**
* Get the {@link DevLoadingViewManager}. Override this to use a custom dev loading view manager
*/
protected @Nullable DevLoadingViewManager getDevLoadingViewManager() {
return null;
}
protected @Nullable PausedInDebuggerOverlayManager getPausedInDebuggerOverlayManager() {
return null;
}
/**
* Returns the name of the main module. Determines the URL used to fetch the JS bundle from Metro.
* It is only used when dev support is enabled. This is the first file to be executed once the
* {@link ReactInstanceManager} is created. e.g. "index.android"
*/
protected String getJSMainModuleName() {
return "index.android";
}
/**
* Returns a custom path of the bundle file. This is used in cases the bundle should be loaded
* from a custom path. By default it is loaded from Android assets, from a path specified by
* {@link getBundleAssetName}. e.g. "file://sdcard/myapp_cache/index.android.bundle"
*/
protected @Nullable String getJSBundleFile() {
return null;
}
/**
* Returns the name of the bundle in assets. If this is null, and no file path is specified for
* the bundle, the app will only work with {@code getUseDeveloperSupport} enabled and will always
* try to load the JS bundle from Metro. e.g. "index.android.bundle"
*/
protected @Nullable String getBundleAssetName() {
return "index.android.bundle";
}
/** Returns whether dev mode should be enabled. This enables e.g. the dev menu. */
public abstract boolean getUseDeveloperSupport();
/** Get the {@link DevSupportManagerFactory}. Override this to use a custom dev support manager */
protected @Nullable DevSupportManagerFactory getDevSupportManagerFactory() {
return null;
}
/**
* Returns a list of {@link ReactPackage} used by the app. You'll most likely want to return at
* least the {@code MainReactPackage}. If your app uses additional views or modules besides the
* default ones, you'll want to include more packages here.
*/
protected abstract List<ReactPackage> getPackages();
/**
* Returns a custom implementation of ChoreographerProvider to be used this host. If null - React
* will use default direct android.view.Choreographer-based provider.
*/
protected @Nullable ChoreographerProvider getChoreographerProvider() {
return null;
}
}
@@ -0,0 +1,212 @@
/*
* 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.
*/
@file:Suppress("DEPRECATION")
package com.facebook.react
import android.annotation.SuppressLint
import android.app.Application
import com.facebook.react.bridge.JSExceptionHandler
import com.facebook.react.bridge.JavaScriptExecutorFactory
import com.facebook.react.bridge.ReactMarker
import com.facebook.react.bridge.ReactMarkerConstants
import com.facebook.react.bridge.UIManagerProvider
import com.facebook.react.common.LifecycleState
import com.facebook.react.common.SurfaceDelegateFactory
import com.facebook.react.common.annotations.internal.LegacyArchitecture
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger
import com.facebook.react.devsupport.DevSupportManagerFactory
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.internal.ChoreographerProvider
/**
* Simple class that holds an instance of [ReactInstanceManager]. This can be used in your
* [Application] class (see [ReactApplication]), or as a static field.
*
* @deprecated This class will be replaced by com.facebook.react.ReactHost in the New Architecture.
*/
@Deprecated("This class is part of Legacy Architecture and will be removed in a future release")
@LegacyArchitecture(logLevel = LegacyArchitectureLogLevel.ERROR)
@SuppressLint("DeprecatedClass", "DeprecatedInterface")
public abstract class ReactNativeHost protected constructor(private val mApplication: Application) {
private var mReactInstanceManager: ReactInstanceManager? = null
/**
* Get the current [ReactInstanceManager] instance, or create one.
*
* NOTE: Care must be taken when storing this reference outside of the ReactNativeHost lifecycle.
* The ReactInstanceManager will be invalidated during [clear], and may not be used again
* afterwards.
*/
public open val reactInstanceManager: ReactInstanceManager
@Synchronized
get() {
if (mReactInstanceManager == null) {
ReactMarker.logMarker(ReactMarkerConstants.INIT_REACT_RUNTIME_START)
ReactMarker.logMarker(ReactMarkerConstants.GET_REACT_INSTANCE_MANAGER_START)
mReactInstanceManager = createReactInstanceManager()
ReactMarker.logMarker(ReactMarkerConstants.GET_REACT_INSTANCE_MANAGER_END)
}
return mReactInstanceManager!!
}
/**
* Get whether this holder contains a [ReactInstanceManager] instance, or not. I.e. if
* [getReactInstanceManager] has been called at least once since this object was created or
* [clear] was called.
*/
@Synchronized public open fun hasInstance(): Boolean = mReactInstanceManager != null
/**
* Destroy the current instance and invalidate the internal ReactInstanceManager, reclaiming its
* resources and preventing it from being reused.
*/
@Synchronized
public open fun clear() {
mReactInstanceManager?.invalidate()
mReactInstanceManager = null
}
protected open fun createReactInstanceManager(): ReactInstanceManager {
ReactMarker.logMarker(ReactMarkerConstants.BUILD_REACT_INSTANCE_MANAGER_START)
val builder = getBaseReactInstanceManagerBuilder()
ReactMarker.logMarker(ReactMarkerConstants.BUILD_REACT_INSTANCE_MANAGER_END)
return builder.build()
}
protected open fun getBaseReactInstanceManagerBuilder(): ReactInstanceManagerBuilder {
val builder =
ReactInstanceManager.builder()
.setApplication(mApplication)
.setJSMainModulePath(getJSMainModuleName())
.setUseDeveloperSupport(getUseDeveloperSupport())
.setDevSupportManagerFactory(getDevSupportManagerFactory())
.setDevLoadingViewManager(getDevLoadingViewManager())
.setRequireActivity(getShouldRequireActivity())
.setSurfaceDelegateFactory(getSurfaceDelegateFactory())
.setJSExceptionHandler(getJSExceptionHandler())
.setLazyViewManagersEnabled(getLazyViewManagersEnabled())
.setRedBoxHandler(getRedBoxHandler())
.setJavaScriptExecutorFactory(getJavaScriptExecutorFactory())
.setUIManagerProvider(getUIManagerProvider())
.setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
.setReactPackageTurboModuleManagerDelegateBuilder(
getReactPackageTurboModuleManagerDelegateBuilder()
)
.setChoreographerProvider(getChoreographerProvider())
.setPausedInDebuggerOverlayManager(getPausedInDebuggerOverlayManager())
for (reactPackage in getPackages()) {
builder.addPackage(reactPackage)
}
val jsBundleFile = getJSBundleFile()
if (jsBundleFile != null) {
builder.setJSBundleFile(jsBundleFile)
} else {
builder.setBundleAssetName(
com.facebook.infer.annotation.Assertions.assertNotNull(getBundleAssetName())
)
}
return builder
}
/** Get the [RedBoxHandler] to send RedBox-related callbacks to. */
protected open fun getRedBoxHandler(): RedBoxHandler? = null
protected open fun getJSExceptionHandler(): JSExceptionHandler? = null
/** Get the [JavaScriptExecutorFactory]. Override this to use a custom Executor. */
protected open fun getJavaScriptExecutorFactory(): JavaScriptExecutorFactory? = null
protected open fun getReactPackageTurboModuleManagerDelegateBuilder():
ReactPackageTurboModuleManagerDelegate.Builder? = null
protected fun getApplication(): Application = mApplication
protected open fun getUIManagerProvider(): UIManagerProvider? = UIManagerProvider { null }
/** Returns whether or not to treat it as normal if Activity is null. */
public open fun getShouldRequireActivity(): Boolean = true
/**
* Returns whether view managers should be created lazily. See [ViewManagerOnDemandReactPackage]
* for details.
*
* @experimental
*/
public open fun getLazyViewManagersEnabled(): Boolean = false
/**
* Return the [SurfaceDelegateFactory] used by NativeModules to get access to a SurfaceDelegate to
* interact with a surface. By default in the mobile platform the SurfaceDelegate it returns is
* null, and the NativeModule needs to implement its own SurfaceDelegate to decide how it would
* interact with its own container surface.
*/
public open fun getSurfaceDelegateFactory(): SurfaceDelegateFactory = SurfaceDelegateFactory {
null
}
/** Get the [DevLoadingViewManager]. Override this to use a custom dev loading view manager. */
protected open fun getDevLoadingViewManager(): DevLoadingViewManager? = null
protected open fun getPausedInDebuggerOverlayManager(): PausedInDebuggerOverlayManager? = null
/**
* Returns the name of the main module. Determines the URL used to fetch the JS bundle from Metro.
* It is only used when dev support is enabled. This is the first file to be executed once the
* [ReactInstanceManager] is created. e.g. "index.android"
*/
protected open fun getJSMainModuleName(): String = "index.android"
/**
* Returns a custom path of the bundle file. This is used in cases the bundle should be loaded
* from a custom path. By default it is loaded from Android assets, from a path specified by
* [getBundleAssetName]. e.g. "file://sdcard/myapp_cache/index.android.bundle"
*/
protected open fun getJSBundleFile(): String? = null
/**
* Returns the name of the bundle in assets. If this is null, and no file path is specified for
* the bundle, the app will only work with [getUseDeveloperSupport] enabled and will always try to
* load the JS bundle from Metro. e.g. "index.android.bundle"
*/
protected open fun getBundleAssetName(): String? = "index.android.bundle"
/** Returns whether dev mode should be enabled. This enables e.g. the dev menu. */
public abstract fun getUseDeveloperSupport(): Boolean
/** Get the [DevSupportManagerFactory]. Override this to use a custom dev support manager. */
protected open fun getDevSupportManagerFactory(): DevSupportManagerFactory? = null
/**
* Returns a list of [ReactPackage] used by the app. You'll most likely want to return at least
* the `MainReactPackage`. If your app uses additional views or modules besides the default ones,
* you'll want to include more packages here.
*/
protected abstract fun getPackages(): List<ReactPackage>
/**
* Returns a custom implementation of ChoreographerProvider to be used this host. If null - React
* will use default direct android.view.Choreographer-based provider.
*/
protected open fun getChoreographerProvider(): ChoreographerProvider? = null
private companion object {
init {
LegacyArchitectureLogger.assertLegacyArchitecture(
"ReactNativeHost",
LegacyArchitectureLogLevel.ERROR,
)
}
}
}
@@ -54,7 +54,7 @@ protected constructor(
DefaultComponentsRegistry.register(componentFactory)
val viewManagerRegistry =
if (lazyViewManagersEnabled) {
if (getLazyViewManagersEnabled()) {
ViewManagerRegistry(
object : ViewManagerResolver {
override fun getViewManager(viewManagerName: String) =
@@ -120,12 +120,12 @@ protected constructor(
val concreteJSRuntimeFactory = jsRuntimeFactory ?: HermesInstance()
return DefaultReactHost.getDefaultReactHost(
context,
packages,
jsMainModuleName,
bundleAssetName ?: "index.android.bundle",
jsBundleFile,
getPackages(),
getJSMainModuleName(),
getBundleAssetName() ?: "index.android.bundle",
getJSBundleFile(),
concreteJSRuntimeFactory,
useDeveloperSupport,
getUseDeveloperSupport(),
)
}
}