Repository navigation
Expand file tree
/
Copy pathReactReloadManager.java
More file actions
254 lines (225 loc) Β· 9.98 KB
/
Copy pathReactReloadManager.java
File metadata and controls
254 lines (225 loc) Β· 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cn.reactnative.modules.update;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import androidx.annotation.Nullable;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactDelegate;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.ReactApplicationContext;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
final class ReactReloadManager {
private ReactReloadManager() {
}
/**
* For the UI thread: point the running React instance at
* {@code updateBundlePath} and reload it. Throws when no supported
* injection worked β the caller rejects RESTART_FAILED. There is
* deliberately no Activity.recreate() fallback: it would reload the
* previous bundle while the promise reported success (CODE_AUDIT 2.9).
*/
static void reload(
UpdateContext updateContext,
ReactApplicationContext reactContext,
@Nullable String updateBundlePath
) throws Throwable {
Context application = reactContext.getApplicationContext();
Activity currentActivity = reactContext.getCurrentActivity();
boolean newArchitectureEnabled = isNewArchitectureEnabled(application);
Object reactHost = newArchitectureEnabled ? getReactHost(currentActivity, application) : null;
if (reactHost != null) {
try {
reloadReactHost(reactHost, createBundleLoader(application, updateBundlePath, true));
return;
} catch (Throwable err) {
Log.e(Texts.LOG_TAG,
"Failed to restart via ReactHost, trying ReactInstanceManager", err);
}
}
JSBundleLoader loader = createBundleLoader(application, updateBundlePath, false);
ReactInstanceManager instanceManager =
resolveReactInstanceManager(updateContext, application);
// Java (mBundleLoader / mJSBundleFile) and Kotlin (bundleLoader /
// jsBundleFile) spellings of the private fields are both tried.
try {
Field loadField = getCompatibleField(instanceManager.getClass(), "bundleLoader");
loadField.setAccessible(true);
loadField.set(instanceManager, loader);
} catch (Throwable err) {
Field jsBundleField = getCompatibleField(instanceManager.getClass(), "jsBundleFile");
jsBundleField.setAccessible(true);
jsBundleField.set(instanceManager, updateBundlePath);
}
instanceManager.recreateReactContextInBackground();
}
private static Field getCompatibleField(Class<?> clazz, String fieldName)
throws NoSuchFieldException {
try {
return clazz.getDeclaredField("m" + capitalize(fieldName));
} catch (NoSuchFieldException e) {
try {
return clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e2) {
throw new NoSuchFieldException(
"Field not found with either name: m"
+ capitalize(fieldName)
+ " or "
+ fieldName
);
}
}
}
private static String capitalize(String str) {
if (str == null || str.length() == 0) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
private static String getDefaultBundleAssetName(Context application) {
String bundleAssetName = "index.android.bundle";
if (!(application instanceof ReactApplication)) {
return bundleAssetName;
}
try {
ReactNativeHost reactNativeHost = ((ReactApplication) application).getReactNativeHost();
if (reactNativeHost == null) {
return bundleAssetName;
}
Method getBundleAssetNameMethod = ReactNativeHost.class.getDeclaredMethod("getBundleAssetName");
getBundleAssetNameMethod.setAccessible(true);
Object resolvedBundleAssetName = getBundleAssetNameMethod.invoke(reactNativeHost);
if (resolvedBundleAssetName instanceof String && !((String) resolvedBundleAssetName).isEmpty()) {
return (String) resolvedBundleAssetName;
}
} catch (Exception e) {
Log.e(Texts.LOG_TAG, "Failed to get default asset name from ReactNativeHost", e);
}
return bundleAssetName;
}
private static String toAssetUrl(String bundleAssetName) {
if (bundleAssetName == null || bundleAssetName.isEmpty()) {
return "assets://index.android.bundle";
}
if (bundleAssetName.startsWith("assets://")) {
return bundleAssetName;
}
return "assets://" + bundleAssetName;
}
private static JSBundleLoader createBundleLoader(
Context application,
@Nullable String updateBundlePath,
boolean loadAssetSynchronously
) {
if (updateBundlePath != null) {
return JSBundleLoader.createFileLoader(updateBundlePath);
}
return JSBundleLoader.createAssetLoader(
application,
toAssetUrl(getDefaultBundleAssetName(application)),
loadAssetSynchronously
);
}
@Nullable
private static Object getReactHost(@Nullable Activity currentActivity, Context application) {
if (currentActivity instanceof ReactActivity) {
try {
Method getReactDelegateMethod = ReactActivity.class.getMethod("getReactDelegate");
ReactDelegate reactDelegate =
(ReactDelegate) getReactDelegateMethod.invoke(currentActivity);
if (reactDelegate != null) {
Field reactHostField = getCompatibleField(reactDelegate.getClass(), "reactHost");
reactHostField.setAccessible(true);
Object reactHost = reactHostField.get(reactDelegate);
if (reactHost != null) {
return reactHost;
}
}
} catch (Throwable ignored) {
Log.w(Texts.LOG_TAG, "getReactHost via ReactDelegate reflection failed", ignored);
}
}
try {
Method getReactHostMethod = application.getClass().getMethod("getReactHost");
return getReactHostMethod.invoke(application);
} catch (Throwable ignored) {
Log.w(Texts.LOG_TAG, "getReactHost via Application.getReactHost() failed", ignored);
}
return null;
}
private static boolean isNewArchitectureEnabled(Context application) {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
return true;
}
try {
Class<?> buildConfigClass = Class.forName(application.getPackageName() + ".BuildConfig");
Field newArchitectureField = buildConfigClass.getField("IS_NEW_ARCHITECTURE_ENABLED");
if (newArchitectureField.getBoolean(null)) {
return true;
}
} catch (Throwable ignored) {
}
if (application instanceof ReactApplication) {
try {
ReactNativeHost reactNativeHost = ((ReactApplication) application).getReactNativeHost();
Method isNewArchEnabledMethod = getDeclaredMethodInHierarchy(
reactNativeHost.getClass(),
"isNewArchEnabled"
);
isNewArchEnabledMethod.setAccessible(true);
Object result = isNewArchEnabledMethod.invoke(reactNativeHost);
return result instanceof Boolean && (Boolean) result;
} catch (Throwable ignored) {
}
}
return false;
}
private static Method getDeclaredMethodInHierarchy(Class<?> clazz, String methodName)
throws NoSuchMethodException {
Class<?> current = clazz;
while (current != null) {
try {
return current.getDeclaredMethod(methodName);
} catch (NoSuchMethodException ignored) {
current = current.getSuperclass();
}
}
throw new NoSuchMethodException(methodName);
}
private static void reloadReactHost(Object reactHost, JSBundleLoader loader) throws Throwable {
try {
Field devSupportField = getCompatibleField(reactHost.getClass(), "useDevSupport");
devSupportField.setAccessible(true);
devSupportField.set(reactHost, false);
} catch (Throwable ignored) {
}
Field reactHostDelegateField = getCompatibleField(reactHost.getClass(), "reactHostDelegate");
reactHostDelegateField.setAccessible(true);
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
// DefaultReactHostDelegate stores the loader in jsBundleLoader; Expo's
// ExpoReactHostDelegate computes that property from _jsBundleLoader.
Field jsBundleLoaderField;
try {
jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
} catch (NoSuchFieldException e) {
jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("_jsBundleLoader");
}
jsBundleLoaderField.setAccessible(true);
jsBundleLoaderField.set(reactHostDelegate, loader);
Method reloadMethod = reactHost.getClass().getMethod(Texts.RELOAD_METHOD, String.class);
reloadMethod.invoke(reactHost, Texts.LOG_TAG);
}
private static ReactInstanceManager resolveReactInstanceManager(
UpdateContext updateContext,
Context application
) {
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
if (instanceManager != null) {
return instanceManager;
}
return ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
}
}