CodePush 热更新

RN 热更新

Posted by Tan Lin on June 16, 2019

RN 项目热更新,目前已测试开发环境 (Staging),Production待更新

参考1 Medium-CodePush 参考2 MS-App-Center-Doc

特别注意: 版本号与config配置的版本后一直,不然已发布的apk不能正常升级到热更新的版本

Android

cd MyApp
npm install -g code-push-cli
// 完成 MS-App-Center 注册
code-push register

// ios命令: code-push app add MyApp ios react-native
code-push app add MyApp android react-native

npm install --save react-native-code-push

开始配置

自动配置命令: react-native link react-native-code-push 或者手动配置

// file1: android/app/build.gradle
...
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')
    ...
}

// file2: android/settings.gradle
rootProject.name = 'reactNative'
+include ':react-native-code-push'
+project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
...


// file3: android/gradle.properties
+APP_VERSION=0.1.3

// 这个版本号必须跟config配置的版本号一致,如果未指定,报错如下。发布新包的时候,这个不能变... 有点不理解
// code-push release-react MyApp android -d "Staging" -m --description "version update to 0.1.8"
// 会自动读取这个变量,如果指定了targetversion,可能不需要修改这个文件

发布命令: Staging/Production

code-push release-react AppWtfandroid -d “Staging” -m –description “version updated to 0.1.4”

错误1

[Error] No property named “APP_VERSION” exists in the “android/gradle.properties” file. 用户已安装的版本与需要热更新版本不一致 每次发布热更新,请确保 gradle.properties中的APP_VERSION始终保持与项目配置文件的版本号一致


IOS

先按照Android的步骤,完成注册

然后单独创建一个iOS的应用,因为js包可能因为系统不同而不完全一样,所以iOS与Android是两个应用

code-push app add AppWtfiOS ios react-native
开始配置 iOS

自动配置命令: react-native link react-native-code-push 或者手动配置

// file1: MyApp/ios/Podfile
target 'app' do
    ...
    pod 'CodePush', :path => '../node_modules/react-native-code-push'
end

// file2: ios/reactNative/Info.plist
// 'app key' 直接替换为 code-push deployment list MyApp -k 查看到的key
<plist version="1.0">
  <dict>
    ...
    <key>CFBundleShortVersionString</key>
    <string>app version, 需要修改为工程env的版本号0.1.3</string>
    +<key>CodePushDeploymentKey</key>
    +<string>app key</string>
  </dict>
</plist>

// file3: ios/reactNative/AppDelegate.m
- return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
+ return [CodePush bundleURL];

将libCodePush.a添加到library中 xCode打开项目 双击左侧项目试图下的Libraries子目录,Add Files to …,并选择 node_modules/react-native-code-push/ios/CodePush.xcodeproj Add CodePush to project

点击build phases,展开Link Binary With Libraries 将CodePush.xcodeproj项目里的libCodePush.a 拖进来即可,【注意】这里应该选Products目录下的libCodePush.a文件

Link CodePush during build

发布命令: Staging/Production

code-push release-react AppWtfiOS ios -d “Staging” -m –description “version updated to 0.1.4”

错误1

[CodePush] An update is available but it is not targeting the binary version of your app.

这个错误是因为用户已安装的版本与你热更新的版本号不匹配,queryPackage 应与 update 相同 https://github.com/Microsoft/react-native-code-push/issues/1485 在这个node_modules/react-native-code-push/CodePush.js添加如下代码检测

const update = await sdk.queryUpdateWithCurrentPackage(queryPackage); // 这一行的下方
console.log('[CodePush] queryPackage: ' + queryPackage.appVersion);
console.log('[CodePush] update: ' + update.appVersion);
...
错误2

no such file or directory Products/Debug-appletvsimulator/libCodePush.a

选择libCodePush.a这个库的时候选错了,应该选Products目录下的文件 需要按照Microsoft的引导文档操作, Drag libCodePush.a from Libraries/CodePush.xcodeproj/Products into the “Link Binary With Libraries” section of your project’s “Build Phases” configuration.

错误3

[Error] The “CFBundleShortVersionString” key in the “ios/reactNative/Info.plist” file needs to specify a valid semver string, containing both a major and minor version (e.g. 1.3.2, 1.1).

ios/reactNative/Info.plist 中的CFBundleShortVersionString值手动更新完为项目配置文件的APP_VERSION