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

哈哈晰房,今天是大豬在簡書寫的第一篇文章,步入正題还绘,此文使用當前最新版本的RNCode-Push進行演示,其中的參數(shù)不會過多進行詳細解釋如庭,更多參數(shù)解釋可參考其它文章,這里只保證APP能正常進行熱更新操作撼港,方便快速入門坪它,跟著大豬一起來快活吧。

操作指南

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

  1. 創(chuàng)建React-Native項目
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帳號對其進行授權(quán),授權(quán)成功會給一串Token,點擊復制,在控制進行粘貼回車(或者使用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. 在項目根目錄添加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復制粘貼到這里,復制名為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ù)復制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. 運行項目在ios模擬器上
react-native run-ios

如圖下所顯

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

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

目前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刷新應用
  1. 安卓發(fā)布

與上面9~11步驟是一樣的懒震,命令改成Android對應的,以下命令結(jié)果簡化

1.修改App.js的deploymentKey為安卓的

deploymentKey:'T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d'

2.運行

react-native run-android

3.發(fā)布

code-push release-react dounineApp-android android

4.刷新應用,如下圖

福利 項目源碼


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市嗤详,隨后出現(xiàn)的幾起案子个扰,更是在濱河造成了極大的恐慌,老刑警劉巖葱色,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件递宅,死亡現(xiàn)場離奇詭異,居然都是意外死亡苍狰,警方通過查閱死者的電腦和手機办龄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來淋昭,“玉大人土榴,你說我怎么就攤上這事∠炫#” “怎么了搔谴?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵姊扔,是天一觀的道長。 經(jīng)常有香客問我,道長始腾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任扇救,我火速辦了婚禮鹃共,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘豺憔。我一直安慰自己额获,他們只是感情好,可當我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布恭应。 她就那樣靜靜地躺著抄邀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪昼榛。 梳的紋絲不亂的頭發(fā)上境肾,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天,我揣著相機與錄音胆屿,去河邊找鬼奥喻。 笑死,一個胖子當著我的面吹牛非迹,可吹牛的內(nèi)容都是我干的环鲤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼憎兽,長吁一口氣:“原來是場噩夢啊……” “哼冷离!你這毒婦竟也來了结闸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤酒朵,失蹤者是張志新(化名)和其女友劉穎桦锄,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蔫耽,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡结耀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了匙铡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片图甜。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖鳖眼,靈堂內(nèi)的尸體忽然破棺而出黑毅,到底是詐尸還是另有隱情,我是刑警寧澤钦讳,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布矿瘦,位于F島的核電站,受9級特大地震影響愿卒,放射性物質(zhì)發(fā)生泄漏缚去。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一琼开、第九天 我趴在偏房一處隱蔽的房頂上張望易结。 院中可真熱鬧,春花似錦柜候、人聲如沸搞动。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鹦肿。三九已至,卻和暖如春飞主,著一層夾襖步出監(jiān)牢的瞬間狮惜,已是汗流浹背高诺。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工碌识, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虱而。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓筏餐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牡拇。 傳聞我的和親對象是個殘疾皇子魁瞪,可洞房花燭夜當晚...
    茶點故事閱讀 45,066評論 2 355

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