前提:熱更新方案 選擇:code-push
安裝App Center CLI:
sudo npm install -g appcenter-cli
登陸app cetner:
$ appcenter login
我是用的github登陸:
用戶名:
lypcliuli
3b3c63c5b396a247c5e8ba0742727872408bf062e7
運(yùn)行以上命令并在命令行確認(rèn)后,網(wǎng)頁(yè)會(huì)彈出一個(gè)要求登陸的頁(yè)面,登陸后倘是,會(huì)得到一串Access code涨醋,復(fù)制粘貼回命令行阵具,成功的話會(huì)返回登陸賬號(hào)
添加App信息庭砍,這里要分別添加安卓與iOS彩届,我的app名字是ZhangFeiApp,以此為例:
// -d 后面接的是app顯示的名字吨枉,為了區(qū)分不同平臺(tái)后面也寫(xiě)上平臺(tái)命
// -o 表示運(yùn)行系統(tǒng)(operation) 安卓/iOS
// -p 表示平臺(tái)(Platform)這里是 react-native
$ appcenter apps create -d ZhangFeiApp-android -o Android -p React-Native
$ appcenter apps create -d ZhangFeiApp-ios -o iOS -p React-Native
LYPC:~ LYPC$ appcenter apps create -d ZhangFeiApp-android -o Android -p React-Native
App Secret: e5c504be-532a6-427d-a2e6-4f2e2853ad1e
Description:
Display Name: ZhangFeiApp-android
Name: ZhangFeiApp-android
OS: Android
Platform: React-Native
Release Type:
Owner ID: cc0b5aa7-eb8f2-4d8e-926a-dea678c9299f
Owner Display Name: 下里巴人
Owner Email: lypcliuli@163.com
Owner Name: lypcliuli-163.com
Azure Subscription ID:
LYPC:~ LYPC$
LYPC:~ LYPC$ appcenter apps create -d ZhangFeiApp-ios -o iOS -p React-Native
App Secret: 7ec3848c-0324b-4add-929f5-35f0ca5e9f5e
Description:
Display Name: ZhangFeiApp-ios
Name: ZhangFeiApp-ios
OS: iOS
Platform: React-Native
Release Type:
Owner ID: cc0b5aa7-eb228f-4d8e-926a-dea678c9299f
Owner Display Name: 下里巴人_liuli
Owner Email: lypcliuli@163.com
Owner Name: lypcliuli
Azure Subscription ID:
LYPC:~ LYPC$
運(yùn)行一下appcenter apps list檢測(cè)是否添加成功:
LYPC:~ LYPC$ appcenter apps list
lypcliuli/ZhangFeiApp-android
lypcliuli/ZhangFeiApp-ios
LYPC:~ LYPC$
到此~成功添加
將已添加的app部署熱更新服務(wù)蹦渣,一般會(huì)部署兩個(gè)用于灰度更新,和正式更新貌亭,這里分別叫做Staging與Production柬唯。分別給安卓和iOS部署,所以一共要運(yùn)行四行命令
// -a 是指應(yīng)用(application)圃庭,這里要寫(xiě)上“用戶名和程序名”
// 部署IOS
$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-ios Staging
$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-ios Production
// 部署安卓
$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-android Staging
$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-android Production
執(zhí)行截圖:
LYPC:~ LYPC$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-ios Staging
Deployment Staging has been created for lypcliuli/ZhangFeiApp-ios with key rPAobllL5JO1lumtQVK7rUxbpfS0K3NrknEwRqgB
LYPC:~ LYPC$
LYPC:~ LYPC$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-ios Production
Deployment Production has been created for lypcliuli/ZhangFeiApp-ios with key LYTC3GoEpcdf_RoGglOLJLkZq7MM-QSkMLPRcgB
LYPC:~ LYPC$
LYPC:~ LYPC$ appcenter codepush deployment add -a lypcliuli-163.com/ZhangFeiApp-android Staging
Deployment Staging has been created for lypcliuli-163.com/ZhangFeiApp-android with key mQkr7LW0ufgsSljOyTiyfiAoJ132BJXPDA9eH
LYPC:~ LYPC$
LYPC:~ LYPC$ appcenter codepush deployment add -a lypcliuli/ZhangFeiApp-android Production
Deployment Production has been created for lypcliuli/ZhangFeiApp-android with key Smm_Q7dQdsFfypkZfHhd3FfM4i03HkmdD0qxB
LYPC:~ LYPC$
獲取部署碼锄奢,運(yùn)行以上命令之后失晴,命令行會(huì)返部署碼,但是有可能沒(méi)記下就關(guān)掉了命令行
appcenter codepush deployment list -a <ownerName>/<appName> <deploymentName> -k 命令可以查看部署碼:
看iOS的:
appcenter codepush deployment list -a lypcliuli/ZhangFeiApp-ios -k
LYPC:~ LYPC$ appcenter codepush deployment list -a lypcliuli/ZhangFeiApp-ios -k
┌────────────┬───────────────────────────────────────┐
│ Name │ Key │
├────────────┼───────────────────────────────────────┤
│ Staging │ rPAobL5JO1umtQVK7rkUxbpfS0K3NrknEwRqgB │
├────────────┼───────────────────────────────────────┤
│ Production │ LYTC3GEpcdf_RGkglOLJLkZq7MM-QSkMLPRcgB │
└────────────┴───────────────────────────────────────┘
LYPC:~ LYPC$
看Android的:
appcenter codepush deployment list -a lypcliuli/ZhangFeiApp-android -k
LYPC:~ LYPC$ appcenter codepush deployment list -a lypcliuli/ZhangFeiApp-android -k
┌────────────┬───────────────────────────────────────┐
│ Name │ Key │
├────────────┼───────────────────────────────────────┤
│ Staging │ mQkr7LW0ufgsSljOyTiyfiAooJ132BJXPDA9eH │
├────────────┼───────────────────────────────────────┤
│ Production │ Smm_Q7dQdsFfypkZfHhd3FfM4i03HokmdD0qxB │
└────────────┴───────────────────────────────────────┘
LYPC:~ LYPC$
客戶端安裝與配置:創(chuàng)建好你的RN項(xiàng)目然后進(jìn)行以下操作
安裝依賴包:
yarn add react-native-code-push
react-native link react-native-code-push
運(yùn)行react-native link react-native-code-push的時(shí)候拘央,命令行會(huì)提示輸入部署碼What is your CodePush deployment key for Android (hit <ENTER> to ignore)涂屁,這個(gè)提示只是第一次輸入有效。
會(huì)詢問(wèn)你? What is your CodePush deployment key for Android (hit <ENTER> to ignore)
輸入你的不熟碼:填寫(xiě)部署碼灰伟,我這里都是輸入Staging的部署碼拆又。如果是正式環(huán)境,建議寫(xiě)Production的部署碼:
LYPC:ZhangFeiApp LYPC$ react-native link react-native-code-push
info Linking "react-native-code-push" iOS dependency
info iOS module "react-native-code-push" has been successfully linked
info Linking "react-native-code-push" Android dependency
info Android module "react-native-code-push" has been successfully linked
Running android postlink script
? What is your CodePush deployment key for Android (hit <ENTER> to ignore) mQkr7
LW0ufgsSljOyTiyfiAoJ132BJXPDA9eH
Running ios postlink script
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) rPAobL5JO
1umtQVK7rUxbpfS0K3NrknEwRqgB
LYPC:ZhangFeiApp LYPC$
iOS平臺(tái):
使用Xcode打開(kāi)iOS項(xiàng)目栏账,找到Info.plist文件 修改CodePushDeploymentKey 的值為的部署碼 此處我修改成了的是iOS的Staging環(huán)境的rPAobL5JO1umtQVK7rUxbpfS0K3NrknEwRqgB:
Android平臺(tái):
沒(méi)改 后序完善
api調(diào)用(安靜模式)
安靜模式是我最喜歡一種帖族,配置簡(jiǎn)單,在用戶沒(méi)察覺(jué)的情況下就更新了app发笔。在用戶打開(kāi)app的時(shí)候盟萨,自動(dòng)下載更新包,下次再啟動(dòng)的時(shí)候自動(dòng)安裝更新包
api調(diào)用(自定義模式)
在更新之前可以獲取更新包的大小了讨,更新的具體信息捻激,監(jiān)聽(tīng)下載進(jìn)度等等。
推送更新版版:
推送命令前计,在項(xiàng)目根目錄運(yùn)行
appcenter codepush release-react -a lypcliuli-163.com/ZhangFeiApp-ios
// 在默認(rèn)情況下胞谭,更新會(huì)推送到Staging的部署
appcenter codepush release-react -a lypcliuli-163.com/ZhangFeiApp-android
// 指定版本更新 -d 加部署名
appcenter codepush release-react -a lypcliuli-163.com/ZhangFeiApp-android -d Production
// 設(shè)置更新日志,供前端讀取
appcenter codepush release-react -a lypcliuli-163.com/ZhangFeiApp-android --description '1800的更新'
強(qiáng)制更新男杈,在項(xiàng)目根目錄運(yùn)行:
appcenter codepush release-react -a lypcliuli-163.com/ZhangFeiApp-ios -m true
然后在app里面檢測(cè)更新 就能檢測(cè)到新版了
下一篇文章分享一個(gè)原生和RN混合開(kāi)發(fā)+熱更新的demo