React-Native熱更新 - 3分鐘教你實(shí)現(xiàn)

哈哈,今天是大豬在簡(jiǎn)書寫的第一篇文章,步入正題,此文使用當(dāng)前最新版本的RNCode-Push進(jìn)行演示,其中的參數(shù)不會(huì)過(guò)多進(jìn)行詳細(xì)解釋,更多參數(shù)解釋可參考其它文章,這里只保證APP能正常進(jìn)行熱更新操作,方便快速入門,跟著大豬一起來(lái)快活吧。

操作指南

以下操作在Mac系統(tǒng)上完成的,畢竟 大豬 工作多年之后終于買得起一個(gè)Mac了。

  1. 創(chuàng)建React-Native項(xiàng)目
react-native init dounineApp
  1. 安裝code-push-cli
npm install -g code-push-cli
  1. 注冊(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)行粘貼回車(或者使用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.
  1. 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 │
└────────────┴──────────────────────────────────────────────────────────────────┘
  1. 繼續(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 │
└────────────┴──────────────────────────────────────────────────────────────────┘
  1. 在項(xiàng)目根目錄添加react-native-code-push
npm install react-native-code-push --save
#或者
yarn add react-native-code-push
  1. 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
  1. react-nativeApp.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',
      });
  }
  ...
  1. 運(yùn)行項(xiàng)目在ios模擬器上
react-native run-ios

如圖下所顯

1:開(kāi)啟debug調(diào)試

2:CodePush已經(jīng)成功運(yùn)行

目前App已經(jīng)是最新版本

  1. 發(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.
  1. 重新Load刷新應(yīng)用
  1. 安卓發(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)目源碼


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容