热更新客户端配置

参考文档

https://www.jianshu.com/p/ca795f03e12d
最新
https://segmentfault.com/a/1190000018003425

  • 前期准备
  • 配置Android
  • 配置IOS
  • 配置RN
  • 发布命令

前期准备

  1. 项目中安装react-native-code-push
npm install --save react-native-code-push@latest
react-native link react-native-code-push

link时会提示:

What is your CodePush deployment key for Android (hit <ENTER> to ignore)
What is your CodePush deployment key for iOS (hit <ENTER> to ignore)

一直Enter即可,后边会配置。

  1. 安装code-push-cli
npm install -g code-push-cli
  1. 登陆code-push
code-push login http://127.0.0.1:3000
  1. 注册应用
  • ios
code-push add app test02-ios ios react-native
Name Deployment Key
Production xHyO39jpKtdBj2p6kuyz4w8Mv0lG4ksvOXqog
Staging GTLSvDtcYqkGhLweJ9allwTa0zQB4ksvOXqog
  • android
code-push add app test02-android android react-native
Name Deployment Key
Production sKhqyi4Tc4ZxlGHhhZrrr29b0JoH4ksvOXqog
Staging kw8iMmcSgtY3lpnW0yzQ0a1THK0Y4ksvOXqog

Production对应生产包,其中Staging对应测试包。

  1. 查看是否添加成功
code-push app ls
Name Deployments
test02-ios Production, Staging
test02-android Production, Staging
  1. 查看key
code-push deployment ls test02-ios -k
Name Deployment Key Update Metadata Install Metrics
Production eSJ9SxnaI5lYHkj6I9o90Z317AeL4ksvOXqog No updates released No installs reco
Staging r4SNF3BL4vkgP4UK2gMWx7Iibh4S4ksvOXqog No updates released No installs reco
  1. 查看其他命令

code-push --h


配置Android

  1. 修改android-buildTypes节点,在android/app/build.gradle
buildTypes {
    debug {
        //这行没有的话本地运行会报错,提示[CODEPUSH_KEY]不存在,因为默认[react-native run-android]运行的是debug版本
        buildConfigField "String", "CODEPUSH_KEY", '""'
    }
    releaseStaging {
        ...
        buildConfigField "String", "CODEPUSH_KEY", '"JhGbiijRJf4miIUqBSPib1qux7G34ksvOXqog"'
    }
    release {
        ...
        buildConfigField "String", "CODEPUSH_KEY", '"HwMz0dkX2EOiybqt02JvajcT1FA64ksvOXqog"'
    }
}
  1. 修改getPackages()方法,在android/app/src/main/java/com/codepushclient(项目名)/MainApplication.java
//最后一个参数为codepush服务器地址
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      ...
      new CodePush(BuildConfig.CODEPUSH_KEY, MainApplication.this, BuildConfig.DEBUG, "http://127.0.0.1:3000")
  );
}
  1. 修改VersionName,在android/app/build.gradle,android-defaultConfig节点,将版本号改成三位
defaultConfig {
    ...
    versionName "1.0.0"
    ...
}

在模拟器上安装对应版本包

下边的命令除了第一个(默认debug安装),剩余两个都需要对安卓文件进行配置才可以,具体配置方法可以参考我的这篇文章安卓打包APK

  • cd android && ./gradlew installDebugdebug版
  • cd android && ./gradlew installReleaseStaging测试版
  • cd android && ./gradlew installRelease正式版
    安装不同版本时,如果提示错误com.codepushclient signatures do not match the previously installed version,将模拟器app卸载后再安装
  • cd android && ./gradlew assembleRelease打包

配置IOS

  1. xcode打开项目Project中选中项目->选择Info标签->选择Configurations节点下的+,选择选择Duplicate "Release" Configaration输入Staging
    Info
  2. 选择BuildSettings标签->点击+选择Add User-Defined Setting->输入CODEPUSH_KEY (可随意)->填写deployment key
    Build Settings
  3. 修改Info.plist文件
  • CodePushDeploymentKey中输入$(CODEPUSH_KEY)
  • 修改Bundle versions版本号为三位
  • 配置CodePushServerURL输入热更新服务器地址
    Info.plist
  1. 如果服务器是http,需要设置Allow Arbitary LoadsYES
    image.png

配置RN

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  Button,
  Alert,
} from 'react-native';
import codePush from 'react-native-code-push';
import config from './config';

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> {
  componentDidMount() {
    codePush.notifyAppReady()
  }
  checkUpdate=()=>{
    codePush.checkForUpdate(config.deploymentKey).then((update)=>{
      if(!update||update.failedInstall){
        Alert.alert("提示","已是最新版本",[
          {
            text:'ok',
            onPress:()=>{
              console.log('click');
            }
          }
        ]);
      }else{
        codePush.sync(
          {
            deploymentKey: config.deploymentKey,
            updateDialog: {
              optionalIgnoreButtonLabel: '稍后',
              optionalInstallButtonLabel: '立即更新',
              optionalUpdateMessage: '有新版本了,是否更新?',
              title: '更新提示'
            },
            installMode: codePush.InstallMode.IMMEDIATE,
          },
          (status)=>{
            switch (status) {
              case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                console.log("DOWNLOADING_PACKAGE");
                // Alert.alert('DOWNLOADING_PACKAGE')
                break;
              case codePush.SyncStatus.INSTALLING_UPDATE:
                console.log(" INSTALLING_UPDATE");
                // Alert.alert('INSTALLING_UPDATE')
                break;
              case codePush.SyncStatus.UP_TO_DATE:
                // Alert.alert('UP_TO_DATE')
                break;
              case codePush.SyncStatus.UPDATE_INSTALLED:
                // Alert.alert('UPDATE_INSTALLED')
                break;
              case codePush.SyncStatus.UPDATE_IGNORED:
                // Alert.alert('UPDATE_IGNORED')
                break;
              case codePush.SyncStatus.UNKNOWN_ERROR:
                // Alert.alert('UNKNOWN_ERROR')
                break;
              case codePush.SyncStatus.SYNC_IN_PROGRESS:
                // Alert.alert('SYNC_IN_PROGRESS')
                break;
              case codePush.SyncStatus.CHECKING_FOR_UPDATE:
                // Alert.alert('CHECKING_FOR_UPDATE')
                break;
              case codePush.SyncStatus.AWAITING_USER_ACTION:
                // Alert.alert('AWAITING_USER_ACTION')
                break;
              case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                // Alert.alert('DOWNLOADING_PACKAGE')
                break;
              case codePush.SyncStatus.INSTALLING_UPDATE:
                // Alert.alert('INSTALLING_UPDATE')
                break;
            }
          },
          (progress) => {
            console.log(progress.receivedBytes + " of " + progress.totalBytes + " received.");
          }
        )
      }
    })
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          12
        </Text>
        <Button 
          title="检查更新"
          onPress={this.checkUpdate}
        />
      </View>
    );
  }
}

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,
  },
});
export default App;
//config.js
import {
  Platform
} from 'react-native';
const isDev=true;
let deploymentKey = '';
if(isDev){
  deploymentKey = Platform.OS === 'ios'?'r4SNF3BL4vkgP4UK2gMWx7Iibh4S4ksvOXqog':'JhGbiijRJf4miIUqBSPib1qux7G34ksvOXqog';
}else{
  deploymentKey = Platform.OS === 'ios'?'eSJ9SxnaI5lYHkj6I9o90Z317AeL4ksvOXqog':'HwMz0dkX2EOiybqt02JvajcT1FA64ksvOXqog';
}
const config = {
  deploymentKey,
}
export default config;

发布命令

  1. 更新测试包(默认更新的是Staging环境)
//更新ios
code-push release-react test02-ios ios
//更新android
code-push release-react test02-android android
  1. 更新正式包
//更新ios
code-push release-react test02-ios ios -d Production
//更新android
code-push release-react test02-android android -d Production

codePush配置

codePush.sync({
  updateDialog: {
    optionalIgnoreButtonLabel: '稍后',
    optionalInstallButtonLabel: '立即更新',
    optionalUpdateMessage: '有新版本了,是否更新?',
    title: '更新提示'
  },
  installMode: codePush.InstallMode.IMMEDIATE,
})
  • codePush.InstallMode.IMMEDIATE(表示安装完成立即重启更新)
  • codePush.InstallMode.ON_NEXT_RESTART(表示安装完成后会在下次重启后进行更新)
  • codePush.InstallMode.ON_NEXT_RESUME(表示安装完成后会在应用进入后台后重启更新)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容