From 78430e2ec24777290ea3fa1695d3677e3b123bf2 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Wed, 17 Sep 2025 16:36:15 +0800 Subject: [PATCH] support 0.81 android --- Example/testHotUpdate/bun.lock | 4 +- Example/testHotUpdate/package.json | 2 +- Example/testHotUpdate/src/index.tsx | 4 +- .../modules/update/UpdateModuleImpl.java | 40 +++++++++++++++++-- package.json | 2 +- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Example/testHotUpdate/bun.lock b/Example/testHotUpdate/bun.lock index 35f64bd..0e97e2b 100644 --- a/Example/testHotUpdate/bun.lock +++ b/Example/testHotUpdate/bun.lock @@ -12,7 +12,7 @@ "react-native-paper": "^5.14.5", "react-native-safe-area-context": "^5.6.1", "react-native-svg": "^15.13.0", - "react-native-update": "^10.31.0-beta.3", + "react-native-update": "^10.31.0-beta.4", "react-native-vector-icons": "^10.3.0", }, "devDependencies": { @@ -1430,7 +1430,7 @@ "react-native-svg": ["react-native-svg@15.13.0", "", { "dependencies": { "css-select": "^5.1.0", "css-tree": "^1.1.3", "warn-once": "0.1.1" }, "peerDependencies": { "react": "*", "react-native": "*" } }, "sha512-/YPK+PAAXg4T0x2d2vYPvqqAhOYid2bRKxUVT7STIyd1p2JxWmsGQkfZxXCkEFN7TwLfIyVlT5RimT91Pj/qXw=="], - "react-native-update": ["react-native-update@10.31.0-beta.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=="], + "react-native-update": ["react-native-update@10.31.0-beta.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=="], "react-native-url-polyfill": ["react-native-url-polyfill@2.0.0", "", { "dependencies": { "whatwg-url-without-unicode": "8.0.0-3" }, "peerDependencies": { "react-native": "*" } }, "sha512-My330Do7/DvKnEvwQc0WdcBnFPploYKp9CYlefDXzIdEaA+PAhDYllkvGeEroEzvc4Kzzj2O4yVdz8v6fjRvhA=="], diff --git a/Example/testHotUpdate/package.json b/Example/testHotUpdate/package.json index 202e32d..31d30c8 100644 --- a/Example/testHotUpdate/package.json +++ b/Example/testHotUpdate/package.json @@ -22,7 +22,7 @@ "react-native-paper": "^5.14.5", "react-native-safe-area-context": "^5.6.1", "react-native-svg": "^15.13.0", - "react-native-update": "^10.31.0-beta.3", + "react-native-update": "^10.31.0-beta.4", "react-native-vector-icons": "^10.3.0" }, "devDependencies": { diff --git a/Example/testHotUpdate/src/index.tsx b/Example/testHotUpdate/src/index.tsx index d499864..1b82419 100644 --- a/Example/testHotUpdate/src/index.tsx +++ b/Example/testHotUpdate/src/index.tsx @@ -40,7 +40,8 @@ function App() { packageVersion, currentHash, parseTestQrCode, - progress: {received, total} = {}, + progress: { received, total } = {}, + currentVersionInfo, } = useUpdate(); const [useDefaultAlert, setUseDefaultAlert] = useState(true); const [showTestConsole, setShowTestConsole] = useState(false); @@ -114,6 +115,7 @@ function App() { {'\n'} 当前热更新版本Hash: {currentHash || '(空)'} {'\n'} + 当前热更新版本信息: {JSON.stringify(currentVersionInfo) || '(空)'} 下载进度:{received} / {total} diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java index 349191f..338ac28 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java @@ -23,6 +23,38 @@ import java.util.Map; public class UpdateModuleImpl { public static final String NAME = "Pushy"; + + /** + * 获取字段的兼容性方法,尝试带m前缀和不带m前缀的字段名 + * @param clazz 目标类 + * @param fieldName 基础字段名(不带m前缀) + * @return 找到的字段对象 + * @throws NoSuchFieldException 如果两种命名都找不到字段 + */ + private static Field getCompatibleField(Class clazz, String fieldName) throws NoSuchFieldException { + // 首先尝试带m前缀的字段名 + try { + return clazz.getDeclaredField("m" + capitalize(fieldName)); + } catch (NoSuchFieldException e) { + // 如果找不到带m前缀的,尝试不带m前缀的 + 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); + } public static void downloadFullUpdate(UpdateContext updateContext, final ReadableMap options, final Promise promise) { String url = options.getString("updateUrl"); @@ -143,16 +175,16 @@ public class UpdateModuleImpl { ReactDelegate reactDelegate = (ReactDelegate) getReactDelegateMethod.invoke(currentActivity); - Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost"); + Field reactHostField = getCompatibleField(ReactDelegate.class, "reactHost"); reactHostField.setAccessible(true); Object reactHost = reactHostField.get(reactDelegate); - Field devSupport = reactHost.getClass().getDeclaredField("mUseDevSupport"); + Field devSupport = getCompatibleField(reactHost.getClass(), "useDevSupport"); devSupport.setAccessible(true); devSupport.set(reactHost, false); - // Access the mReactHostDelegate field - Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate"); + // Access the ReactHostDelegate field (compatible with mReactHostDelegate/reactHostDelegate) + Field reactHostDelegateField = getCompatibleField(reactHost.getClass(), "reactHostDelegate"); reactHostDelegateField.setAccessible(true); Object reactHostDelegate = reactHostDelegateField.get(reactHost); diff --git a/package.json b/package.json index d33f1a6..daf6924 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "10.31.0-beta.3", + "version": "10.31.0-beta.4", "description": "react-native hot update", "main": "src/index", "scripts": {