|
23 | 23 | public class UpdateModuleImpl {
|
24 | 24 |
|
25 | 25 | public static final String NAME = "Pushy";
|
| 26 | + |
| 27 | + /** |
| 28 | + * 获取字段的兼容性方法,尝试带m前缀和不带m前缀的字段名 |
| 29 | + * @param clazz 目标类 |
| 30 | + * @param fieldName 基础字段名(不带m前缀) |
| 31 | + * @return 找到的字段对象 |
| 32 | + * @throws NoSuchFieldException 如果两种命名都找不到字段 |
| 33 | + */ |
| 34 | + private static Field getCompatibleField(Class<?> clazz, String fieldName) throws NoSuchFieldException { |
| 35 | + // 首先尝试带m前缀的字段名 |
| 36 | + try { |
| 37 | + return clazz.getDeclaredField("m" + capitalize(fieldName)); |
| 38 | + } catch (NoSuchFieldException e) { |
| 39 | + // 如果找不到带m前缀的,尝试不带m前缀的 |
| 40 | + try { |
| 41 | + return clazz.getDeclaredField(fieldName); |
| 42 | + } catch (NoSuchFieldException e2) { |
| 43 | + // 如果都找不到,抛出异常并包含两种尝试的信息 |
| 44 | + throw new NoSuchFieldException("Field not found with either name: m" + capitalize(fieldName) + " or " + fieldName); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 首字母大写的辅助方法 |
| 51 | + */ |
| 52 | + private static String capitalize(String str) { |
| 53 | + if (str == null || str.length() == 0) { |
| 54 | + return str; |
| 55 | + } |
| 56 | + return str.substring(0, 1).toUpperCase() + str.substring(1); |
| 57 | + } |
26 | 58 |
|
27 | 59 | public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
|
28 | 60 | String url = options.getString("updateUrl");
|
@@ -143,16 +175,16 @@ public void run() {
|
143 | 175 | ReactDelegate reactDelegate = (ReactDelegate)
|
144 | 176 | getReactDelegateMethod.invoke(currentActivity);
|
145 | 177 |
|
146 |
| - Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost"); |
| 178 | + Field reactHostField = getCompatibleField(ReactDelegate.class, "reactHost"); |
147 | 179 | reactHostField.setAccessible(true);
|
148 | 180 | Object reactHost = reactHostField.get(reactDelegate);
|
149 | 181 |
|
150 |
| - Field devSupport = reactHost.getClass().getDeclaredField("mUseDevSupport"); |
| 182 | + Field devSupport = getCompatibleField(reactHost.getClass(), "useDevSupport"); |
151 | 183 | devSupport.setAccessible(true);
|
152 | 184 | devSupport.set(reactHost, false);
|
153 | 185 |
|
154 |
| - // Access the mReactHostDelegate field |
155 |
| - Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate"); |
| 186 | + // Access the ReactHostDelegate field (compatible with mReactHostDelegate/reactHostDelegate) |
| 187 | + Field reactHostDelegateField = getCompatibleField(reactHost.getClass(), "reactHostDelegate"); |
156 | 188 | reactHostDelegateField.setAccessible(true);
|
157 | 189 | Object reactHostDelegate = reactHostDelegateField.get(reactHost);
|
158 | 190 |
|
|
0 commit comments