美文网首页
RN开发之CodePush热更新

RN开发之CodePush热更新

作者: 小白这么吃香 | 来源:发表于2019-10-18 14:25 被阅读0次

    一、CodePush CLI安装

    $ npm install -g code-push-cli

    二、注册 CodePush账号

    $ code-push register

    当执行完register命令后,会自动打开一个授权网页,进行授权登录,这里我选择的是使用GitHub登录。注册成功后获取到一个key值,复制后输入终端即可登录成功。

    $ code-push login

    输入此命令验证是否已登录。

    三、在CodePush服务器注册App

    1、注册iOS App:

    code-push app add <AppName> ios react-native

    比如:code-push app add CodePushDemo_iOS ios react-native

    2、注册Android App:

    code-push app add <AppName> Android react-native

    比如:code-push app add CodePushDemo_android Android react-native

    注册成功后会生成2个Deployment Key,分为<Staging>和<Production>,这2个key需要配置到原生项目中去。

    3、查看注册的App列表

    $ code-push app list

    App列表

    4、查看注册App的Deployment Key

    $ code-push deployment ls <AppName> -k

    Deployment Key

    四、添加CodePush组件

    1、安装组件:将codepush添加到node_modules

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

    2、添加依赖:

    $ npm link react-native-code-push

    五、原生项目中配置CodePush

    1、iOS项目配置:

    a.使用Xcode打开项目,在Xcode的项目导航视图中的PROJECT下选择Info页签 ,在Configurations节点下单击 + 按钮 ,选择Duplicate "Release" Configaration,输入Staging。

    添加Staging

    b.选择Build Settings栏,搜索Build Location -> Per-configuration Build Products Path -> Staging,将之前的值:$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)改为:$(BUILD_DIR)/Release$(EFFECTIVE_PLATFORM_NAME)

    注意:Xcode11之后没有了 Per-configuration Build Products Path 这个配置

    Per-configuration Build Products Path

    c.选择Build Settings栏,点击 + 号,选择Add User-Defined Setting,将key设置为CODEPUSH_KEY(名称可自定义,但要与Info.plist里面保持一致),Release和Staging的值为前面创建的Deployment Key,直接复制进去即可。

    配置CODEPUSH_KEY

    d.打开Info.plist文件,在CodePushDeploymentKey中输入$(CODEPUSH_KEY),并修改Bundle versions为三位,CodePush必须要三位(格式:1.1.0)

    Info.plist配置

    e.工程Scheme切换到Staging环境运行、调试

    Scheme切换

    注意:如果报错:library not found for -lPods ,在target下删除libPods-xxxx.a,即可运行。(多target项目会出现)

    删除libPods

    f.将 'CodePush' Pod到原生项目中

    在Podfile文件中添加:pod 'CodePush', :path => '../node_modules/react-native-code-push',然后执行pod install。

    g.修改项目中bundleURL的加载方式

    测试环境:[[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

    改为:[CodePush bundleURL]; <默认生成的文件名为:main.jsbundle>

    注意:如果打包后codepush热更新没有生效,则可能是第<d>步骤配置的codepush_key没有生效,可以直接将Deployment Key只输入。

    配置key

    六、js文件中配置CodePush

    在index.js文件添加CodePush.sync(),App启动后就会加载更新。

    index.js

    在其他js界面的componentDidMount()方法中通过CodePush.sync()进行更新。

    componentDidMount方法


    七、发布更新包

    iOS发布更新:

    Staging环境:code-push release-react demo_iOS ios --plistFile ./ios/demo/Info.plist --mandatory true

    Production环境:code-push release-react demo_iOS ios --plistFile ./ios/demo/Info.plist --deploymentName Production --mandatory true

    Android发布更新:

    Staging环境:code-push release demo_Android ./bundles <版本号> --description '更新内容'--mandatory true

    Production环境:code-push release demo_Android ./bundles <版本号> --deploymentName Production  --description '更新内容' --mandatory true

    八、版本控制

    1、版本回滚(rollback)

    code-push rollback <appName> <deploymentName>  --targetRelease <releaseLabel> 

    如:code-push rollback demo_iOS Staging --targetRelease v4 

            "demo_iOS" 中 "Staging" 部署执行回滚,回滚到v4这个标签版本。

    注意:当部署的版本不同时,不能跨版本回滚!!!

    例如:CodePush历史版本中为7.0.1,此时发布了7.1.0版本。 当从7.1.0发起回滚操作回到7.0.1时,是不可行的。

    2、版本清除(clear)

    code-push deployment clear

    如:code-push deployment clear demo_iOS Staging

    注意:不能指定清除版本,发布的所有版本会被全部清除!!!

    九、版本更新推送百分比控制(rollout)

    code-push promote  <appName> Staging Production

     "App"中"Staging"部署的最新更新发布到"Production"部署中,推送100%的用户。

    code-push promote <appName> Staging Production -r 25

    "App"中"Staging"部署的最新更新发布到"Production"部署中,并且只推送25%的用户。

    rollout

    注意:如果Production版本中有rollout版本存在,则再执行code-push promote会失败。可以先清除之前的版本再重新推送。

    [Error]  Cannot promote to an unfinished rollout release unless it is already disabled.

    相关文章

      网友评论

          本文标题:RN开发之CodePush热更新

          本文链接:https://www.haomeiwen.com/subject/ckjimctx.html