Code Push
环境配置
- 安装react-native-code-push :
$ npm install --save react-native-code-push
- 安装App Center CLI :
$ npm install -g appcenter-cli
Android工程配置
CodePush 提供两种配置方式,一种为自动配置,一种为手动配置:
RNPM (自动配置)
$ react-native link react-native-code-push
需要在RN项目的根目录下,且Android原生项目也在此根目录下
手动配置
- 在
android/settings.gradle
文件中配置react-native-code-push
工程
include ':app', ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
- 在
android/app/build.gradle
文件中添加:react-native-code-push
工程依赖
...
dependencies {
...
compile project(':react-native-code-push')
}
- 在
android/app/build.gradle
文件中添加codepush.gradle
插件
...
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
...
- 配置Application
...
// 1. Import the plugin class.
import com.microsoft.codepush.react.CodePush;
public class MainApplication extends Application {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
...
// 2. Override the getJSBundleFile method in order 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();
}
@Override
protected List<ReactPackage> getPackages() {
// 3. Instantiate an instance of the CodePush runtime and add it to the list of
// existing packages, specifying the right deployment key. If you don't already
// have it, you can run "appcenter codepush deployment list -a <ownerName>/<appName>" to retrieve your key.
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new CodePush("deployment-key-here", MainApplication.this, BuildConfig.DEBUG)
);
}
};
}
Js配置
检查更新的代码需在js端配置
import CodePush from "react-native-code-push";
class App extends Component {
...
}
App = CodePush({
installMode: CodePush.InstallMode.ON_NEXT_SUSPEND, //app进入后台模式时更新
checkFrequency: CodePush.CheckFrequency.ON_APP_RESUME //每次进入页面时检查是否有更新可用
})(App);
export default App
详细可用配置参数可参考官方文档
发布bundle文件
- 登录AppCenter
$ appcenter login
- 创建app(可直接在web端操作)
$ appcenter apps create -d <appDisplayName> -o <operatingSystem> -p <platform>
- 发布
//开发模式
appcenter codepush deployment add -a <ownerName>/<appName> Staging
//生产模式
appcenter codepush deployment add -a <ownerName>/<appName> Production
常见问题
已发布升级包,但是检测更新显示已是最新代码
可能存在的几种情况如下:
- 检查部署的key是否正确,是对应到Staging还是Production的
- 检查android版本号是否正确,每个发布的bundle更新都会对应一个android的版本号,用于针对版本的更新
realease版本无法正常显示
检查是否正确配置proguard文件,可参考官方配置