在现有 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:
- npx expo install expo-updates然后,为 iOS 安装 pods:
🌐 Then, install pods for iOS:
- 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.
- 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.
下方示例中的
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.
| 1 | 1 | "expo": { |
| 2 | 2 | "name": "MyApp", |
| 3 | "updates": { | |
| 4 | "url": "https://u.expo.dev/[your-project-id]" | |
| 5 | } | |
| 3 | "updates": { | |
| 4 | "url": "http://localhost:3000/api/manifest" | |
| 5 | } | |
| 6 | 6 | } |
| 7 | 7 | } |
安卓
🌐 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:
{ "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:
<?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
- 要开始在 EAS Build 中使用 EAS Update,请参阅 EAS Update 的 入门指南。
- 有关如何使用该库的更多信息,请参见
expo-updatesAPI 参考。 - 查看如何直接使用本地构建的 EAS 更新。
- 也可以将
expo-updates与实现了 Expo Updates 协议 的自定义服务器一起使用。请参阅custom-expo-updates-server自述文件。