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

编辑页面

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


For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

expo-updates 是一个库,可让你的应用管理应用代码的远程更新。它会与已配置的远程更新服务通信,以获取可用更新的信息。本指南说明如何为裸 React Native 项目配置,以便与 EAS Update 一起使用;EAS Update 是一个托管的远程更新服务,提供工具来简化 expo-updates 库的安装和配置。

你在项目中使用 Continuous Native Generation(CNG)吗?

你可能在查看错误的指南。要在使用 CNG 的项目中使用 expo-updates,请参阅 EAS Update “Get started”

先决条件

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

安装

开始之前,先安装 expo-updates

Terminal
npx expo install expo-updates

然后,安装 iOS 的 pods:

Terminal
npx pod-install

配置 expo-updates 库

按照以下各节中的 diff 进行相应更改,以便在项目中配置 expo-updates

JavaScript 和 JSON

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

Terminal
eas update:configure

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

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

如果你想改为设置一个自定义 expo-updates 服务器,请在 app.json 中将你的 URL 添加到 updates.url

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):

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

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

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

iOS

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

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

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

使用 Xcode,将 Expo.plist 文件添加到 ios/your-project/Supporting,内容如下,以与 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>

后续步骤