CodePush是一个微软开发的云服务器。经过它,开发者能够直接在用户的设备上部署手机应用更新。CodePush至关于一个中心仓库,开发者能够推送当前的更新(包括JS/HTML/CSS/IMAGE等)到CoduPush,而后应用将会查询是否有更新。javascript
安装CodePush指令,直接在终端上输入以下命令便可,注意:这个CodePush指令只须要全局安装一次便可,若是第一次安装成功了,那后面就不在须要安装css
$ npm install -g code-push-cli
java
注册CodePush帐号也很简单,一样是只需简单的执行下面的命令,一样这个注册操做也是全局只须要注册一次便可react
$ code-push register
android
注意:当执行完上面的命令后,会自动打开一个受权网页,让你选择使用哪一种方式进行受权登陆,这里咱们统一就选择使用GitHub便可ios
当注册成功后,CodePush会给咱们一个keygit
咱们直接复制这个key,而后在终端中将这个key填写进去便可,填写key登陆成功显示效果以下github
咱们使用下面的命令来验证个人登陆是否成功npm
$ code-push login
react-native
CodePush注册登陆相关命令:
为了让CodePush服务器有咱们的App,咱们须要CodePush注册App,输入下面命令便可完成注册,这里须要注意若是咱们的应用分为iOS和Android两个平台,这时咱们须要分别注册两套key
应用添加成功后就会返回对应的production
和Staging
两个key,production
表明生产版的热更新部署,Staging
表明开发版的热更新部署,在ios中将staging的部署key复制在info.plist的CodePushDeploymentKey值中,在android中复制在Application的getPackages的CodePush中
添加iOS平台应用
$ code-push app add iOSRNHybrid ios react-native
添加Android平台应用
$ code-push app add iOSRNHybridForAndroid Android react-native
咱们能够输入以下命令来查看咱们刚刚添加的App
$ code-push app list
CodePush管理App的相关命令:
首先咱们须要安装CodeoPush组件,而后经过link命令添加原生依赖,最后在RN根组件中添加热更新逻辑代码
安装组件
$ npm install react-native-code-push --save
添加原生依赖,这里添加依赖咱们使用自动添加依赖的方式
$ react-native link react-native-code-push
咱们在RN项目的根组件中添加热更新逻辑代码以下
import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View } from 'react-native'; import CodePush from "react-native-code-push"; // 引入code-push let codePushOptions = { //设置检查更新的频率 //ON_APP_RESUME APP恢复到前台的时候 //ON_APP_START APP开启的时候 //MANUAL 手动检查 checkFrequency : CodePush.CheckFrequency.ON_APP_RESUME }; const instructions = Platform.select({ ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu', }); type Props = {}; class App extends Component<Props> { //若是有更新的提示 syncImmediate() { 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 : '更新提示' } , } , ); } componentWillMount() { CodePush.disallowRestart();//禁止重启 this.syncImmediate(); //开始检查更新 } componentDidMount() { CodePush.allowRestart();//在加载完了,容许重启 } render() { return ( <View style={styles.container}> <Text style={styles.welcome}> Welcome to React Native! </Text> <Text style={styles.instructions}> To get started, edit App.js </Text> <Text style={styles.instructions}> {instructions} </Text> <Text style={styles.instructions}> 这是更新的版本 </Text> </View> ); } } // 这一行必需要写 App = CodePush(codePushOptions)(App) export default App const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, })
这里原生应用中配置CodePush咱们须要分别配置iOS平台和Android平台
$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
改成:$(BUILD_DIR)/Release$(EFFECTIVE_PLATFORM_NAME)
+
号,选择Add User-Defined Setting
,将key设置为CODEPUSH_KEY
,Release 和 Staging的值为前面建立的key,咱们直接复制进去便可$(CODEPUSH_KEY)
,并修改Bundle versions为三位iOS平台CodePush环境集成完毕
在使用以前须要考虑的是检查更新时机,更新是否强制,更新是否要求即时等
通常常见的应用内更新时机分为两种,一种是打开App就检查更新,一种是放在设置界面让用户主动检查更新并安装
打开APP就检查更新
最为简单的使用方式在React Natvie的根组件的componentDidMount方法中经过
codePush.sync()(须要先导入codePush包:import codePush from 'react-native-code-push')方法检查并安装更新,若是有更新包可供下载则会在重启后生效。不过这种下载和安装都是静默的,即用户不可见。若是须要用户可见则须要额外的配置。具体能够参考codePush官方API文档,部分代码,完整代码请参照文档上面
codePush.sync({ updateDialog: { appendReleaseDescription: true, descriptionPrefix:'\n\n更新内容:\n', title:'更新', mandatoryUpdateMessage:'', mandatoryContinueButtonLabel:'更新', }, mandatoryInstallMode:codePush.InstallMode.IMMEDIATE, deploymentKey: CODE_PUSH_PRODUCTION_KEY, });
上面的配置在检查更新时会弹出提示对话框, mandatoryInstallMode表示强制更新,appendReleaseDescription表示在发布更新时的描述会显示到更新对话框上让用户可见
用户点击检查更新按钮
在用户点击检查更新按钮后进行检查,若是有更新则弹出提示框让用户选择是否更新,若是用户点击当即更新按钮,则会进行安装包的下载(实际上这时候应该显示下载进度,这里省略了)下载完成后会当即重启并生效(也可配置稍后重启),部分代码以下
codePush.checkForUpdate(deploymentKey).then((update) => { if (!update) { Alert.alert("提示", "已经是最新版本--", [ { text: "Ok", onPress: () => { console.log("点了OK"); } } ]); } else { codePush.sync({ deploymentKey: deploymentKey, updateDialog: { optionalIgnoreButtonLabel: '稍后', optionalInstallButtonLabel: '当即更新', optionalUpdateMessage: '有新版本了,是否更新?', title: '更新提示' }, installMode: codePush.InstallMode.IMMEDIATE, }, (status) => { switch (status) { case codePush.SyncStatus.DOWNLOADING_PACKAGE: console.log("DOWNLOADING_PACKAGE"); break; case codePush.SyncStatus.INSTALLING_UPDATE: console.log(" INSTALLING_UPDATE"); break; } }, (progress) => { console.log(progress.receivedBytes + " of " + progress.totalBytes + " received."); } ); } }
若是是强制更新须要在发布的时候指定,发布命令中配置--m true
在更新配置中经过指定installMode来决定安装完成的重启时机,亦即更新生效时机
在将RN的bundle放到CodePush服务器以前,咱们须要先生成bundle,在将bundle上传到CodePush
生成bundle
react-native bundle --platform 平台 --entry-file 启动文件 --bundle-output 打包js输出文件 --assets-dest 资源输出目录 --dev 是否调试
$ react-native bundle --entry-file index.ios.js --bundle-output ./bundle/ios/main.jsbundle --platform ios --assets-dest ./bundle/ios --dev false
上传bundle
$ code-push release-react <Appname> <Platform> --t <本更新包面向的旧版本号> --des <本次更新说明>
注意: CodePush默认是更新Staging 环境的,若是发布生产环境的更新包,须要指定--d参数:--d Production,若是发布的是强制更新包,须要加上 --m true强制更新
$ code-push release-react iOSRNHybrid ios --t 1.0.0 --dev false --d Production --des "这是第一个更新包" --m true
更新包上传到CodePush服务器成功后,效果图以下:
查看发布的历史记录,命令以下
查询Production
$ code-push deployment history projectName Production
查询Staging
$ code-push deployment history projectName Staging
对1.0.0版本的应用如何发布第二个、第n个更新包
操做步骤和上面发布第一个更新包流程同样,咱们任然先须要打出bundle包,将生成的bundle文件和资源文件拖到工程中,而后再将bundle发布到CodePush
$ react-native bundle --entry-file index.ios.js --bundle-output ./bundle/ios/main.jsbundle --platform ios --assets-dest ./bundle/ios --dev false
$ code-push release-react iOSRNHybrid ios --t 1.0.0 --dev false --d Production --des "这是第二个更新包" --m true
bundle -> ios
,打bundle命令以下:$ react-native bundle --entry-file index.ios.js --bundle-output ./bundle/ios/main.jsbundle --platform ios --assets-dest ./bundle/ios --dev false
发布更新包命令中的 -- t
对应的参数是和咱们项目中的版本号一致的,这个不要误理解为是更新包的版本号,例如项目中的版本号为1.0.0
, 这时若是咱们须要对这个1.0.0
版本的项目进行第一次热更新,那么命令中的 -- t
也为1.0.0
,第二次热更新任然为1.0.0
项目的版本号须要改成三位的,默认是两位的,可是CodePush须要三位数的版本号
发布更新应用时,应用的名称必需要和以前注册过的应用名称一致