From 70a757fc1ede08fcb9d5014f3b05c94e5badaea7 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Wed, 13 Dec 2023 04:49:43 -0800 Subject: [PATCH] Update getDefaultJSExecutorFactory to load Hermes first and fallback to JSC (#41914) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41914 If the user does not specify which engine they're using, we still default to loading JSC first and then attempting to load Hermes. This has a small performance hit (as we attempt to load an existing library) + it prints an inactionable log for the user every time. Changelog: [Android] [Fixed] - Update getDefaultJSExecutorFactory to load Hermes first and fallback to JSC Reviewed By: luluwu2032 Differential Revision: D52080545 fbshipit-source-id: 95f37304d713da7d7079eabbd2dfdf230d29a1b9 --- .../react/ReactInstanceManagerBuilder.java | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index f574f81c416..3ae051fbfef 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -373,31 +373,28 @@ public class ReactInstanceManagerBuilder { private JavaScriptExecutorFactory getDefaultJSExecutorFactory( String appName, String deviceName, Context applicationContext) { - // Relying solely on try catch block and loading jsc even when - // project is using hermes can lead to launch-time crashes especially in - // monorepo architectures and hybrid apps using both native android - // and react native. - // So we can use the value of enableHermes received by the constructor - // to decide which library to load at launch - - // if nothing is specified, use old loading method - // else load the required engine + initializeSoLoaderIfNecessary(applicationContext); + // Hermes has been enabled by default in OSS since React Native 0.70. + // If the user hasn't specified a JSEngineResolutionAlgorithm, + // we attempt to load Hermes first, and fallback to JSC if we can't resolve the library. if (mJSEngineResolutionAlgorithm == 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); - JSCExecutor.loadLibrary(); - return new JSCExecutorFactory(appName, deviceName); - } catch (UnsatisfiedLinkError jscE) { - if (jscE.getMessage().contains("__cxa_bad_typeid")) { - throw jscE; - } HermesExecutor.loadLibrary(); return new HermesExecutorFactory(); + } catch (UnsatisfiedLinkError ignoredHermesError) { + try { + JSCExecutor.loadLibrary(); + return new JSCExecutorFactory(appName, deviceName); + } catch (UnsatisfiedLinkError jscError) { + FLog.e( + TAG, + "Unable to load neither the Hermes nor the JSC native library. " + + "Your application is not built correctly and will fail to execute"); + if (jscError.getMessage().contains("__cxa_bad_typeid")) { + throw jscError; + } + return null; + } } } else if (mJSEngineResolutionAlgorithm == JSEngineResolutionAlgorithm.HERMES) { HermesExecutor.loadLibrary();