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

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

操作指南

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

  1. 創(chuàng)建React-Native項(xiàng)目
react-native init dounineApp
  1. 安裝code-push-cli
npm install -g code-push-cli
  1. 注冊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,點(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添加一個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添加一個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測試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文件添加自動更新代碼
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為剛才生成的,打包哪個平臺的App就使用哪個Key,這里用IOS的打包測試
      deploymentKey: 'IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d',
      });
  }
  ...
  1. 運(yùn)行項(xiàng)目在ios模擬器上
react-native run-ios

如圖下所顯

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

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

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

  1. 發(fā)布一個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對應(yīng)的,以下命令結(jié)果簡化

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)容合作請聯(lián)系作者
  • 序言:七十年代末短曾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赐劣,更是在濱河造成了極大的恐慌嫉拐,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件魁兼,死亡現(xiàn)場離奇詭異婉徘,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)璃赡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門判哥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來献雅,“玉大人碉考,你說我怎么就攤上這事⊥ι恚” “怎么了侯谁?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長章钾。 經(jīng)常有香客問我墙贱,道長,這世上最難降的妖魔是什么贱傀? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任惨撇,我火速辦了婚禮,結(jié)果婚禮上府寒,老公的妹妹穿的比我還像新娘魁衙。我一直安慰自己,他們只是感情好株搔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布剖淀。 她就那樣靜靜地躺著,像睡著了一般纤房。 火紅的嫁衣襯著肌膚如雪纵隔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音捌刮,去河邊找鬼碰煌。 笑死,一個胖子當(dāng)著我的面吹牛糊啡,可吹牛的內(nèi)容都是我干的拄查。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼棚蓄,長吁一口氣:“原來是場噩夢啊……” “哼堕扶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起梭依,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤稍算,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后役拴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糊探,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年河闰,在試婚紗的時候發(fā)現(xiàn)自己被綠了科平。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡姜性,死狀恐怖瞪慧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情部念,我是刑警寧澤弃酌,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站儡炼,受9級特大地震影響妓湘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜乌询,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一榜贴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧妹田,春花似錦唬党、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沮趣,卻和暖如春屯烦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工驻龟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留温眉,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓翁狐,卻偏偏與公主長得像类溢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子露懒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容