From e0c624210499ce7a85e89ad614367e14484a5f03 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 5 Dec 2024 09:31:43 -0800 Subject: [PATCH] Fix crash on HeadlessJsTaskService on old architecture Summary: 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 Differential Revision: D66826271 --- .../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..5194cb89aee030 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,9 +179,18 @@ protected ReactContext getReactContext() { } private void createReactContextAndScheduleTask(final HeadlessJsTaskConfig taskConfig) { - final ReactHost reactHost = getReactHost(); - - if (reactHost == null) { // old arch + if (ReactNativeFeatureFlags.enableBridgelessArchitecture()) { + final ReactHost reactHost = getReactHost(); + reactHost.addReactInstanceEventListener( + new ReactInstanceEventListener() { + @Override + public void onReactContextInitialized(@NonNull ReactContext reactContext) { + invokeStartTask(reactContext, taskConfig); + reactHost.removeReactInstanceEventListener(this); + } + }); + reactHost.start(); + } else { final ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager(); @@ -194,16 +203,6 @@ public void onReactContextInitialized(@NonNull ReactContext reactContext) { } }); reactInstanceManager.createReactContextInBackground(); - } else { // new arch - reactHost.addReactInstanceEventListener( - new ReactInstanceEventListener() { - @Override - public void onReactContextInitialized(@NonNull ReactContext reactContext) { - invokeStartTask(reactContext, taskConfig); - reactHost.removeReactInstanceEventListener(this); - } - }); - reactHost.start(); } } }