Skip to content

Commit 78430e2

Browse files
committed
support 0.81 android
1 parent ec5b9e1 commit 78430e2

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

Example/testHotUpdate/bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-native-paper": "^5.14.5",
1313
"react-native-safe-area-context": "^5.6.1",
1414
"react-native-svg": "^15.13.0",
15-
"react-native-update": "^10.31.0-beta.3",
15+
"react-native-update": "^10.31.0-beta.4",
1616
"react-native-vector-icons": "^10.3.0",
1717
},
1818
"devDependencies": {
@@ -1430,7 +1430,7 @@
14301430

14311431
"react-native-svg": ["[email protected]", "", { "dependencies": { "css-select": "^5.1.0", "css-tree": "^1.1.3", "warn-once": "0.1.1" }, "peerDependencies": { "react": "*", "react-native": "*" } }, "sha512-/YPK+PAAXg4T0x2d2vYPvqqAhOYid2bRKxUVT7STIyd1p2JxWmsGQkfZxXCkEFN7TwLfIyVlT5RimT91Pj/qXw=="],
14321432

1433-
"react-native-update": ["[email protected].3", "", { "dependencies": { "nanoid": "^3.3.3", "react-native-url-polyfill": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0", "react-native": ">=0.59.0" } }, "sha512-ApbK7pFY9E9zbpxKFdCUqYTmzwVhKjAdw6JysWRLZKCxRBwdhifN4vc8QGCKXF/WTQDRdKNX/3M0FKlMNnetQA=="],
1433+
"react-native-update": ["[email protected].4", "", { "dependencies": { "nanoid": "^3.3.3", "react-native-url-polyfill": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0", "react-native": ">=0.59.0" } }, "sha512-6TdSq9PdxH07IHFcMkhMaPxA81teike++PZtgVWbL1GJiZJ5MXpLhwsxD2Nd4x+eHJY8pj4wgMBC8iCAfqVJlg=="],
14341434

14351435
"react-native-url-polyfill": ["[email protected]", "", { "dependencies": { "whatwg-url-without-unicode": "8.0.0-3" }, "peerDependencies": { "react-native": "*" } }, "sha512-My330Do7/DvKnEvwQc0WdcBnFPploYKp9CYlefDXzIdEaA+PAhDYllkvGeEroEzvc4Kzzj2O4yVdz8v6fjRvhA=="],
14361436

Example/testHotUpdate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"react-native-paper": "^5.14.5",
2323
"react-native-safe-area-context": "^5.6.1",
2424
"react-native-svg": "^15.13.0",
25-
"react-native-update": "^10.31.0-beta.3",
25+
"react-native-update": "^10.31.0-beta.4",
2626
"react-native-vector-icons": "^10.3.0"
2727
},
2828
"devDependencies": {

Example/testHotUpdate/src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function App() {
4040
packageVersion,
4141
currentHash,
4242
parseTestQrCode,
43-
progress: {received, total} = {},
43+
progress: { received, total } = {},
44+
currentVersionInfo,
4445
} = useUpdate();
4546
const [useDefaultAlert, setUseDefaultAlert] = useState(true);
4647
const [showTestConsole, setShowTestConsole] = useState(false);
@@ -114,6 +115,7 @@ function App() {
114115
{'\n'}
115116
当前热更新版本Hash: {currentHash || '(空)'}
116117
{'\n'}
118+
当前热更新版本信息: {JSON.stringify(currentVersionInfo) || '(空)'}
117119
</Text>
118120
<Text>
119121
下载进度:{received} / {total}

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,38 @@
2323
public class UpdateModuleImpl {
2424

2525
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+
}
2658

2759
public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) {
2860
String url = options.getString("updateUrl");
@@ -143,16 +175,16 @@ public void run() {
143175
ReactDelegate reactDelegate = (ReactDelegate)
144176
getReactDelegateMethod.invoke(currentActivity);
145177

146-
Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost");
178+
Field reactHostField = getCompatibleField(ReactDelegate.class, "reactHost");
147179
reactHostField.setAccessible(true);
148180
Object reactHost = reactHostField.get(reactDelegate);
149181

150-
Field devSupport = reactHost.getClass().getDeclaredField("mUseDevSupport");
182+
Field devSupport = getCompatibleField(reactHost.getClass(), "useDevSupport");
151183
devSupport.setAccessible(true);
152184
devSupport.set(reactHost, false);
153185

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");
156188
reactHostDelegateField.setAccessible(true);
157189
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
158190

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.31.0-beta.3",
3+
"version": "10.31.0-beta.4",
44
"description": "react-native hot update",
55
"main": "src/index",
66
"scripts": {

0 commit comments

Comments
 (0)