在现有 React Native 项目中安装 expo-updates

了解如何在现有的 React Native 项目中安装和配置 expo-updates。


expo-updates 是一个库,能够让你的应用管理远程更新应用代码。它会与配置的远程更新服务通信,以获取可用更新的信息。本指南将解释如何为 EAS Update 设置一个基础的 React Native 项目,EAS Update 是一个托管的远程更新服务,包含简化 expo-updates 库安装和配置的工具。

你在项目中使用连续本地生成(CNG)吗?

你可能在看错误的指南。要在使用 CNG 的项目中使用 expo-updates,请参阅 EAS 更新“入门指南”

🌐 You may be reading the wrong guide. To use expo-updates in a project that uses CNG, see EAS Update "Get started".

先决条件

🌐 Prerequisites

必须安装并配置 expo 包。 如果你是使用 npx @react-native-community/cli@latest init 创建的项目,并且没有安装任何其他 Expo 库,则需要在继续之前安装 Expo 模块

安装

🌐 Installation

要开始,请安装 expo-updates

🌐 To get started, install expo-updates:

Terminal
npx expo install expo-updates

然后,为 iOS 安装 pods:

🌐 Then, install pods for iOS:

Terminal
npx pod-install

配置 expo-updates 库

🌐 Configuring expo-updates library

将以下部分的差异更改应用到你的项目中,以配置 expo-updates

🌐 Apply the changes from the diffs from the following sections to configure expo-updates in your project.

JavaScript 和 JSON

🌐 JavaScript and JSON

运行 eas update:configure 来在 app.json 中设置 updates URL 和 projectId

🌐 Run eas update:configure to set the updates URL and projectId in app.json.

Terminal
eas update:configure

修改 app.jsonexpo 部分。如果你使用 npx @react-native-community/cli@latest init 创建了项目,你将需要添加以下更改,包括 updates URL

🌐 Modify the expo section of app.json. If you created your project using npx @react-native-community/cli@latest init, you will need to add the following changes including the updates URL.

下方示例中的 updates URL 和 projectId 用于 EAS 更新。当运行 eas update:configure 时,EAS CLI 会为 EAS 更新服务正确设置此 URL。

If you want to set up a custom expo-updates server instead, add your URL to updates.url in app.json.

app.json
11"expo": {
22 "name": "MyApp",
3 "updates": {
4 "url": "https://u.expo.dev/[your-project-id]"
5 }
3 "updates": {
4 "url": "http://localhost:3000/api/manifest"
5 }
66}
77}

安卓

🌐 Android

修改 android/app/build.gradle,以检查 Expo 文件中的 JS 引擎配置(JSC 或 Hermes):

🌐 Modify android/app/build.gradle to check for the JS engine configuration (JSC or Hermes) in Expo files:

修改 android/app/src/main/AndroidManifest.xml,添加 expo-updates 配置 XML,使其与 app.json 的内容一致:

🌐 Modify android/app/src/main/AndroidManifest.xml to add the expo-updates configuration XML so that it matches the contents of app.json:

如果使用更新服务器 URL(在同一台机器上运行的自定义非 HTTPS 更新服务器),你需要修改 android/app/src/main/AndroidManifest.xml,添加更新服务器 URL 并启用 usesCleartextTraffic

🌐 If using the updates server URL (a custom non-HTTPS update server running on the same machine), you will need to modify android/app/src/main/AndroidManifest.xml to add the update server URL and enable usesCleartextTraffic:

将 Expo 运行时版本字符串键添加到 android/app/src/main/res/values/strings.xml

🌐 Add the Expo runtime version string key to android/app/src/main/res/values/strings.xml:

iOS

将文件 Podfile.properties.json 添加到 ios 目录中:

🌐 Add the file Podfile.properties.json to the ios directory:

ios/Podfile.properties.json
{ "expo.jsEngine": "hermes" }

修改 ios/Podfile,以检查 Expo 文件中的 JS 引擎配置(JSC 或 Hermes):

🌐 Modify ios/Podfile to check for the JS engine configuration (JSC or Hermes) in Expo files:

使用 Xcode,将 Expo.plist 文件添加到 ios/your-project/Supporting,并使用以下内容以匹配 app.json 的内容:

🌐 Using Xcode, add Expo.plist file to ios/your-project/Supporting with the following content to match the contents of app.json:

Expo.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>EXUpdatesCheckOnLaunch</key> <string>ALWAYS</string> <key>EXUpdatesEnabled</key> <true/> <key>EXUpdatesLaunchWaitMs</key> <integer>0</integer> <key>EXUpdatesRuntimeVersion</key> <string>1.0.0</string> <key>EXUpdatesURL</key> <string>http://localhost:3000/api/manifest</string> </dict> </plist>

下一步

🌐 Next steps