ReactNative 热更新 PushCode

React Native Client SDK入门

安装插件

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

Android

安装插件

  1. 在您的android/settings.gradle文件中,添加以下内容:
1
2
include ':app', ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
  1. android/app/build.gradle文件中,将文件添加codepush.gradle为下面的其他构建任务定义react.gradle
1
2
3
4
...
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
...
  1. MainApplication.java通过以下更改更新文件以使用CodePush:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
// 1. Import the plugin class.
import com.microsoft.codepush.react.CodePush;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
...
// 2. Override the getJSBundleFile method to let
// the CodePush runtime determine where to get the JS
// bundle location from on each app start
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
};
}
  1. 将部署密钥添加到strings.xml:

为了让CodePush运行时知道它应该查询哪些部署更新,请打开您的应用程序的strings.xml文件,并添加一个名为新的字符串CodePushDeploymentKey,要配置对这个应用程序(如为关键,它的值是部署的关键Staging部署的FooBar应用程序)。您可以通过appcenter codepush deployment list -a / -k在App Center CLI中运行来检索此值(该-k标志是必需的,因为默认情况下不会显示键),然后复制与Key您要使用的部署相对应的列的值(请参见下文)。使用部署名称(例如暂存)将不起作用。“友好名称”仅用于CLI中经过身份验证的管理用途,不适用于您的应用程序中的公共使用。

图片

为了有效利用与CodePush应用程序一起创建的StagingProduction部署,请在实际将您的应用程序对CodePush的使用移入生产之前,请参考下面的多部署测试文档。