react-native-code-push(react-native 热更新)

1. 全局安装code-push-cli

npm install -g code-push-cli

2. 注册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帐号对其进行授权,授权成功会给一串Token,点击复制,在控制进行粘贴回车(或者使用code-push login命令)。

Enter your token from the browser:  casfwa1234xxxxxxxxxxxxxxxxx
#如果成功的话会有下面提示:
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.

3. 在code-push添加一个ios的app

code-push app add 3271450-ios ios react-native
#成功提示如下方
Successfully added the "3271450-ios" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name       │ Deployment Key                                                   │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ eWkbCoUBPWQhK4hPFDFdijwqV3GWcc988a73-c917-4dba-bd40-1837998442a6 │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging    │ sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6 │
└────────────┴──────────────────────────────────────────────────────────────────┘

4. 在code-push添加一个android的app

code-push app add 3271450-android android react-native
#成功提示如下方
Successfully added the "3271450-android" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name       │ Deployment Key                                                   │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ yGi8qCCYL5s6BKR1S82I1z4OXF2Pcc988a73-c917-4dba-bd40-1837998442a6 │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging    │ fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6 │
└────────────┴──────────────────────────────────────────────────────────────────┘

5. 在项目根目录添加react-native-code-push

npm install react-native-code-push --save

6.执行:react-native link react-native-code-push

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) fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6
#添加的Android App的Deployment Key里面的Staging的Key。
image.png
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6

#添加的ios App的Deployment Key里面的Staging的Key

Running android postlink script

7. 在App.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为刚才生成的,用Platform判断下平台
      deploymentKey: Platform.OS === 'ios'?'sYvpLUxuBU9FxICqJ5sccL2GDUPZcc988a73-c917-4dba-bd40-1837998442a6':'fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6',
      });
  }
  ...

8. 模拟器上运行项目

react-native run-ios
react-native run-android

如图下所显
1:开启debug调试


image.png

2:出现以下内容说明CodePush已经成功运行

image.png

9. 发布一个ios/android新版本
执行:code-push release-react 3271450-ios ios 或code-push release-react 3271450-android android


Detecting ios app version:

Using the target binary version value "1.0" from "ios/vv/Info.plist".

Running "react-native bundle" command:

node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush --bundle-output /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Scanning folders for symlinks in /Users/ct/Desktop/demo/vv/node_modules (20ms)
Scanning folders for symlinks in /Users/ct/Desktop/demo/vv/node_modules (100ms)
Loading dependency graph, done.

bundle: Writing bundle output to: /var/folders/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/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/w5/lh1853r53v9c07p_1m6blttm0000gn/T/CodePush/CodePush" directory to the "Staging" deployment of the "3271450-ios" app.

10. 刷新页面
放的是安卓的,ios显示样式一样

image.png

11. 修改更新弹出框内容
进入node_modules->react-native-code-push->Codepush.js修改以下内容

image.png

修改以后效果:
image.png

12. 需要用户强制更新

code-push release-react 3271450-android android --m true

效果如下


image.png
(样式修改:同样进入node_modules->react-native-code-push->Codepush.js修改以下内容)
image.png

以下为进阶内容:

在app 版本号默认的情况下以上是没问题,但app肯定是需要迭代!

下面是安卓版本号修改以后的截图

android/app/build.gradle中的android.defaultConfig.versionName属性,需要把应用版本改成 1.0.0(默认是1.0,但是codepush需要三位数)。

image.png

这样的话上面代码就失效了。

需要使用下面命令:
code-push release-react <Appname> <Platform> --t <本更新包面向版本号> --des <本次更新说明(可不加)> --m true < --m true 可不加>
code-push release-react 3271450-android android --t 1.0.1;  //非强制更新(1.0.1是versionNmae)
code-push release-react 3271450-android android --t 1.0.1 --m true; //强制更新(1.0.1是versionNmae)

▶️▶️▶️大家注意:修改了原生文件代码(例如MainActivity.java等原生文件,或者添加新插件)时CodePush是无法进行更新的,必须通过提交到应用商店进行更新!!!

亲测没有问题,觉得有用的小伙伴点个小红心就行😄,么么哒。

问题总结:

1、app:recordFilesBeforeBundleCommandDebug

将node-module/react-native-code-push/codepush.gradle修改如下


def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
//修改成以下代码
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["/usr/local/bin/node"]

2、 [Error:Execution failed for task ':app:transformClassesWithDexForDebug]

在项目android/app/build.gradle下添加

 defaultConfig {
        ...
        multiDexEnabled true
}
友情提示:在开发中有遇到RN相关的技术问题,欢迎小伙伴加入交流群(620792950),在群里提问、互相交流学习。交流群也定期更新最新的RN学习资料给大家,谢谢大家支持!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容

  • 《摆渡人》,讲的是一个少女死亡后,在踏入另一个世界的过程中,穿过荒原时与她的灵魂引导者慢慢相爱的故事。他们勇敢地面...
    吃素的萌虎阅读 224评论 0 0
  • 郁闷的年小年将刚写好的通讯稿拿给宋瀛,“今天开会了,我想着写了一篇通讯稿。” “好事啊,那你为什么不开心?”宋瀛从...
    晏苹阅读 435评论 0 0
  • 这是本我把它归入了值得再读一次的书籍之一。 是在亚马逊上看到这本书的,在我的亚马逊首页推荐上,几乎是一看题目就马上...
    晓晓的窝阅读 583评论 0 1
  • 这是一座偏僻的小镇,因为观星的魔鬼岩而出名,每天人来人往。 何浪是一家店的老板,店里很小只有一个木台,上面摆着照片...
    雾中鱼阅读 267评论 1 3