Skip to content

Commit ef34404

Browse files
committed
fix
1 parent 9f65d59 commit ef34404

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.facebook.react.ReactApplication;
99
import com.facebook.react.ReactDelegate;
1010
import com.facebook.react.ReactInstanceManager;
11+
import com.facebook.react.ReactNativeHost;
1112
import com.facebook.react.bridge.JSBundleLoader;
1213
import com.facebook.react.bridge.Promise;
1314
import com.facebook.react.bridge.ReactApplicationContext;
@@ -18,6 +19,7 @@
1819
import java.io.File;
1920
import java.io.IOException;
2021
import java.lang.reflect.Field;
22+
import java.lang.reflect.Method;
2123
import java.util.Map;
2224

2325
public class UpdateModuleImpl {
@@ -56,6 +58,31 @@ private static String capitalize(String str) {
5658
return str.substring(0, 1).toUpperCase() + str.substring(1);
5759
}
5860

61+
private static String getDefaultBundleAssetName(Context application) {
62+
String bundleAssetName = "index.android.bundle";
63+
if (!(application instanceof ReactApplication)) {
64+
return bundleAssetName;
65+
}
66+
67+
try {
68+
ReactNativeHost reactNativeHost = ((ReactApplication) application).getReactNativeHost();
69+
if (reactNativeHost == null) {
70+
return bundleAssetName;
71+
}
72+
73+
Method getBundleAssetNameMethod = ReactNativeHost.class.getDeclaredMethod("getBundleAssetName");
74+
getBundleAssetNameMethod.setAccessible(true);
75+
Object resolvedBundleAssetName = getBundleAssetNameMethod.invoke(reactNativeHost);
76+
if (resolvedBundleAssetName instanceof String && !((String) resolvedBundleAssetName).isEmpty()) {
77+
return (String) resolvedBundleAssetName;
78+
}
79+
} catch (Exception e) {
80+
Log.e(NAME, "Failed to get default asset name from ReactNativeHost: " + e.getMessage());
81+
}
82+
83+
return bundleAssetName;
84+
}
85+
5986
public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
6087
String url = options.getString("updateUrl");
6188
String hash = options.getString("hash");
@@ -149,7 +176,6 @@ public void run() {
149176
updateContext.switchVersion(hash);
150177
}
151178

152-
final Context application = mContext.getApplicationContext();
153179
final Context application = mContext.getApplicationContext();
154180
String updateBundlePath = updateContext.getBundleUrl(application);
155181

@@ -158,17 +184,7 @@ public void run() {
158184
if (updateBundlePath != null) {
159185
loader = JSBundleLoader.createFileLoader(updateBundlePath);
160186
} else {
161-
String bundleAssetName = "index.android.bundle";
162-
try {
163-
ReactInstanceManager defaultInstanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
164-
String rnBundleAssetName = defaultInstanceManager.getBundleAssetName();
165-
if (rnBundleAssetName != null && !rnBundleAssetName.isEmpty()) {
166-
bundleAssetName = rnBundleAssetName;
167-
}
168-
} catch (Exception e) {
169-
Log.e(NAME, "Failed to get default asset name from ReactNativeHost: " + e.getMessage());
170-
}
171-
loader = JSBundleLoader.createAssetLoader(application, bundleAssetName, false);
187+
loader = JSBundleLoader.createAssetLoader(application, getDefaultBundleAssetName(application), false);
172188
}
173189
try {
174190
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
@@ -324,4 +340,3 @@ public static void getLocalHashInfo(UpdateContext updateContext, final String ha
324340
}
325341

326342
}
327-

0 commit comments

Comments
 (0)