CodePush配置

来源:互联网 发布:ug编程二次开粗技巧 编辑:程序博客网 时间:2024/05/20 06:22

Code Push 配置与使用

不同平台下, 对于软件热更新的方式有不同的要求

  • ios: 要求不能弹框, 软件下次启动时更新
  • android: 要求以弹框方式告知用户后加载更新

对于以上两种要求, 分别使用以下两种方式进行代码热更新

  • ios: 在入口js生命周期DidMount中写入
codePush.sync();
  • android: 在入口js生命周期函数DidMount中写入
codePush.sync({    updateDialog: true,    installMode: codePush.InstallMode.IMMEDIATE});

一.安装与注册以及cli常用命令

  • 安装CodePush cli
npm install -g code-push-cli
  • 检测安装
code-push -v
  • 注册codepush, 推荐使用github账号

输入以下命令, 自动打开网址, 选择注册账号

code-push register
  • 查看账户
code-push whoami
  • 登入
codepush login
  • 登出
codepush logout
  • 注册App
codepush app add APPNAME
  • 列出已登录token
code-push access-key ls
  • 删除某个access-key
code-push access-key rm KEY
  • 为团队中添加成员
code-push collaborator add APPNAME TEAMMEMBER@EMAIL.COM

二.团队子成员权限限制

  • 团队子成员不包含的权限:
  • 重命名或删除App;
  • 更改App所有者;
  • 创建,重命名,删除除了App在外的新部署
  • 删除一个历史版本部署
  • 添加或删除其他的子成员

三.安卓配置以及打包部署

  • 1).在android/settings.gradle添加
include ':react-native-code-push'project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
  • 2).在android/app/build.gradle下的dependencies加上依赖等参数
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"// ...compile project(':react-native-code-push')
  • 3).生成一个部署
codepush app add APPNAME && code-push deployment add AppName DeploymentName
  • 4).获取部署的App的密匙, 终端输入
code-push deployment ls AppName -k
  • 5).在MainApplication.java中将getJSBundleFile改写为以下
@Overrideprotected String getJSBundleFile() {    return CodePush.getJSBundleFile();}
  • 6).在MainAPplication.java中加入类
import com.microsoft.codepush.react.CodePush;...new CodePush("4处获取的key", MainApplication.this, BuildConfig.DEBUG)
  • 7).在android/app/build.gradle下更改versionName版本号

改为1.0.0 (支持三位以上版本号, 默认的是两位), 如果要更改核心代码, (java/c)等代码, 则需要更新版本号, 进行大的更新

  • 8).在js入口文件, index.android.js下的DidMount回调函数中加入
codePush.sync({    updateDialog: true,    installMode: codePush.InstallMode.IMMEDIATE});
  • 9).打包js文件

可用code-push的release-react来打包:

  code-push release-react <appName> <platform>  code-push release-react MyApp-iOS ios  code-push release-react MyApp-Android android

[ ]中为可选选项, –dev true 开启调试, false 关闭调试

react-native bundle --platform [ios||android] --entry-file [index.ios.jd||index.android.js] --bundle-output [./bundles/index.android.bundle||./bundles/index.ios.bundle] --assets-dest ./bundles --dev [false||true] 

举例: react-native bundle –platform android –entry-file index.android.js –bundle-output ./bundles/index.android.bundle –assets-dest ./bundles –dev false
ios版: react-native bundle –platform ios –entry-file index.ios.js –bundle-output ./bundles/index.ios.bundle –assets-dest ./bundles –dev false

  • 10).打包好后, 用codepush发布新的版本
code-push release [应用名] [Bundles目录] [应用版本] --deploymentName --description 更新描述 --mandatory 是否强制更新

举例: code-push release todos ./bundles/ 1.0.1 –deploymentName Production –description “测试一下更新” –mandatory true

版本号必须和应用的版本号保持一致, 如1.0.0则只有1.0.0的版本会收到更新提示, 即每次更新是小更新, 到了大的更新, 就会更换版本号同时更新deployment key, 就到了下一个版本的部署

  • 11).生成密匙与证书
keytool -genkey -v -keystore [取名].keystore -alias [别名] -keyalg RSA -keysize 2048 -validity 10000 

注意事项 store密码和key密码要一样

生成后将xxx.keystore文件复制到android/app下, 在用户目录下的.gradle文件夹下新建gradle.properties并写入全局变量

// 用户目录: windows: C:/User/UserName/.gradle Mac: /User/UserName/.gradleMYAPP_RELEASE_STORE_FILE=my-test-key.keystoreMYAPP_RELEASE_KEY_ALIAS=shinepansMYAPP_RELEASE_STORE_PASSWORD=shinepansMYAPP_RELEASE_KEY_PASSWORD=shinepans  
  • 12).保存bundle文件(确保打包服务器在运行且可连接)
curl -k "http://localhost:8081/index.android.bundle" > android/app/src/main/assets/index.android.bundle 
  • 13).在android/app/build.gradle中配置
signingConfigs {    release {        storeFile file(MYAPP_RELEASE_STORE_FILE)          storePassword MYAPP_RELEASE_STORE_PASSWORD          keyAlias MYAPP_RELEASE_KEY_ALIAS          keyPassword MYAPP_RELEASE_KEY_PASSWORD      }}
  • 14).代码混淆与压缩 在build.gradle中配置以下代码
def enableProguardInReleaseBuilds = true
  • 15).打包签名apk

到安卓目录下, 运行命令 (ancademy-app/android)

gradlew assembleRelease
  • 16).安装apk

到安卓目录下, 运行命令 (ancademy-app/android)

gradlew installRelease

四.IOS配置以及打包部署

类似于三, 原生代码配置TODO

五.关于自建服务器的使用(CodePushServer)

由于微软服务器延迟高, 速度慢, 容易丢包, 故搭建自己的服务器来配合使用codepush, 以下是搭建步骤.

  • 1).安装mysql并配置, 开启mysql, 然后安装code-push-server

    安装

    $ git clone git@github.com:lisong/code-push-server.git$ cd code-push-server$ npm install

    初始化数据库(将提示success), 在code-push-server项目目录下运行

     node ./bin/www init --dbhost localhost --dbuser root --dbpassword

    修改配置文件, 在全局安装目录下: /config/config.js

    db: {    username: "root",    password: "shine",    database: "codepush",    host: "127.0.0.1",    port: 3306,    dialect: "mysql"},

    修改数据库文件路径, 在/config/config.js

    storageType: 选择存储类型,支持local和qiniu配置

    local: {    storageDir: "/Users/tablee/workspaces/storage",    downloadUrl: "http://127.0.0.1:3000/download"},common: {    dataDir: "/Users/luozhong/workspaces/data",    storageType: "local"   },

    启动服务, 在 code-push-server下

    node ./bin/www #启动服务 浏览器中打开 http://127.0.0.1:3000
  • 2).与code-push建立连接

    登出code-push

    code-push logout

    登入自建服务器

    code-push login http://localhost:3000

    使用密码登录网页获取token:

    admin 123456

    登录后点击获取token, 样例: i5jwA5iowVC3KIks8xUmSgBtlSYb4ksvOXqog

    将token复制到terminal, 回车, 完成登录, 到此, code-push已经与自己的服务器建立联系

    最后, 在MainApplication.java文件下降CodePush类加上如下参数

    new CodePush(    "dvtyLiNt04zqK7jr3UgKRQSexC8DNkHScFbif",     MainApplication.this,     BuildConfig.DEBUG,    "http://127.0.0.1:3000/")

    第一项 参数是步骤三种新建的部署的DeploymentKey

    第二项 参数是指针

    第三项 参数是配置信息

    第四项 参数是code-push-server的本地服务器

  • 3).接下来继续三步骤中的打包与发布, 步骤相同

TestDone

原创粉丝点击