Skip to content

Commit 763b06b

Browse files
committed
fix(Android): support React Native version 0.81+ by updating field access for ReactHost
1 parent ea45e52 commit 763b06b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

android/app/proguard-rules.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
private final ** mBundleLoader;
2222
}
2323
-keepclassmembers class com.facebook.react.ReactDelegate {
24-
private ** mReactHost; # bridgeless
24+
private ** mReactHost; # RN < 0.81
25+
private ** reactHost; # RN 0.81+
2526
public void reload(...); # RN 0.74 and above
2627
}
2728
# bridgeless
@@ -30,7 +31,8 @@
3031
}
3132
# bridgeless
3233
-keepclassmembers class com.facebook.react.runtime.ReactHostImpl {
33-
private final ** mReactHostDelegate;
34+
private final ** mReactHostDelegate; # RN < 0.81
35+
private final ** reactHostDelegate; # RN 0.81+
3436
}
3537

3638
# Can't find referenced class org.bouncycastle.**

android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import com.facebook.react.ReactDelegate;
1414
import com.facebook.react.ReactHost;
15-
import com.facebook.react.ReactInstanceManager;
1615
import com.facebook.react.ReactActivity;
1716
import com.facebook.react.ReactRootView;
1817
import com.facebook.react.bridge.Arguments;
@@ -140,9 +139,16 @@ private void setJSBundle(String latestJSBundleFile) throws IllegalAccessExceptio
140139

141140
@OptIn(markerClass = UnstableReactNativeAPI.class)
142141
private void setJSBundleLoaderBridgeless(ReactHost reactHost, JSBundleLoader latestJSBundleLoader) throws NoSuchFieldException, IllegalAccessException {
143-
Field mReactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate");
144-
mReactHostDelegateField.setAccessible(true);
145-
ReactHostDelegate reactHostDelegate = (ReactHostDelegate) mReactHostDelegateField.get(reactHost);
142+
Field reactHostDelegateField;
143+
try {
144+
// RN < 0.81
145+
reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate");
146+
} catch (NoSuchFieldException e) {
147+
// RN >= 0.81
148+
reactHostDelegateField = reactHost.getClass().getDeclaredField("reactHostDelegate");
149+
}
150+
reactHostDelegateField.setAccessible(true);
151+
ReactHostDelegate reactHostDelegate = (ReactHostDelegate) reactHostDelegateField.get(reactHost);
146152
assert reactHostDelegate != null;
147153
Field jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
148154
jsBundleLoaderField.setAccessible(true);
@@ -220,7 +226,14 @@ private ReactHost resolveReactHost() {
220226
}
221227

222228
try {
223-
Field reactHostField = reactDelegate.getClass().getDeclaredField("mReactHost");
229+
Field reactHostField;
230+
try {
231+
// RN < 0.81
232+
reactHostField = reactDelegate.getClass().getDeclaredField("mReactHost");
233+
} catch (NoSuchFieldException e) {
234+
// RN >= 0.81
235+
reactHostField = reactDelegate.getClass().getDeclaredField("reactHost");
236+
}
224237
reactHostField.setAccessible(true);
225238
return (ReactHost) reactHostField.get(reactDelegate);
226239
} catch (Exception e) {

0 commit comments

Comments
 (0)