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  to ignore) fqdFCqLyL4XclNZjWvNN3KNhImR5cc988a73-c917-4dba-bd40-1837998442a6
#添加的Android App的Deployment Key里面的Staging的Key。
react-native-code-push(react-native 热更新)_第1张图片
image.png
? What is your CodePush deployment key for iOS (hit  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调试


react-native-code-push(react-native 热更新)_第2张图片
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显示样式一样

react-native-code-push(react-native 热更新)_第3张图片
image.png

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

react-native-code-push(react-native 热更新)_第4张图片
image.png

修改以后效果:
react-native-code-push(react-native 热更新)_第5张图片
image.png

12. 需要用户强制更新

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

效果如下


react-native-code-push(react-native 热更新)_第6张图片
image.png
(样式修改:同样进入node_modules->react-native-code-push->Codepush.js修改以下内容)
react-native-code-push(react-native 热更新)_第7张图片
image.png

以下为进阶内容:

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

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

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

react-native-code-push(react-native 热更新)_第8张图片
image.png

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

需要使用下面命令:
code-push release-react   --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学习资料给大家,谢谢大家支持!

你可能感兴趣的:(react-native-code-push(react-native 热更新))