Skip to content

Commit

Permalink
fix(android): forward-port getReactContext to new arch bridgeless mode
Browse files Browse the repository at this point in the history
...but do so in a way that is backwards-compatible as the symbols required
- do not show up at all until react-native 0.73
- moved to different package names in 0.74
  • Loading branch information
mikehardy committed Oct 18, 2024
1 parent bf9618e commit 4c5afba
Showing 1 changed file with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.os.Looper;
import android.util.Log;
import android.util.SparseArray;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ProcessLifecycleOwner;
Expand Down Expand Up @@ -105,11 +107,41 @@ static void promiseResolver(Promise promise, Exception e) {
}
}

@SuppressLint("VisibleForTests")
private static @Nullable ReactContext getReactContext() {
ReactNativeHost reactNativeHost =
try {

// Carefully try to load new architecture classes so we preserve backwards compatibility
// These symbols are only available in react-native >= 0.73
try {
Class<?> entryPoint = Class.forName("com.facebook.react.defaults.DefaultNewArchitectureEntryPoint");
Method bridgelessEnabled = entryPoint.getMethod("getBridgelessEnabled");
Object result = bridgelessEnabled.invoke(null);
if (result == Boolean.TRUE) {
Log.d("getReactContext", "We are in bridgeless new architecture mode");
Object reactApplication = EventSubscriber.getContext();
Method getReactHost = reactApplication.getClass().getMethod("getReactHost");
Object reactHostInstance = getReactHost.invoke(reactApplication);
Method getCurrentReactContext = reactHostInstance.getClass().getMethod("getCurrentReactContext");
return (ReactContext)getCurrentReactContext.invoke(reactHostInstance);
} else {
Log.d("getReactContext", "we are NOT in bridgeless new architecture mode");
}
} catch (Exception e) {
Log.d("getReactContext", "New Architecture class load failed. Using fallback.");
}

Log.d("getReactContext", "Determining ReactContext using fallback method");
ReactNativeHost reactNativeHost =
((ReactApplication) EventSubscriber.getContext()).getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
} catch (Exception e) {
Log.w("getReactContext", "ReactHost unexpectedly null", e);
}

Log.w("getReactContext", "Unable to determine ReactContext");
return null;
}

private static void initializeReactContext(GenericCallback callback) {
Expand All @@ -121,7 +153,7 @@ private static void initializeReactContext(GenericCallback callback) {
reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceManager.ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(final ReactContext reactContext) {
public void onReactContextInitialized(@NonNull final ReactContext reactContext) {
reactInstanceManager.removeReactInstanceEventListener(this);
new Handler(Looper.getMainLooper()).postDelayed(callback::call, 100);
}
Expand Down

0 comments on commit 4c5afba

Please sign in to comment.