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 要求
1 要求
如果你使用 npx @react-native-community/cli@latest init 创建了项目并且没有安装其他 Expo 库,你将需要在继续之前安装 Expo 模块。
安装
🌐 Installation
要开始,请安装 expo-updates:
🌐 To get started, install expo-updates:
然后,为 iOS 安装 pods:
🌐 Then, install pods for iOS:
配置 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.
修改 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.
下方示例中的
updatesURL 和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.
安卓
🌐 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,以检查 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:
在构建时配置更新渠道
🌐 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:
iOS
在 Expo.plist 中,将 EXUpdatesRequestHeaders 字典添加到现有的 <dict> 元素内:
🌐 In Expo.plist, add the EXUpdatesRequestHeaders dictionary inside the existing <dict> element:
信息 如果你之后采用 CNG,你可以在 app.json 中使用
updates.requestHeaders来配置渠道。在你的流水线中运行npx expo prebuild就会将渠道写入这些文件。查看 在 app.json 中配置更新渠道。
下一步
🌐 Next steps
- 要开始在 EAS Build 中使用 EAS Update,请参阅 EAS Update 的 入门指南。
- 有关如何使用该库的更多信息,请参见
expo-updatesAPI 参考。 - 查看如何直接使用本地构建的 EAS 更新。
- 也可以将
expo-updates与实现了 Expo Updates 协议 的自定义服务器一起使用。请参阅custom-expo-updates-server自述文件。