Skip to content

Commit 9f65d59

Browse files
authored
fix: Dynamically load JS bundle (from update or assets) to prevent NPE (#527)
1 parent 54c2c7a commit 9f65d59

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,26 @@ public void run() {
150150
}
151151

152152
final Context application = mContext.getApplicationContext();
153-
JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application));
153+
final Context application = mContext.getApplicationContext();
154+
String updateBundlePath = updateContext.getBundleUrl(application);
155+
156+
JSBundleLoader loader;
157+
158+
if (updateBundlePath != null) {
159+
loader = JSBundleLoader.createFileLoader(updateBundlePath);
160+
} 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);
172+
}
154173
try {
155174
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
156175

0 commit comments

Comments
 (0)