在现有 React Native 项目中安装 expo-updates
了解如何在现有的 React Native 项目中安装和配置 expo-updates。
expo-updates 是一个库,可让你的应用管理对应用代码的远程更新。它与配置的远程更新服务通信以获取有关可用更新的信息。本指南介绍如何设置一个裸 React Native 项目以用于 EAS 更新,EAS 更新 是一种托管远程更新服务,其中包括用于简化 expo-updates 库安装和配置的工具。
¥expo-updates is a library that enables your app to manage remote updates to your application code. It communicates with the configured remote update service to get information about available updates. This guide explains how to set up a bare React Native project for use with EAS Update, a hosted remote update service that includes tools to simplify installation and configuration of the expo-updates library.
Do you use Continuous Native Generation (CNG) in your project?
You may be reading the wrong guide. To use expo-updates in a project that uses CNG, see EAS Update "Get started".
Prerequisites
The expo package must be installed and configured. If you created your project with npx @react-native-community/cli@latest init and do not have any other Expo libraries installed, you will need to install Expo modules before proceeding.
Installation
To get started, install expo-updates:
- npx expo install expo-updatesThen, install pods for iOS:
- npx pod-installConfiguring expo-updates library
Apply the changes from the diffs from the following sections to configure expo-updates in your project.
JavaScript and JSON
Run eas update:configure to set the updates URL and projectId in app.json.
- eas update:configureModify 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.
The example
updatesURL andprojectIdshown below are used with EAS Update. The EAS CLI sets this URL correctly for the EAS Update service when runningeas update:configure.
If you want to set up a custom expo-updates server instead, add your URL to updates.url in app.json.
{ "name": "MyApp", "displayName": "MyApp", "expo": { "name": "MyApp", ... "updates": { - "url": "https://u.expo.dev/[your-project-id]" + "url": "http://localhost:3000/api/manifest" } } }
Android
Modify android/app/build.gradle to check for the JS engine configuration (JSC or Hermes) in Expo files:
Modify android/app/src/main/AndroidManifest.xml to add the expo-updates configuration XML so that it matches the contents of app.json:
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:
<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme" + android:usesCleartextTraffic="true" > - <meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="https://u.expo.dev/[your-project-id]"/> + <meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="http://localhost:3000/api/manifest"/> </application>
Add the Expo runtime version string key to android/app/src/main/res/values/strings.xml:
iOS
Add the file Podfile.properties.json to the ios directory:
{ "expo.jsEngine": "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 Update 与 EAS Build 结合使用,请参阅 EAS Update 开始使用。
¥To start using EAS Update with EAS Build, see the EAS Update Get started.
-
有关如何使用该库的更多信息,请参阅
expo-updatesAPI 参考。¥See
expo-updatesAPI reference for more information on how to use the library. -
了解如何使用 直接使用本地版本进行 EAS 更新。
¥See how to use EAS Update with a local build directly.
-
也可以将
expo-updates与实现 Expo 更新协议 的自定义服务器一起使用。参见custom-expo-updates-server自述文件。¥It is also possible to use
expo-updateswith a custom server that implements the Expo Updates protocol. Seecustom-expo-updates-serverREADME.