1. 全局安裝code-push-cli
npm install -g code-push-cli
2. 注冊code-push
帳號
code-push register
Please login to Mobile Center in the browser window we've just opened.
Enter your token from the browser:
#會彈出一個瀏覽器,讓你注冊,可以使用github帳號對其進(jìn)行授權(quán),授權(quán)成功會給一串Token,點擊復(fù)制,在控制進(jìn)行粘貼回車(或者使用code-push login命令)晋控。
Enter your token from the browser: casfwa1234xxxxxxxxxxxxxxxxx
#如果成功的話會有下面提示:
Successfully logged-in. Your session file was written to /Users/huanghuanlai/.code-push.config. You can run the code-push logout command at any time to delete this file and terminate your session.
3. 在code-push
添加一個ios的app
code-push app add 3271450-ios ios react-native
#成功提示如下方
Successfully added the "3271450-ios" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ eWkbCoUBPWQhK4hPFDFdijwqV3GWcc988a73-c917-4dba-bd40-1837998442a6 │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6 │
└────────────┴──────────────────────────────────────────────────────────────────┘
4. 在code-push
添加一個android的app
code-push app add 3271450-android android react-native
#成功提示如下方
Successfully added the "3271450-android" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ yGi8qCCYL5s6BKR1S82I1z4OXF2Pcc988a73-c917-4dba-bd40-1837998442a6 │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6 │
└────────────┴──────────────────────────────────────────────────────────────────┘
5. 在項目根目錄添加react-native-code-push
npm install react-native-code-push --save
6.執(zhí)行:react-native link react-native-code-push
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (8ms)
? What is your CodePush deployment key for Android (hit <ENTER> to ignore) fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6
#添加的Android App的Deployment Key里面的Staging的Key。
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6
#添加的ios App的Deployment Key里面的Staging的Key
Running android postlink script
7. 在App.js
文件添加以下代碼
...
import codePush from "react-native-code-push";
const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
export default class App extends Component<{}> {
componentDidMount(){
codePush.sync({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE,
mandatoryInstallMode:codePush.InstallMode.IMMEDIATE,
//deploymentKey為剛才生成的,用Platform判斷下平臺
deploymentKey: Platform.OS === 'ios'?'sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6':'fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6',
});
}
...
8. 模擬器上運行項目
react-native run-ios
react-native run-android
如圖下所顯
1:開啟debug調(diào)試
2:出現(xiàn)以下內(nèi)容說明CodePush
已經(jīng)成功運行
9. 發(fā)布一個ios/android新版本
執(zhí)行:code-push release-react 3271450-ios ios 或code-push release-react 3271450-android android
Detecting ios app version:
Using the target binary version value "1.0" from "ios/vv/Info.plist".
Running "react-native bundle" command:
node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush --bundle-output /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Scanning folders for symlinks in /Users/ct/Desktop/demo/vv/node_modules (20ms)
Scanning folders for symlinks in /Users/ct/Desktop/demo/vv/node_modules (100ms)
Loading dependency graph, done.
bundle: Writing bundle output to: /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush/main.jsbundle
bundle: Done writing bundle output
Releasing update contents to CodePush:
Upload progress:[==================================================] 100% 0.0s
Successfully released an update containing the "/var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush" directory to the "Staging" deployment of the "3271450-ios" app.
10. 刷新頁面
放的是安卓的,ios顯示樣式一樣
11. 修改更新彈出框內(nèi)容
進(jìn)入node_modules->react-native-code-push->Codepush.js修改以下內(nèi)容
修改以后效果:
12. 需要用戶強制更新
code-push release-react 3271450-android android --m true
效果如下
(樣式修改:同樣進(jìn)入node_modules->react-native-code-push->Codepush.js修改以下內(nèi)容)
以下為進(jìn)階內(nèi)容:
在app 版本號默認(rèn)的情況下以上是沒問題,但app肯定是需要迭代破停!
下面是安卓版本號修改以后的截圖
android/app/build.gradle中的android.defaultConfig.versionName屬性轧铁,需要把應(yīng)用版本改成 1.0.0(默認(rèn)是1.0,但是codepush需要三位數(shù))癌椿。
這樣的話上面代碼就失效了。
需要使用下面命令:
code-push release-react <Appname> <Platform> --t <本更新包面向版本號> --des <本次更新說明(可不加)> --m true < --m true 可不加>
code-push release-react 3271450-android android --t 1.0.1; //非強制更新(1.0.1是versionNmae)
code-push release-react 3271450-android android --t 1.0.1 --m true; //強制更新(1.0.1是versionNmae)
??????大家注意:修改了原生文件代碼(例如MainActivity.java等原生文件,或者添加新插件)時CodePush是無法進(jìn)行更新的菱阵,必須通過提交到應(yīng)用商店進(jìn)行更新L叨怼!晴及!
親測沒有問題都办,覺得有用的小伙伴點個小紅心就行??,么么噠虑稼。
問題總結(jié):
1琳钉、app:recordFilesBeforeBundleCommandDebug
將node-module/react-native-code-push/codepush.gradle修改如下
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
//修改成以下代碼
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["/usr/local/bin/node"]
2、 [Error:Execution failed for task ':app:transformClassesWithDexForDebug]
在項目android/app/build.gradle下添加
defaultConfig {
...
multiDexEnabled true
}