哈哈,今天是大豬在簡(jiǎn)書(shū)寫(xiě)的第一篇文章,步入正題前翎,此文使用當(dāng)前最新版本的
RN
與Code-Push
進(jìn)行演示稚配,其中的參數(shù)不會(huì)過(guò)多進(jìn)行詳細(xì)解釋?zhuān)鄥?shù)解釋可參考其它文章,這里只保證APP能正常進(jìn)行熱更新操作港华,方便快速入門(mén)道川,跟著大豬一起來(lái)快活吧。
操作指南
以下操作在Mac系統(tǒng)上完成的立宜,畢竟 大豬 工作多年之后終于買(mǎi)得起一個(gè)Mac了冒萄。
- 創(chuàng)建
React-Native
項(xiàng)目
react-native init dounineApp
- 安裝
code-push-cli
npm install -g code-push-cli
- 注冊(cè)
code-push
帳號(hào)
code-push register
Please login to Mobile Center in the browser window we've just opened.
Enter your token from the browser:
#會(huì)彈出一個(gè)瀏覽器,讓你注冊(cè),可以使用github帳號(hào)對(duì)其進(jìn)行授權(quán),授權(quán)成功會(huì)給一串Token,點(diǎn)擊復(fù)制,在控制進(jìn)行粘貼回車(chē)(或者使用code-push login命令)橙数。
Enter your token from the browser: b0c9ba1f91dd232xxxxxxxxxxxxxxxxx
#成功提示如下方
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.
- 在
code-push
添加一個(gè)ios的app
code-push app add dounineApp-ios ios react-native
#成功提示如下方
Successfully added the "dounineApp-ios" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ yMAPMAjXpfXoTfxCd0Su9c4-U4lU6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
└────────────┴──────────────────────────────────────────────────────────────────┘
- 繼續(xù)在
code-push
添加一個(gè)android的app
code-push app add dounineApp-android android react-native
#成功提示如下方
Successfully added the "dounineApp-android" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name │ Deployment Key │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ PZVCGLlVW-0FtdoCF-3ZDWLcX58L6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging │ T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
└────────────┴──────────────────────────────────────────────────────────────────┘
- 在項(xiàng)目根目錄添加
react-native-code-push
npm install react-native-code-push --save
#或者
yarn add react-native-code-push
- link react-native-code-push
react-native link
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) T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d
#將剛才添加的Android App的Deployment Key復(fù)制粘貼到這里,復(fù)制名為Staging測(cè)試Deployment Key尊流。
rnpm-install info Linking react-native-code-push android dependency
rnpm-install info Android module react-native-code-push has been successfully linked
rnpm-install info Linking react-native-code-push ios dependency
rnpm-install WARN ERRGROUP Group 'Frameworks' does not exist in your Xcode project. We have created it automatically for you.
rnpm-install info iOS module react-native-code-push has been successfully linked
Running ios postlink script
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d
#繼續(xù)復(fù)制Ios的Deployment Key
Running android postlink script
- 在
react-native
的App.js
文件添加自動(dòng)更新代碼
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為剛才生成的,打包哪個(gè)平臺(tái)的App就使用哪個(gè)Key,這里用IOS的打包測(cè)試
deploymentKey: 'IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d',
});
}
...
- 運(yùn)行項(xiàng)目在ios模擬器上
react-native run-ios
如圖下所顯
1:開(kāi)啟debug調(diào)試
2:CodePush
已經(jīng)成功運(yùn)行
目前App已經(jīng)是最新版本
- 發(fā)布一個(gè)ios新版本
code-push release-react dounineApp-ios ios
發(fā)布成功如圖下
Detecting ios app version:
Using the target binary version value "1.0" from "ios/dounineApp/Info.plist".
Running "react-native bundle" command:
node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush --bundle-output /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Loading dependency graph, done.
bundle: start
bundle: finish
bundle: Writing bundle output to: /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/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/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush" directory to the "Staging" deployment of the "dounineApp-ios" app.
- 重新Load刷新應(yīng)用
- 安卓發(fā)布
與上面9~11步驟是一樣的,命令改成Android對(duì)應(yīng)的,以下命令結(jié)果簡(jiǎn)化
1.修改App.js的deploymentKey為安卓的
deploymentKey:'T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d'
2.運(yùn)行
react-native run-android
3.發(fā)布
code-push release-react dounineApp-android android
4.刷新應(yīng)用,如下圖
福利 項(xiàng)目源碼