React Native 热更新-App端集成react-native-code-push

"react": "16.9.0",
"react-native": "0.61.5",

Android

在项目中安装 react-native-code-push插件,终端进入你的项目根目录然后运行

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

 

插件适用于版本大于 React Native 0.60 version 的安装和配置步骤 (Android)

  1. 在/android/settings.gradle中末尾添加如下代码:

    include ':app', ':react-native-code-push'
    project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
  2. 在 android/app/build.gradle文件里面添如下代码

    apply from: "../../node_modules/react-native/react.gradle"后面
    apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
     
  3. 更新 MainApplication.java 按照如下代码:

    // 1. Import the plugin class.
    import com.microsoft.codepush.react.CodePush;
    
    public class MainApplication extends Application implements ReactApplication {
    
        private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
            ...
    
            // 2. Override the getJSBundleFile method in order to let
            // the CodePush runtime determine where to get the JS
            // bundle location from on each app start
            @Override
            protected String getJSBundleFile() {
                return CodePush.getJSBundleFile();
            }
        };
    }
  4. 添加 Deployment key 到 strings.xml:

 

  让CodePush运行时知道应该查询哪个deployment 更新,在strings.xml文件并添加一个新的字符串CodePushDeploymentKey,其值要配置这个App的Deployment key(比如图中APP的Staging Deployment key)。用命令code-push deployment list appName -k 可以查询, 复制(如图)你想使用的Deployment key的值到Strings.xml中的CodePushDeploymentKey中。

 

Deployment list

strings.xml 应该像下面这样:

<resources>
     <string name="app_name">AppName</string>
     <string moduleConfig="true" name="CodePushDeploymentKey">DeploymentKey</string>
 </resources>

整合到本地code-push-server还要增加CodePushServerUrl,默认指向微软服务器,配置后指定本地服务器地址

<resources>
     <string name="app_name">AppName</string>
     <string moduleConfig="true" name="CodePushDeploymentKey">DeploymentKey</string>
    <string moduleConfig="true" name="CodePushServerUrl">http://127.0.0.1:3000</string>
 </resources>

 

Note: 如果需要动态地使用不同的部署,还可以在JS代码中使用 Code-Push options

为了有效地利用随CodePush应用程序一起创建的Staging 和Production deployments,可以使用以下配置 

For React Native >= v0.60

  1.  在 android/app/build.gradle文件里面添如下代码

  2. 找到 android { buildTypes {} } 这一部分,针对不同的编译发布环境增加 resValue

    ...
    buildTypes {
        debug {
            ...
            // Note: CodePush updates should not be tested in Debug mode as they are overriden by the RN packager. 
       // However, because CodePush checks for updates in all modes, we must supply a key.
            resValue "string", "CodePushDeploymentKey", ""
            ...
        }

        releaseStaging {
            ...
       //需要配置加密钥匙,可以复用release
       signingConfig signingConfigs.release
            resValue "string", "CodePushDeploymentKey", "<INSERT_STAGING_KEY>"
       //整合到本地code-push-server还要增加CodePushServerUrl,默认指向微软服务器,配置后指定本地服务器地址
       resValue "string", "CodePushServerUrl", "Server URL"
            // Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues
            // Add the following line if not already there
            matchingFallbacks = ['release']//定义子模块没有定义此buildTpye而用的buildType

            ...
        }

        release {
            ...
            resValue "string", "CodePushDeploymentKey", "<INSERT_PRODUCTION_KEY>"
       resValue "string", "CodePushServerUrl", "Server URL"

            ...
        }
    }
    ...
}

 

 

NOTE: 记得把以上的key从 strings.xml移除

NOTE:  releaseStaging 的命名约定参考 this line.

posted @ 2020-12-04 11:50  晓燚缺  阅读(262)  评论(0编辑  收藏  举报