$ npm install --save react-native-code-push
$ npm install -g appcenter-cli
CodePush 提供兩種配置方式,一種爲自動配置,一種爲手動配置:node
$ react-native link react-native-code-push
react
須要在RN項目的根目錄下,且Android原生項目也在此根目錄下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" ...
... // 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端配置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
$ appcenter login
$ 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
可能存在的幾種狀況以下:web
檢查是否正確配置proguard文件,可參考官方配置shell