Skip to content

Commit 60e00d9

Browse files
Petter Hesselbergfacebook-github-bot
Petter Hesselberg
authored andcommitted
Ensure SoLoader is initialized before attempting to load jscexecutor … (#26343)
Summary: This change fixes the issue "[ReactInstanceManagerBuilder.build fails unless SoLoader.init has been called](#26342)" on Android. The `ReactInstanceManager` constructor calls `initializeSoLoaderIfNecessary`, so the intent is clearly that things should work without an explicit call to `SoLoader.init` on the part of the application. However, with the introduction of Hermes, we have `ReactInstanceManagerBuilder.getDefaultJSExecutorFactory`, which gets called before the `ReactInstanceManager` constructor. It attempts this: SoLoader.loadLibrary("jscexecutor"); This fails with the following stack trace: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.react.uiapp/com.facebook.react.uiapp.RNTesterActivity}: java.lang.RuntimeException: SoLoader.init() not yet called at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.RuntimeException: SoLoader.init() not yet called at com.facebook.soloader.SoLoader.assertInitialized(SoLoader.java:781) at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:505) at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:484) at com.facebook.react.ReactInstanceManagerBuilder.getDefaultJSExecutorFactory(ReactInstanceManagerBuilder.java:291) at com.facebook.react.ReactInstanceManagerBuilder.build(ReactInstanceManagerBuilder.java:266) at com.facebook.react.ReactNativeHost.createReactInstanceManager(ReactNativeHost.java:86) at com.facebook.react.ReactNativeHost.getReactInstanceManager(ReactNativeHost.java:38) at com.facebook.react.ReactDelegate.loadApp(ReactDelegate.java:103) at com.facebook.react.ReactActivityDelegate.loadApp(ReactActivityDelegate.java:83) at com.facebook.react.ReactActivityDelegate.onCreate(ReactActivityDelegate.java:78) at com.facebook.react.uiapp.RNTesterActivity$RNTesterActivityDelegate.onCreate(RNTesterActivity.java:40) at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:44) at android.app.Activity.performCreate(Activity.java:7183) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6944)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)  ## Changelog [Android] [Fixed] - Don't crash ReactInstanceManagerBuilder.build even if SoLoader has not been explicitly initialized Pull Request resolved: #26343 Test Plan: To demonstrate the defect, remove the call to `SoLoader.init` from `RNTester.onCreate` and run the app. To demonstrate the fix, apply this PR, which does in fact also remove the call to `SoLoader.init` Differential Revision: D17223701 Pulled By: mdvacca fbshipit-source-id: c508ab52bd3fefe8a946ebab7b2906a5d8c21e0f
1 parent fa6add7 commit 60e00d9

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

RNTester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public List<ReactPackage> getPackages() {
4747
public void onCreate() {
4848
ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik);
4949
super.onCreate();
50-
SoLoader.init(this, /* native exopackage */ false);
5150
initializeFlipper(this);
5251
}
5352

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public List<ReactPackage> getPackages() {
317317
return new ArrayList<>(mPackages);
318318
}
319319

320-
private static void initializeSoLoaderIfNecessary(Context applicationContext) {
320+
static void initializeSoLoaderIfNecessary(Context applicationContext) {
321321
// Call SoLoader.initialize here, this is required for apps that does not use exopackage and
322322
// does not use SoLoader for loading other native code except from the one used by React Native
323323
// This way we don't need to require others to have additional initialization code and to

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
package com.facebook.react;
77

8+
import static com.facebook.react.ReactInstanceManager.initializeSoLoaderIfNecessary;
89
import static com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getFriendlyDeviceName;
910

1011
import android.app.Activity;
1112
import android.app.Application;
13+
import android.content.Context;
14+
1215
import androidx.annotation.Nullable;
1316
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
1417
import com.facebook.infer.annotation.Assertions;
@@ -254,6 +257,7 @@ public ReactInstanceManager build() {
254257
}
255258

256259
// We use the name of the device and the app for debugging & metrics
260+
//noinspection ConstantConditions
257261
String appName = mApplication.getPackageName();
258262
String deviceName = getFriendlyDeviceName();
259263

@@ -262,7 +266,7 @@ public ReactInstanceManager build() {
262266
mCurrentActivity,
263267
mDefaultHardwareBackBtnHandler,
264268
mJavaScriptExecutorFactory == null
265-
? getDefaultJSExecutorFactory(appName, deviceName)
269+
? getDefaultJSExecutorFactory(appName, deviceName, mApplication.getApplicationContext())
266270
: mJavaScriptExecutorFactory,
267271
(mJSBundleLoader == null && mJSBundleAssetUrl != null)
268272
? JSBundleLoader.createAssetLoader(
@@ -284,9 +288,10 @@ public ReactInstanceManager build() {
284288
mCustomPackagerCommandHandlers);
285289
}
286290

287-
private JavaScriptExecutorFactory getDefaultJSExecutorFactory(String appName, String deviceName) {
291+
private JavaScriptExecutorFactory getDefaultJSExecutorFactory(String appName, String deviceName, Context applicationContext) {
288292
try {
289293
// If JSC is included, use it as normal
294+
initializeSoLoaderIfNecessary(applicationContext);
290295
SoLoader.loadLibrary("jscexecutor");
291296
return new JSCExecutorFactory(appName, deviceName);
292297
} catch (UnsatisfiedLinkError jscE) {

0 commit comments

Comments
 (0)