diff --git a/ReactAndroid/src/main/java/com/facebook/react/JSEngineResolutionAlgorithm.java b/ReactAndroid/src/main/java/com/facebook/react/JSEngineResolutionAlgorithm.java new file mode 100644 index 00000000000000..7f33bc78c5a55c --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/JSEngineResolutionAlgorithm.java @@ -0,0 +1,18 @@ +/* + * 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; + +/** + * An enum that specifies the algorithm to use when loading theJS Engine. {@link #JSC} will load + * JavaScriptCore first and fail if it is not available. {@link #HERMES} will load Hermes first and + * fail if it is not available. + */ +public enum JSEngineResolutionAlgorithm { + JSC, + HERMES +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/JSInterpreter.java b/ReactAndroid/src/main/java/com/facebook/react/JSInterpreter.java deleted file mode 100644 index 7e960263cc55da..00000000000000 --- a/ReactAndroid/src/main/java/com/facebook/react/JSInterpreter.java +++ /dev/null @@ -1,18 +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; - -/** - * An enum that specifies the JS Engine to be used in the app Old Logic uses the legacy code - * JSC/HERMES loads the respective engine using the revamped logic - */ -public enum JSInterpreter { - OLD_LOGIC, - JSC, - HERMES -} diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index 6dcb191808611f..68ac1ed63451e1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -14,6 +14,7 @@ import android.app.Application; import android.content.Context; import androidx.annotation.Nullable; +import com.facebook.common.logging.FLog; import com.facebook.hermes.reactexecutor.HermesExecutor; import com.facebook.hermes.reactexecutor.HermesExecutorFactory; import com.facebook.infer.annotation.Assertions; @@ -40,6 +41,8 @@ /** Builder class for {@link ReactInstanceManager} */ public class ReactInstanceManagerBuilder { + private static final String TAG = ReactInstanceManagerBuilder.class.getSimpleName(); + private final List mPackages = new ArrayList<>(); private @Nullable String mJSBundleAssetUrl; @@ -64,7 +67,7 @@ public class ReactInstanceManagerBuilder { private @Nullable Map mCustomPackagerCommandHandlers; private @Nullable ReactPackageTurboModuleManagerDelegate.Builder mTMMDelegateBuilder; private @Nullable SurfaceDelegateFactory mSurfaceDelegateFactory; - private JSInterpreter jsInterpreter = JSInterpreter.OLD_LOGIC; + private JSEngineResolutionAlgorithm jsEngineResolutionAlgorithm = null; /* package protected */ ReactInstanceManagerBuilder() {} @@ -118,28 +121,12 @@ public ReactInstanceManagerBuilder setJSBundleLoader(JSBundleLoader jsBundleLoad } /** - * Sets the jsEngine as JSC or HERMES as per the setJsEngineAsHermes call Uses the enum {@link - * JSInterpreter} - * - * @param jsInterpreter - */ - private void setJSEngine(JSInterpreter jsInterpreter) { - this.jsInterpreter = jsInterpreter; - } - - /** - * Utility setter to set the required JSEngine as HERMES or JSC Defaults to OLD_LOGIC if not - * called by the host app - * - * @param hermesEnabled hermesEnabled = true sets the JS Engine as HERMES and JSC otherwise + * Sets the JS Engine to load as either Hermes or JSC. If not set, the default is JSC with a + * Hermes fallback. */ - public ReactInstanceManagerBuilder setJsEngineAsHermes(boolean hermesEnabled) { - if (hermesEnabled) { - setJSEngine(JSInterpreter.HERMES); - } else { - setJSEngine(JSInterpreter.JSC); - } - return this; + private void setJSEngineResolutionAlgorithm( + @Nullable JSEngineResolutionAlgorithm jsEngineResolutionAlgorithm) { + this.jsEngineResolutionAlgorithm = jsEngineResolutionAlgorithm; } /** @@ -365,7 +352,11 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory( // if nothing is specified, use old loading method // else load the required engine - if (jsInterpreter == JSInterpreter.OLD_LOGIC) { + if (jsEngineResolutionAlgorithm == null) { + FLog.w( + TAG, + "You're not setting the JS Engine Resolution Algorithm. " + + "We'll try to load JSC first, and if it fails we'll fallback to Hermes"); try { // If JSC is included, use it as normal initializeSoLoaderIfNecessary(applicationContext); @@ -378,7 +369,7 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory( HermesExecutor.loadLibrary(); return new HermesExecutorFactory(); } - } else if (jsInterpreter == JSInterpreter.HERMES) { + } else if (jsEngineResolutionAlgorithm == JSEngineResolutionAlgorithm.HERMES) { HermesExecutor.loadLibrary(); return new HermesExecutorFactory(); } else { diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java b/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java index 02a23c36a10bb1..d030019b035373 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactNativeHost.java @@ -190,4 +190,12 @@ protected String getJSMainModuleName() { * default ones, you'll want to include more packages here. */ protected abstract List getPackages(); + + /** + * Returns the {@link JSEngineResolutionAlgorithm} to be used when loading the JS engine. If null, + * will try to load JSC first and fallback to Hermes if JSC is not available. + */ + protected @Nullable JSEngineResolutionAlgorithm getJSEngineResolutionAlgorithm() { + return null; + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactNativeHost.kt b/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactNativeHost.kt index 9bbaee4ebbfadb..23c0bada20a220 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactNativeHost.kt +++ b/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactNativeHost.kt @@ -8,6 +8,7 @@ package com.facebook.react.defaults import android.app.Application +import com.facebook.react.JSEngineResolutionAlgorithm import com.facebook.react.ReactNativeHost import com.facebook.react.ReactPackageTurboModuleManagerDelegate import com.facebook.react.bridge.JSIModulePackage @@ -20,8 +21,10 @@ import com.facebook.react.bridge.JSIModulePackage * providing the default TurboModuleManagerDelegateBuilder and the default JSIModulePackage, * provided the name of the dynamic library to load. */ -abstract class DefaultReactNativeHost protected constructor(application: Application) : - ReactNativeHost(application) { +abstract class DefaultReactNativeHost +protected constructor( + application: Application, +) : ReactNativeHost(application) { override fun getReactPackageTurboModuleManagerDelegateBuilder(): ReactPackageTurboModuleManagerDelegate.Builder? = @@ -38,6 +41,13 @@ abstract class DefaultReactNativeHost protected constructor(application: Applica null } + override fun getJSEngineResolutionAlgorithm(): JSEngineResolutionAlgorithm? = + when (isHermesEnabled) { + true -> JSEngineResolutionAlgorithm.HERMES + false -> JSEngineResolutionAlgorithm.JSC + null -> null + } + /** * Returns whether the user wants to use the New Architecture or not. * @@ -48,4 +58,14 @@ abstract class DefaultReactNativeHost protected constructor(application: Applica */ protected open val isNewArchEnabled: Boolean get() = false + + /** + * Returns whether the user wants to use Hermes. + * + * If true, the app will load the Hermes engine, and fail if not found. If false, the app will + * load the JSC engine, and fail if not found. If null, the app will attempt to load JSC first and + * fallback to Hermes if not found. + */ + protected open val isHermesEnabled: Boolean? + get() = null } diff --git a/packages/rn-tester/android/app/build.gradle b/packages/rn-tester/android/app/build.gradle index c26d4444cf3030..82cb8afbc84880 100644 --- a/packages/rn-tester/android/app/build.gradle +++ b/packages/rn-tester/android/app/build.gradle @@ -139,9 +139,11 @@ android { productFlavors { hermes { dimension "vm" + buildConfigField("boolean", "IS_HERMES_ENABLED_IN_FLAVOR", "true") } jsc { dimension "vm" + buildConfigField("boolean", "IS_HERMES_ENABLED_IN_FLAVOR", "false") } } 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 index cdc5ac628282a9..4570ee8e75a707 100644 --- 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 @@ -115,6 +115,11 @@ public List createViewManagers( protected boolean isNewArchEnabled() { return true; } + + @Override + protected Boolean isHermesEnabled() { + return BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR; + } }; @Override diff --git a/template/android/app/src/main/java/com/helloworld/MainApplication.java b/template/android/app/src/main/java/com/helloworld/MainApplication.java index ccf1addb2b9d9b..03e36b14f47f34 100644 --- a/template/android/app/src/main/java/com/helloworld/MainApplication.java +++ b/template/android/app/src/main/java/com/helloworld/MainApplication.java @@ -38,6 +38,11 @@ protected String getJSMainModuleName() { protected boolean isNewArchEnabled() { return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; } + + @Override + protected Boolean isHermesEnabled() { + return BuildConfig.IS_HERMES_ENABLED; + } }; @Override