Android端CodePush配置

Code Push

環境配置

  1. 安裝react-native-code-push : $ npm install --save react-native-code-push
  2. 安裝App Center CLI : $ npm install -g appcenter-cli

Android工程配置

CodePush 提供兩種配置方式,一種爲自動配置,一種爲手動配置:node

RNPM (自動配置)

$ react-native link react-native-code-push react

須要在RN項目的根目錄下,且Android原生項目也在此根目錄下android

手動配置

  1. 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')
  1. android/app/build.gradle文件中添加:react-native-code-push工程依賴
...
dependencies {
    ...
    compile project(':react-native-code-push')
}
  1. 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"
...
  1. 配置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端配置git

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

詳細可用配置參數可參考官方文檔github

發佈bundle文件

  1. 登陸AppCenter $ appcenter login
  2. 建立app(可直接在web端操做)$ appcenter apps create -d <appDisplayName> -o <operatingSystem> -p <platform>
  3. 發佈
//開發模式
appcenter codepush deployment add -a <ownerName>/<appName> Staging
//生產模式
appcenter codepush deployment add -a <ownerName>/<appName> Production

常見問題

已發佈升級包,可是檢測更新顯示已經是最新代碼

可能存在的幾種狀況以下:web

  • 檢查部署的key是否正確,是對應到Staging仍是Production的
  • 檢查android版本號是否正確,每一個發佈的bundle更新都會對應一個android的版本號,用於針對版本的更新

realease版本沒法正常顯示

檢查是否正確配置proguard文件,可參考官方配置shell

相關文章
相關標籤/搜索