Skip to content

Commit effd7e1

Browse files
committed
fix android reload in bridge-less mode
1 parent 9a00cf7 commit effd7e1

File tree

2 files changed

+63
-81
lines changed

2 files changed

+63
-81
lines changed

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

Lines changed: 62 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,31 @@ public void onDownloadFailed(Throwable error) {
9898
}
9999
});
100100
}catch (Exception e){
101-
promise.reject("执行报错:" + e.getMessage());
101+
promise.reject("downloadPatchFromPpk failed: "+e.getMessage());
102102
}
103103
}
104104

105105
public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options, Promise promise) {
106106
final String hash = options.getString("hash");
107-
108-
if (hash == null || hash.isEmpty()) {
109-
promise.reject("hash不能为空");
110-
return;
111-
}
112107
UiThreadUtil.runOnUiThread(new Runnable() {
113108
@Override
114109
public void run() {
110+
111+
updateContext.switchVersion(hash);
112+
final Context application = mContext.getApplicationContext();
113+
JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application));
115114
try {
116-
updateContext.switchVersion(hash);
117-
final Context application = mContext.getApplicationContext();
118115
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
119116

120117
if (instanceManager == null) {
121118
instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
122119
}
123120

124121
try {
125-
JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application));
126122
Field loadField = instanceManager.getClass().getDeclaredField("mBundleLoader");
127123
loadField.setAccessible(true);
128124
loadField.set(instanceManager, loader);
129125
} catch (Throwable err) {
130-
promise.reject("pushy:"+err.getMessage());
131126
Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile");
132127
jsBundleField.setAccessible(true);
133128
jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application));
@@ -137,31 +132,36 @@ public void run() {
137132
promise.resolve(true);
138133

139134
} catch (Throwable err) {
140-
promise.reject(err);
141-
Log.e("pushy", "switchVersion failed ", err);
142135
final Activity currentActivity = mContext.getCurrentActivity();
143136
if (currentActivity == null) {
144137
return;
145138
}
146139
try {
147-
// Try to get getReactDelegate method using reflection
148140
java.lang.reflect.Method getReactDelegateMethod =
149141
ReactActivity.class.getMethod("getReactDelegate");
150-
if (getReactDelegateMethod != null) {
151-
ReactDelegate reactDelegate = (ReactDelegate)
152-
getReactDelegateMethod.invoke(currentActivity);
153-
154-
// Try to get reload method using reflection
155-
java.lang.reflect.Method reloadMethod =
156-
ReactDelegate.class.getMethod("reload");
157-
if (reloadMethod != null) {
158-
reloadMethod.invoke(reactDelegate);
159-
} else {
160-
throw new NoSuchMethodException();
161-
}
162-
} else {
163-
throw new NoSuchMethodException();
164-
}
142+
143+
ReactDelegate reactDelegate = (ReactDelegate)
144+
getReactDelegateMethod.invoke(currentActivity);
145+
146+
Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost");
147+
reactHostField.setAccessible(true);
148+
Object reactHost = reactHostField.get(reactDelegate);
149+
150+
// Access the mReactHostDelegate field
151+
Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate");
152+
reactHostDelegateField.setAccessible(true);
153+
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
154+
155+
// Modify the jsBundleLoader field
156+
Field jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
157+
jsBundleLoaderField.setAccessible(true);
158+
jsBundleLoaderField.set(reactHostDelegate, loader);
159+
160+
// Get the reload method with a String parameter
161+
java.lang.reflect.Method reloadMethod = reactHost.getClass().getMethod("reload", String.class);
162+
163+
// Invoke the reload method with a reason
164+
reloadMethod.invoke(reactHost, "react-native-update");
165165
} catch (Throwable e) {
166166
currentActivity.runOnUiThread(new Runnable() {
167167
@Override
@@ -171,72 +171,54 @@ public void run() {
171171
});
172172
}
173173
}
174+
promise.resolve(true);
174175
}
175176
});
176177
}
177178

178179

179-
public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options,Promise promise) {
180-
try {
181-
final String hash = options.getString("hash");
182-
if(hash==null || hash.isEmpty()){
183-
promise.reject("hash不能为空");
184-
return;
185-
}
186-
UiThreadUtil.runOnUiThread(new Runnable() {
187-
@Override
188-
public void run() {
189-
try {
190-
updateContext.switchVersion(hash);
191-
promise.resolve(true);
192-
} catch (Throwable err) {
193-
promise.reject("switchVersionLater failed:"+err.getMessage());
194-
Log.e("pushy", "switchVersionLater failed", err);
195-
}
180+
public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) {
181+
final String hash = options.getString("hash");
182+
UiThreadUtil.runOnUiThread(new Runnable() {
183+
@Override
184+
public void run() {
185+
try {
186+
updateContext.switchVersion(hash);
187+
promise.resolve(true);
188+
} catch (Throwable err) {
189+
promise.reject("switchVersionLater failed: "+err.getMessage());
190+
Log.e("pushy", "switchVersionLater failed", err);
196191
}
197-
});
198-
} catch (Exception e){
199-
promise.reject("执行报错:"+e.getMessage());
200-
}
192+
}
193+
});
201194
}
202195

203-
public static void markSuccess(UpdateContext updateContext,Promise promise) {
204-
try {
205-
UiThreadUtil.runOnUiThread(new Runnable() {
206-
@Override
207-
public void run() {
208-
updateContext.markSuccess();
209-
promise.resolve(true);
210-
}
211-
});
212-
} catch (Exception e){
213-
promise.reject("执行报错:"+e.getMessage());
214-
}
196+
public static void markSuccess(UpdateContext updateContext, Promise promise) {
197+
UiThreadUtil.runOnUiThread(new Runnable() {
198+
@Override
199+
public void run() {
200+
updateContext.markSuccess();
201+
promise.resolve(true);
202+
}
203+
});
215204
}
216205

217206
public static void setUuid(UpdateContext updateContext, String uuid, Promise promise) {
218-
try {
219-
UiThreadUtil.runOnUiThread(new Runnable() {
220-
@Override
221-
public void run() {
222-
updateContext.setKv("uuid", uuid);
223-
promise.resolve(true);
224-
}
225-
});
226-
} catch (Exception e){
227-
promise.reject("执行报错:"+e.getMessage());
228-
}
229-
207+
UiThreadUtil.runOnUiThread(new Runnable() {
208+
@Override
209+
public void run() {
210+
updateContext.setKv("uuid", uuid);
211+
promise.resolve(true);
212+
}
213+
});
230214
}
231215

232216
public static boolean check(String json) {
233217
ObjectMapper mapper = new ObjectMapper();
234218
try {
235219
mapper.readValue(json, Map.class);
236-
System.out.println("String can be converted to Map");
237220
return true;
238221
} catch (IOException e) {
239-
System.out.println("String cannot be converted to Map");
240222
return false;
241223
}
242224
}
@@ -246,12 +228,12 @@ public static void setLocalHashInfo(UpdateContext updateContext, final String ha
246228
UiThreadUtil.runOnUiThread(new Runnable() {
247229
@Override
248230
public void run() {
249-
if(!check(info)){
250-
updateContext.setKv("hash_" + hash, info);
251-
promise.reject("校验报错:json字符串格式错误");
252-
}else {
231+
if (check(info)) {
253232
updateContext.setKv("hash_" + hash, info);
254233
promise.resolve(true);
234+
} else {
235+
updateContext.setKv("hash_" + hash, info);
236+
promise.reject("setLocalHashInfo failed: invalid json string");
255237
}
256238
}
257239
});
@@ -262,7 +244,7 @@ public static void getLocalHashInfo(UpdateContext updateContext, final String ha
262244
if (check(value)) {
263245
promise.resolve(value);
264246
} else {
265-
promise.reject("校验报错:json字符串格式错误");
247+
promise.reject("getLocalHashInfo failed: invalid json string");
266248
}
267249

268250
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update",
3-
"version": "10.24.3",
3+
"version": "10.25.0",
44
"description": "react-native hot update",
55
"main": "src/index",
66
"scripts": {

0 commit comments

Comments
 (0)