React-Native使用Codepush热更新

React-Native使用Codepush热更新

在开始之前,首先了解一下RN的热更新原理

开始:

首先,在全局安装appcenter

powershell 复制代码
npm install  -g appcenter-cli

下面对appcenter进行设置

1.登陆

appcenter是微软的,所以我们需要登陆使用,在终端中输入登录命令appcenter login,会自动打开浏览器,可以选择自己喜欢的方式登陆,登陆后会在网页上显token,点击复制复制下来,关闭浏览器,粘贴到终端中,回车。

2.配置命令
  • 查看配置:appcenter profile list
  • 退出登录:appcenter logout
  • 查看所有的登录token:appcenter tokens list
  • 删除一个token:appcenter tokens delete <machineName>
3.appcenter相关操作

以下操作均可在网页版appcenter可视化进行,此处仅说明终端部分

为iOS和安卓分别创建App

**** 备注*:将ownerName替换成你在Appcenter的用户名 ****

针对不同系统分别执行下面的命令

shell 复制代码
appcenter apps create -d <appDisplayName> -o <operatingSystem>  -p <platform>

例如针对iOS和安卓分别执行如下命令

shell 复制代码
appcenter apps create -d RNDemoAndroid -o Android -p React-Native
appcenter apps create -d RNDemoiOS -o iOS -p React-Native

在Appcenter创建完App之后需要针对每个App创建Staging和Production 环境对应的Key

shell 复制代码
appcenter codepush deployment add -a <ownerName>/RNDemoiOS Staging
appcenter codepush deployment add -a <ownerName>/RNDemoiOS Production

例如我创建了一个名为TimeTable的安卓应用,则我的终端显示如图:(使用appcenter apps list查看应用列表)

然后使用appcenter codepush deployment list -k -a <username>/myapp-android查看对应应用的deployment key

到此为止,所有的配置已经结束,接下来就是进入项目中进行

然后进入项目中进行

React-native项目安装Codepush

cd <project name>进入项目目录,然后执行命令:

powershell 复制代码
npm install --save react-native-code-push
react-native项目中安卓部分修改
1.修改android/settings.gradle,添加如下代码
js 复制代码
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,添加如下代码
js 复制代码
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
3.修改MainApplication.java文件

在文件头部引入codepush类,并override getJSBundleFile(),整体代码如下

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 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.修改android/app/build.gradle,在buildTypes代码块中添加如下代码

将<INSERT_STAGING_KEY>替换成你在codepush中的deploymentKey

js 复制代码
 buildTypes {
        debug {
            signingConfig signingConfigs.debug
            ndk {
                abiFilters reactNativeArchitectures()
            }
            // Note: CodePush updates shouldn't be tested in Debug mode as they're overriden by the RN packager. However, because CodePush checks for updates in all modes, we must supply a key.
            resValue "string", "CodePushDeploymentKey", '""'
        }
        //**************************添加代码start***************************************
        releaseStaging {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            resValue "string", "CodePushDeploymentKey", '"<INSERT_STAGING_KEY>"'
            matchingFallbacks = ["release"]
        }
        //**************************添加代码end***************************************
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            resValue "string", "CodePushDeploymentKey", '"<INSERT_STAGING_KEY>"'
        }
    }
5.js/ts代码

因为是demo项目,我直接更改了本身的app.js代码,实际开发中视情况而定

js 复制代码
// App.js
import React from 'react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  useColorScheme,
  View,
  Button
} from 'react-native';
import packageInfo from './package.json'
import CodePush from 'react-native-code-push';
import {
  Colors,
  Header,
} from 'react-native/Libraries/NewAppScreen';

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
 * LTI update could not be added via codemod */
const Section = ({children, title}) => {
  const isDarkMode = useColorScheme() === 'dark';
  return (
    <View style={styles.sectionContainer}>
      <Text
        style={[
          styles.sectionTitle,
          {
            color: isDarkMode ? Colors.white : Colors.black,
          },
        ]}>
        {title}
      </Text>
      <Text
        style={[
          styles.sectionDescription,
          {
            color: isDarkMode ? Colors.light : Colors.dark,
          },
        ]}>
        {children}
      </Text>
    </View>
  );
};

const App = () => {
  const isDarkMode = useColorScheme() === 'dark';
  
  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  //检查更新方法
  const checkForUpdate = () => {
    CodePush.sync({
      installMode: CodePush.InstallMode.IMMEDIATE,
    },(status)=>{
      switch (status) {
        case CodePush.SyncStatus.UP_TO_DATE:{
          alert("当前已经是最新版本")
        }
        break;
        case CodePush.SyncStatus.UPDATE_INSTALLED:{
          alert("最新版本已安装")
        }
        break;

        default:
        break
      }
      
      console.log(status)
    },()=>{
      
    });
  };
  //清除更新
  const clear = () => {
    CodePush.clearUpdates();
  };
  var name = packageInfo.name;
  var version = packageInfo.version;
  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title="项目名称">
            <Text style={styles.highlight}>{name}</Text>  
          </Section>
          <Section title="版本号">
            <Text style={styles.highlight}>{version}</Text>
          </Section>
          <Section title="Version Info">
            <Button title='检查更新' onPress={checkForUpdate}/>
            <Button title='清除更新' onPress={clear}/>
          </Section>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};


const styles = StyleSheet.create({
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
  },
  highlight: {
    fontWeight: '700',
  },
});

export default App;

最后打包项目

我更改了package.json中的版本号,然后打包上传了版本。

在终端中执行命令appcenter codepush release-react -a <username>/appname上传版本

使用appcenter codepush deployment list -a <username>/appname可以看到当前用户更新情况,如图:

相关推荐
sealaugh3221 小时前
react native(学习笔记第四课) 英语打卡微应用(3)-ocr的文字转化成语音文件(tts)
笔记·学习·react native
wordbaby2 天前
如何封装一个生产级的 React Native 分页列表 Hook
前端·react native·react.js
沐言人生4 天前
ReactNative 源码分析5——ReactActivity之启动RN应用
android·react native
沐言人生5 天前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
沐言人生6 天前
ReactNative 源码分析3——ReactActivity之初始化RN应用
android·react native
一个扣子6 天前
Hermes 未来路线图:2025 年起的新特性与 React Native New Architecture 协同
react native·未来发展·路线图·hermes·字节码diffing·性能增强
沐言人生7 天前
React Native 源码分析1——HybridData 机制深度分析
android·react native
空中海7 天前
01 React Native 基础、核心组件与布局体系
javascript·react native·react.js
Yue1687 天前
一文教你五分钟学会Zustand,React状态管理更加方便!
react native
空中海7 天前
03 性能、动画与 React Native 新架构
react native·react.js·架构