From 4560fc049748a345d5945bc08d43f4b61ca51ff3 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Fri, 6 Dec 2024 12:05:58 -0800 Subject: [PATCH] Fix crash on HeadlessJsTaskService on old architecture (#48124) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48124 Fixes https://github.com/facebook/react-native/issues/47592 The logic in HeadlessJsTaskService is broken. We should not check whether `getReactContext` is null or not. Instead we should use the `enableBridgelessArchitecture` feature flag to understand if New Architecture was enabled or not. The problem we were having is that `HeadlessJsTaskService` was attempting to load the New Architecture even if the user would have it turned off. The Service would then die attempting to load `libappmodules.so` which was correctly missing. Changelog: [Android] [Fixed] - Fix crash on HeadlessJsTaskService on old architecture Reviewed By: javache Differential Revision: D66826271 fbshipit-source-id: 2b8418e0b01b65014cdbfd0ec2f843420a15f9db --- .../facebook/react/HeadlessJsTaskService.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java index 314e68388372ac..b47a4e9f07e25f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java @@ -179,31 +179,30 @@ protected ReactContext getReactContext() { } private void createReactContextAndScheduleTask(final HeadlessJsTaskConfig taskConfig) { - final ReactHost reactHost = getReactHost(); - - if (reactHost == null) { // old arch - final ReactInstanceManager reactInstanceManager = - getReactNativeHost().getReactInstanceManager(); - - reactInstanceManager.addReactInstanceEventListener( + if (ReactNativeFeatureFlags.enableBridgelessArchitecture()) { + final ReactHost reactHost = getReactHost(); + reactHost.addReactInstanceEventListener( new ReactInstanceEventListener() { @Override public void onReactContextInitialized(@NonNull ReactContext reactContext) { invokeStartTask(reactContext, taskConfig); - reactInstanceManager.removeReactInstanceEventListener(this); + reactHost.removeReactInstanceEventListener(this); } }); - reactInstanceManager.createReactContextInBackground(); - } else { // new arch - reactHost.addReactInstanceEventListener( + reactHost.start(); + } else { + final ReactInstanceManager reactInstanceManager = + getReactNativeHost().getReactInstanceManager(); + + reactInstanceManager.addReactInstanceEventListener( new ReactInstanceEventListener() { @Override public void onReactContextInitialized(@NonNull ReactContext reactContext) { invokeStartTask(reactContext, taskConfig); - reactHost.removeReactInstanceEventListener(this); + reactInstanceManager.removeReactInstanceEventListener(this); } }); - reactHost.start(); + reactInstanceManager.createReactContextInBackground(); } } }