This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

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

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


expo-updates 是一个库,允许你的应用管理对应用代码的远程更新。它与配置好的远程更新服务通信,以获取有关可用更新的信息。本指南解释了如何将现有的 React Native 项目设置为与 EAS Update 一起使用,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".

先决条件

1 要求

安装并配置 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
- yarn expo install expo-updates
- pnpm expo install expo-updates
- bun expo install expo-updates

然后,为 iOS 安装 pods:

🌐 Then, install pods for iOS:

Terminal
- npx pod-install
- yarn dlx pod-install
- pnpm dlx pod-install
- bunx 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.json 的 expo 部分。如果你使用 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.

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>

在构建时配置更新渠道

🌐 Configuring the update channel at build time

构建上的 channel 属性允许你将更新指向特定类型的构建。例如,它允许你向预览版构建发布更新,而不会影响你的生产部署。

🌐 The channel property on a build allows you to point updates at specific types of builds. For example, it allows you to publish updates to a preview build without impacting your production deployment.

这个属性最终也是在你上面修改的 AndroidManifest.xml 和 Expo.plist 文件里设置的。它通常是在构建之前设置的,所以你可以在生产和非生产构建之间切换。如果你使用 EAS Build 构建项目,channel 会在 eas.json 的构建配置里设置。然后 EAS Build 会在本地构建步骤之前应用到这些文件中。

🌐 This property is also ultimately set in the AndroidManifest.xml and Expo.plist files you modified above. It is usually set right before build, so you can switch between production and non-production builds. If you build your project with EAS Build, the channel is set in the eas.json build profile. EAS Build then applies it to these files right before the native build step.

当你在本地或使用其他服务构建时,你需要在这些文件中自己设置通道:

🌐 When you build locally or with another service, you need to set the channel yourself in these files:

安卓

🌐 Android

在 android/app/src/main/AndroidManifest.xml 中,在 <application> 元素内添加请求头 meta-data 条目:

🌐 In android/app/src/main/AndroidManifest.xml, add the request headers meta-data entry inside the <application> element:

android/app/src/main/AndroidManifest.xml
<meta-data android:name="expo.modules.updates.UPDATES_CONFIGURATION_REQUEST_HEADERS_KEY" android:value="{&quot;expo-channel-name&quot;:&quot;your-channel-name&quot;}"/>

iOS

在 Expo.plist 中,将 EXUpdatesRequestHeaders 字典添加到现有的 <dict> 元素内:

🌐 In Expo.plist, add the EXUpdatesRequestHeaders dictionary inside the existing <dict> element:

ios/project-name/Supporting/Expo.plist
<key>EXUpdatesRequestHeaders</key> <dict> <key>expo-channel-name</key> <string>your-channel-name</string> </dict>

下一步

🌐 Next steps