codepush3之Android原生引用集成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
其他命令:
https://github.com/Microsoft/code-push/blob/master/cli/README-cn.md
配置階段
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
然後再次熱更新即可成功測試。
issue:https://github.com/Microsoft/react-native-code-push/issues/941
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:註冊codepush時APP的名字
android:平臺 Android/ios
t :要對那個版本進行熱更新這裏是對1.0.0進行更新
dev:是否是開發者模式
d:staging/production
des: 更新描述
m:是否強制升級
輸入命令後會自動打包生產文件上傳到codepush上面
提示:
1:iOS和android最好將版本號改爲x.x.x形式
2:使用staging時貌似成功機率很低
3:"react-native-code-push":"^4.1.0-beta",
截圖:
tip:
1:不適用於文件目錄結構更改(添加文件可以)
2: 引入第三方後 如果修改了原生代碼更新無法使用會導致閃退。
參考:
https://github.com/wuyunqiang/ReactNativeUtil/issues/26
http://blog.csdn.net/fengyuzhengfan/article/details/52003798
https://github.com/Microsoft/react-native-code-push