1. 更改了java代码需要重新打包apk, 需要重新打包,下载apk 覆盖安装.
2. 只是更改了js代码逻辑, 不需要重新打包apk,需要用命令打包增量包,更新程序。
# 先全局安装命令行工具,每台电脑只用装一次
npm i -g react-native-update-cli
# 然后在项目目录中安装热更新模块
# 0.71 及以上版本使用最新版本如下
npm i react-native-update
# 0.71 以下版本请使用8.x版本
# npm i [email protected]
// ... 其它代码
// ↓↓↓请注意不要少了这句import
import cn.reactnative.modules.update.UpdateContext;
// ↑↑↑
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
// 老版本 RN 这里可能是 new ReactNativeHost(this)
new DefaultReactNativeHost(this) {
// ↓↓↓将下面这一段添加到 DefaultReactNativeHost 内部!
@Override
protected String getJSBundleFile() {
return UpdateContext.getBundleUrl(MainApplication.this);
}
// ↑↑↑
// ...其他代码
}
}
android 会在生成 apk 时自动对 png 图片进行压缩,此操作既耗时又影响增量补丁的生成。为了保证补丁能正常生成,您需要在android/app/build.gradle中关闭此操作:
...
android {
...
signingConfigs { ... }
buildTypes {
release {
...
// 添加下面这行以禁用crunch
crunchPngs false
}
}
}
...
puysh 文档 api 说明
import {isFirstTime,isRolledBack,packageVersion,currentVersion, checkUpdate,downloadUpdate,switchVersion,switchVersionLater,markSuccess,downloadAndInstallApk} from 'react-native-update';
downloadAndInstallApk({
url: updateInfo.downloadUrl,//下载apk的路径
onDownloadProgress:async ({ received, total }) => {
received // 已下载 received / 1024 / 1024 换算成兆;
total // 总大小
console.log(received, total, "==========================")
},
});
let info = {
update: true,
name: '1.1.0',
hash: 'hash',
description: '测试热更新',
metaInfo: '{"silent":true}',
pdiffUrl: '',
diffUrl: '',
updateUrl: versionUrl
}
const hash = await downloadUpdate(info, {
onDownloadProgress:async ({ received, total }) => {
console.log(received, total)
});
console.log(hash)
if (!hash) {
return;
}
Alert.alert('提示', '下载完毕,是否重启应用?', [
{
text: '是',
onPress: () => {
switchVersion(hash);
},
},
{ text: '否' },
{
text: '下次启动时',
onPress: () => {
switchVersionLater(hash);
},
},
]);
// 增量更新,判断是否是更新后的第一次重启,
if (isFirstTime) {
// 必须调用此更新成功标记方法
// 否则默认更新失败,下一次启动会自动回滚
markSuccess();
console.log('更新完成');
} else if (isRolledBack) {
console.log('刚刚更新失败了,版本被回滚.');
}
puysh 文档 命令行工具
pushy bundle --platform android //打包安卓的增量包,在.pushy/output/android.123123.ppk文件夹下