React Native 实现热更新并自动签名打包功能
项目背景:手动link的安卓app
1.下载 react-native-code-push
npm install --save react-native-code-push
2.在android/settings.gradle文件下新增:
include ':app', ':react-native-code-push'
project(':react-native-code-push').projectdir = new file(rootproject.projectdir, '../node_modules/react-native-code-push/android/app')
3.在android\app\src\main\java\com\app\mainapplication.java
文件中修改
... // 1. import the plugin class. import com.microsoft.codepush.react.codepush; public class mainapplication extends application implements reactapplication { private final reactnativehost mreactnativehost = new reactnativehost(this) { ... // 2. override the getjsbundlefile method in order to let // the codepush runtime determine where to get the js // bundle location from on each app start @override protected string getjsbundlefile() { return codepush.getjsbundlefile(); } //手动link需要修改的地方,自动link应该不需要改 @override protected list<reactpackage> getpackages() { @suppresswarnings("unnecessarylocalvariable") list<reactpackage> packages = new packagelist(this).getpackages(); // packages that cannot be autolinked yet can be added manually here, for // example: // packages.add(new myreactnativepackage()); packages.add(new codepush(getresources().getstring(r.string.codepushdeploymentkey), getapplicationcontext(), buildconfig.debug, getresources().getstring(r.string.reactnativecodepush_androidserverurl))); return packages; } }; } //codepushdeploymentkey对应string.xml里面的 deployment key的name //reactnativecodepush_androidserverurl对应string.xml里面热更新服务地址的name
4.string.xml的修改:首先要将你的app添加到推送中心,并获取你需要的环境分支的key
4.1.登录热更新服务器
4.2.推送中心创建项目:(针对第一次部署)
code-push app add 项目名称 android react-native
4.3 添加环境分支dev:code-push deployment add 项目名称 dev (针对第一次部署)
将项目打包至对应的环境分支,需要将环境分支对应的key和热更新地址配置到项目文件中:(strings.xml)
4.4 准备工作已经做好了,现在我们来修改string.xml文件吧
... apply from: "../../node_modules/react-native/react.gradle" apply from: "../../node_modules/react-native-code-push/android/codepush.gradle" ... dependencies { implementation project(':react-native-code-push') //最好手动加上,否则可能会有坑 implementation filetree(dir: "libs", include: ["*.jar"]) .... } ....
5.修改android/app/build.gradle文件
stack trace and/or screenshots :app:compiledebugjavawithjavac - is not incremental (e.g. outputs have changed, no previous execution, etc.). c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:6: error: package com.microsoft.codepush.react does not exist import com.microsoft.codepush.react.codepush; ^ c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:23: error: cannot find symbol return codepush.getjsbundlefile(); ^ symbol: variable codepush c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:35: error: cannot find symbol new codepush(null, getapplicationcontext(), buildconfig.debug), ^ symbol: class codepush 3 errors :app:compiledebugjavawithjavac failed failure: build failed with an exception. * what went wrong: execution failed for task ':app:compiledebugjavawithjavac'. > compilation failed; see the compiler error output for details.
采坑:react-native-code-push需要手动添加依赖,否则会报错:
stack trace and/or screenshots
:app:compiledebugjavawithjavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:6: error: package com.microsoft.codepush.react does not exist
import com.microsoft.codepush.react.codepush;
^
c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:23: error: cannot find symbol
return codepush.getjsbundlefile();
^
symbol: variable codepush
c:\stock api\stock_app\android\app\src\main\java\com\stock_app\mainapplication.java:35: error: cannot find symbol
new codepush(null, getapplicationcontext(), buildconfig.debug),
^
symbol: class codepush
3 errors
:app:compiledebugjavawithjavac failed
failure: build failed with an exception.
* what went wrong:
execution failed for task ':app:compiledebugjavawithjavac'.
> compilation failed; see the compiler error output for details.
6.采坑:手动link react-native-code-push的app,需要禁止autolink,否则会报错:
java.lang.illegalstateexception: native module codepush tried to override codepushnativemodule. check the getpackages() method in mainapplication.java, it might be that module is being created twice. if this was your intention, set canoverrideexistingmodule=true
所以需要加上一个配置文件,禁止自动link
在项目根目录创建react-native.config.js文件
module.exports = { dependencies: { 'react-native-code-push': { platforms: { android: null, // disable android platform, other platforms will still autolink }, }, }, };
7.热更新配置完成,现在我们需要在项目启动的时候检测热更新,并提示
在项目入口文件app.js中:
import react,{ component } from 'react'; import root from './src/inner'; import configurestore from './src/inner/store'; import updatedialog from './src/common/components/updatedialog' import codepush from "react-native-code-push"; const { persistor, store } = configurestore(); class app extends component { state = { visitdialog: false, current: 0, total: 100 } componentdidmount() { codepush.sync({ //安装模式 //on_next_resume 下次恢复到前台时 //on_next_restart 下一次重启时 //immediate 马上更新 installmode: codepush.installmode.immediate, //对话框 updatedialog: { //是否显示更新描述 appendreleasedescription: true, //更新描述的前缀。 默认为"description" descriptionprefix: "更新内容:", //强制更新按钮文字,默认为continue mandatorycontinuebuttonlabel: "立即更新", //强制更新时的信息. 默认为"an update is available that must be installed." mandatoryupdatemessage: "必须更新后才能使用", //非强制更新时,按钮文字,默认为"ignore" optionalignorebuttonlabel: '稍后', //非强制更新时,确认按钮文字. 默认为"install" optionalinstallbuttonlabel: '后台更新', //非强制更新时,检查到更新的消息文本 optionalupdatemessage: '有新版本了,是否更新?', //alert窗口的标题 title: '更新提示' }, }, (status) => { console.log(status, 'status') if (status == 7) { this.setstate({ visitdialog: true }) } }, (progress) => { let receivedbytes = progress.receivedbytes / 1024 / 1024; let totalbytes = progress.totalbytes / 1024 / 1024; this.setstate({ current: receivedbytes, total: totalbytes }) if (receivedbytes === totalbytes) { settimeout(() => { this.setstate({ visitdialog: false }) }, 1000) } console.log(progress, 'progress') } ); } render() { console.log(this.state.visitdialog, 'visitdialog'); return ( <> <root store={store} persistor={persistor} /> {this.state.visitdialog && <updatedialog title={this.state.current === this.state.total ? '已完成' : '正在下载更新文件'} describe={this.state.current === this.state.total ? '欢迎使用' : '请耐心等待'} current={this.state.current} total={this.state.total}></updatedialog>} </> ) } }; let codepushoptions = { //设置检查更新的频率 //on_app_resume app恢复到前台的时候 //on_app_start app开启的时候 //manual 手动检查 checkfrequency: codepush.checkfrequency.on_app_start }; export default codepush(codepushoptions)(app);
updatedialog :是我自己封装的热更新下载进度条的组件,下载提示,可根据自己的心情随便写,这里我就不贴自己的代码了!(写的不好,不好意思)
现在我们热更新配置好了,打包正式的apk吧!
1. 生成签名文件:在项目根目录下运行命令:
keytool -genkey -v -keystore 我的签名-key.jks -keyalg rsa -keysize 2048 -validity 10000 -alias 我的签名
2.将生成的签名文件拷贝至目录:android/app目录下
3.配置gradle.properties
android.useandroidx=true android.enablejetifier=true myapp_release_store_file=wms-app-key.jks //生成的签名密钥 myapp_release_key_alias=ydh //别名 myapp_release_store_password=签名时设置的密码 myapp_release_key_password=签名时设置的密码
4.修改android/app/build.gradle
signingconfigs { debug { ... } release { if (project.hasproperty('myapp_release_store_file')) { storefile file(myapp_release_store_file) storepassword myapp_release_store_password keyalias myapp_release_key_alias keypassword myapp_release_key_password } } }
5..打包(android目录下): .\gradlew.bat assemblerelease
app打包成功,将apk拷贝到手机安装即可
6..推送代码:(需要更新时,推送代码到你想要更新的环境分支)
推送到dev环境:code-push release-react 项目名称 android -d dev
推送到production环境:-m true 代表强制更新,不加代表不强制更新
code-push release-react 项目名称 android -d production -m true
然后重启app,就可以看到更新提示啦
总结
到此这篇关于react native 实现热更新并自动签名打包的文章就介绍到这了,更多相关react native签名打包内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 白了少年头竟是身体缺少这些营养素