code push 热更新

安装 code-push-cli

1
npm install -g code-push-cli

注册 code-push 账号

1
code-push register

会打开一个网页,让你用microsoft 或 github 账号注册,
注册完会出现一个token,把该token粘贴到终端

登录 code-push

1
code-push login

会打开一个网页,让你登录,
登录完会出现一个token,把该token粘贴到终端

将app注册到code push

1
2
code-push app add 2048-ios ios react-native
code-push app add 2048-android android react-native

添加协同开发者

1
code-push collaborator add <appName> <collaboratorEmail>

安装 react-native-code-push

1
yarn add react-native-code-push

如果是通过 react-init 创建工程的,可以运行以下命令就可以了:

1
react-native link react-native-code-push

如果是现有项目集成 react native ,在Podfile添加

1
pod 'CodePush', :path => '../node_modules/react-native-code-push'

运行:

1
pod install

还要在info.plist中添加CodePushDeploymentKey字段(react-native link的不用这步),值就是上面生成的key,Staging和Production, 按需填写,如果想配置不同build不同key可以参考这个 链接

简单配置

多build配置
Project -> Info -> Configurations

Project -> Build Settings -> User-Defined -> CODEPUSH_KEY

Info.plist

这两处改成

1
$(BUILD_DIR)/Release$(EFFECTIVE_PLATFORM_NAME)


替换jsCodeLocation

1
2
3
4
5
6
7
NSURL *jsCodeLocation;

#ifdef DEBUG
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios&dev=true"];
#else
jsCodeLocation = [CodePush bundleURL];
#endif

到这配置基本完成,有问题到这个 链接 看看

code push 使用

在root Component 套上codePush, 这个配置会在app启动时检查更新并自动下载,下次启动时自动安装更新。如果是强制更新,则立即更新。

1
2
3
4
5
6
import codePush from "react-native-code-push";

class MyApp extends Component {
}

MyApp = codePush(MyApp);

如果想更频繁检查更新,使用如下配置

1
2
3
4
5
6
let codePushOptions = { checkFrequency: codePush.CheckFrequency.ON_APP_RESUME };

class MyApp extends Component {
}

MyApp = codePush(codePushOptions)(MyApp);

当然,也可以手动更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };

class MyApp extends Component {
onButtonPress() {
codePush.sync({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE
});
}

render() {
return (
<View>
<TouchableOpacity onPress={this.onButtonPress}>
<Text>Check for updates</Text>
</TouchableOpacity>
</View>
)
}
}

MyApp = codePush(codePushOptions)(MyApp);

code push 发布更新

1
code-push release-react <appName> <platform> [options]

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 添加描述
code-push release-react 2048-ios ios --description "add SafeAreaView"

# 指定plist路径和部署目标为Staging
code-push release-react 2048-ios ios -d Staging --description "add SafeAreaView" --plistFile ios/NumberTileGame/NumberTileGame-Info.plist

# 强制更新
code-push release-react MyApp-iOS ios -m --description "Modified the header color"

# 将开发中版本推送给25%的用户测试
code-push release-react MyApp-Android android --rollout 25% --dev true

# 推送更新给运行特定app版本的用户
# limiting the update to exact version name in the build.gradle file
code-push release-react MyApp-Android android --targetBinaryVersion "~1.1.0"

通过下面命令查看部署情况

1
code-push deployment ls 2048-ios -k

code-push 有很多可配置选项,具体可运行下列命令查看

1
code-push release-react -h

code push 支持差量更新,每次客户端只会下载改变的部分。code push 客户端有回滚机制,当你发布导致crash的更新,会自动回滚到上个版本

1
2
# 可以回滚服务端代码
code-push rollback

code push 动态更新deployment key

1
2
3
// Imagine that "userProfile" is a prop that this component received
// which includes the deployment key that the current user should use.
codePush.sync({ deploymentKey: userProfile.CODEPUSH_KEY });

添加 deployment

1
code-push deployment add 2048-ios test-variant-one

有这两个功能我们可以做一些比较有趣的事,比如进行 A/B Test
服务端随机给不同用户分发不同Deployment Key(KeyA, KeyB),比如50万用户是KeyA, 50万是KeyB。
现在有两个方案(版本)A、B,把方案A推送给KeyA的用户,方案B推送给KeyB的用户。结果A方案创造了10万收入,B方案创造了40万收入,显然B方案比较好。这样就可以把B方案推广给原来KeyA的用户。

参考链接