1. 程式人生 > >codepush熱更新

codepush熱更新

初始化階段:

1:npm install -g code-push-cli 安裝客戶端

2:code-push -v 檢視是否安裝成功

3:code-push register 在codepush註冊賬號 

4:code-push login 

5:code-push app add <appName> <android/ios> react-native 新增app

例如code-push app add test android react-native 

6:code-push app list 列出app列表

code-push deployment  ls <appName> -k 

檢視APP的key

code-push deployment history <appName> Porduction/Staging

例如:code-push deployment history test Production

7:yarn add react-native-code-push 在rn專案下安裝codepush

8:react-native link react-native-code-push 連結codepush

其他命令:

配置階段

Android:

1:專案settings.build

include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')

2:App/build.gradle




3:MainApplication裡面

@Nullable
@Override
protected String getJSBundleFile() {
  return CodePush.getJSBundleFile();
}

@Override
public boolean getUseDeveloperSupport() {
  return BuildConfig.DEBUG;
}

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
          new CodePush(“CODEPUSH_KEY”, MainApplication.this, BuildConfig.DEBUG)

  );
}


ios配置階段:

yarn add react-native-code-push

react-native link react-native-code-push

然後找到info.plist檔案 

CodePushDeploymentKey 填入codekey

ios目前不能再debug模式測試更新 因為會一直載入本地jsbundle 覆蓋下載後的,所以看不到效果。

測試iOS

開啟 AppDelegate.m檔案

1:修改:

jsCodeLocation = [CodePush bundleURL];

//#ifdef DEBUG
////  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
//  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios"fallbackResource:nil];
//#else
//  jsCodeLocation = [CodePush bundleURL];
//#endif
//
2:開啟node_modules/react-native/scripts/react-native-code.sh 17-20
  # if [[ "$PLATFORM_NAME" = "iphonesimulator" ]]; then
      # echo "Skipping bundling for Simulator platform"
      # exit 0;
    # fi

3:重新react-native run-ios

JS端:

import codePush from 'react-native-code-push'
componentDidMount(){
CodePush.sync({
    updateDialog: {
        mandatoryContinueButtonLabel:'更新',
        mandatoryUpdateMessage:'有新版本了,請您及時更新',
        optionalIgnoreButtonLabel: '稍後',
        optionalInstallButtonLabel: '後臺更新',
        optionalUpdateMessage: '有新版本了,是否更新?',
        title: '更新提示'
    },
    installMode: CodePush.InstallMode.IMMEDIATE
});
}


使用階段:

釋出熱更新命令

code-push release-react test android  --t 1.0.0 --dev false --d Production --des "更新描述” --m true

test註冊codepushAPP的名字

android:平臺 Android/ios

t :要對那個版本進行熱更新這裡是對1.0.0進行更新

dev:是否是開發者模式

dstaging/production 

des: 更新描述

m:是否強制升級

輸入命令後會自動打包生產檔案上傳到codepush上面

提示:

1:iOS和android最好將版本號改為x.x.x形式

2:使用staging時貌似成功機率很低

3:"react-native-code-push":"^4.1.0-beta",

截圖:




tip

1:不適用於檔案目錄結構更改(新增檔案可以)

2: 引入第三方後 如果修改了原生程式碼更新無法使用會導致閃退。

參考: