开始使用 EAS 更新
了解如何开始在项目中配置和使用 EAS Update 所需的设置。
设置 EAS 更新可以让你即时推送用户急需的关键错误修复和改进。本指南将引导你完成在新项目或已有项目中设置 EAS 更新的过程。
🌐 Setting up EAS Update allows you to push critical bug fixes and improvements that your users need right away. This guide will walk you through the process of setting up EAS Update in a new or existing project.
信息 如果你计划将 EAS Update 与 EAS Build 一起使用,我们建议在继续此步骤之前先按照 EAS Build 设置指南 进行设置。也就是说,你可以在不使用其他 EAS 服务的情况下使用 EAS Update。
先决条件
🌐 Prerequisites
Expo 用户账户
EAS 更新可供任何拥有 Expo 账户的人使用,无论你是付费使用 EAS 还是使用免费计划。你可以在 expo.dev/signup 注册。
🌐 EAS Update is available to anyone with an Expo account, regardless of whether you pay for EAS or use the Free plan. You can sign up at expo.dev/signup.
付费订阅用户可以向更多用户发布更新,并使用更多的带宽和存储空间。了解更多关于不同计划和优惠的信息,请访问 EAS 价格。
🌐 Paid subscribers can publish updates to more users and use more bandwidth and storage. Learn more about different plans and benefits at EAS pricing.
一个 React Native 项目
还没有项目?没问题。按照本指南,你可以快速轻松地创建一个“Hello world”应用。
🌐 Don't have a project yet? No problem. It's quick and easy to create a "Hello world" app that you can use with this guide.
运行以下命令以创建一个新项目:
🌐 Run the following command to create a new project:
- npx create-expo-app my-appEAS 更新也适用于由 npx create-react-native-app、npx react-native、ignite-cli 及其他项目引导工具创建的项目。
🌐 EAS Update also works well with projects created by npx create-react-native-app, npx react-native, ignite-cli, and other project bootstrapping tools.
你的项目必须使用 Expo CLI 并扩展 Expo Metro 配置
如果你已经使用 npx expo [command] 运行你的项目(例如,如果你是用 npx create-expo-app 创建的),那么你就可以了。
🌐 If you already run your project with npx expo [command] (for example, if you created it with npx create-expo-app) then you're all set.
如果你的项目中还没有 expo 包,请通过运行以下命令来安装它,并选择使用 Expo CLI 和 Metro 配置:
🌐 If you don't have the expo package in your project yet, then install it by running the command below and opt in to using Expo CLI and Metro Config:
- npx install-expo-modules@latest如果命令失败,请参考安装 Expo 模块指南。
🌐 If the command fails, refer to the Installing Expo modules guide.
你的项目必须使用 registerRootComponent 函数,而不是 registerComponent
如果你是使用 npx create-expo-app 创建的项目,或者你的应用中根本没有调用 registerRootComponent(例如,它由 Expo Router 处理),那么一切都已经准备就绪。以下内容适用于使用其他工具创建的项目,例如 React Native Community CLI。
🌐 If you created your project with npx create-expo-app, or you don't call registerRootComponent in your app at all (for example, it's handled by Expo Router), then you are all set. The following applies to projects created with other tools, such as React Native Community CLI.
我们建议使用 EAS 更新的应用使用 Expo 的 registerRootComponent 而不是 React Native 的 registerApplication。这将确保 Expo 能够配置 React Native 加载更新中包含的资源,如图片。如果不使用 registerRootComponent,你可能会发现发布版本中无法使用这些资源。
🌐 We recommend that apps using EAS Update use Expo's registerRootComponent instead of React Native's registerApplication. This will ensure that Expo is able to configure React Native to load assets, such as images, that are included in updates. If you do not use registerRootComponent, you may find that your assets will not be available in release builds.
在使用 React Native Community CLI 创建的简单应用中,差异将如下所示:
🌐 In a simple app created with React Native Community CLI, the diff would look like this:
| 1 | import {AppRegistry} from 'react-native'; | |
| 2 | import {name as appName} from './app.json'; | |
| 1 | import {registerRootComponent} from 'expo'; | |
| 3 | 2 | import App from './App'; |
| 4 | ||
| 5 | AppRegistry.registerComponent(appName, () => App); | |
| 3 | export default registerRootComponent(App); |
在进行该更改后,更新你的 MainActivity 和 AppDelegate 使用模块名称 "main",而不是你的应用名称。
🌐 After making that change, update your MainActivity and AppDelegate to use the module name "main" instead of your app name.
1
安装最新的 EAS CLI
🌐 Install the latest EAS CLI
EAS CLI 是一个命令行应用,你可以通过终端使用它与 EAS 服务进行交互。要安装它,请运行以下命令:
🌐 EAS CLI is the command line app you will use to interact with EAS services from your terminal. To install it, run the command:
- npm install --global eas-cli你也可以使用上述命令来检查是否有新的 EAS CLI 版本可用。我们鼓励你始终保持最新版本。
🌐 You can also use the above command to check if a new version of EAS CLI is available. We encourage you to always stay up to date with the latest version.
我们建议在进行全局包安装时使用
npm而不是yarn。你也可以选择使用npx eas-cli@latest。记住,每当文档中要求使用eas时,应使用它替代。
2
登录到你的 Expo 账户
🌐 Log in to your Expo account
如果你已经使用 Expo CLI 登录了 Expo 账号,可以跳过本节中描述的步骤。如果你还没有登录,请运行以下命令进行登录:
🌐 If you are already signed in to an Expo account using Expo CLI, you can skip the steps described in this section. If you are not, run the following command to log in:
- eas login你可以通过运行 eas whoami 来检查你是否已登录。
🌐 You can check whether you are logged in by running eas whoami.
3
配置你的项目
🌐 Configure your project
在终端中导航到你的项目目录并运行以下命令:
🌐 Navigate to your project directory in your terminal and run the following command:
# Initialize your project with EAS Update- eas update:configure这个命令是做什么的?
eas update:configure 命令将使用 runtimeVersion 和 updates.url 属性更新你的 app.json 文件,如果你的项目之前没有使用任何 EAS 服务,还会添加 extra.eas.projectId 字段。
🌐 The eas update:configure command will update your app.json file with the runtimeVersion and updates.url properties, and add the extra.eas.projectId field if your project wasn't using any EAS services previously.
当你在不使用 CNG 的项目中运行 eas update:configure 时,你会看到本地项目的以下更改:
🌐 When you run eas update:configure in a project that doesn't use CNG, you'll see the following changes to your native projects:
安卓
🌐 Android
在 android/app/src/main/AndroidManifest.xml 文件中,你将看到以下新增内容:
🌐 Inside the android/app/src/main/AndroidManifest.xml file, you'll see the following additions:
<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_RUNTIME_VERSION" android:value="@string/expo_runtime_version"/>
EXPO_UPDATE_URL 值应包含你项目的 ID。
🌐 The EXPO_UPDATE_URL value should contain your project's ID.
在 android/app/src/main/res/values/strings.xml 中,你会在 resources 对象中看到 expo_runtime_version 字符串条目:
🌐 Inside android/app/src/main/res/values/strings.xml, you'll see the expo_runtime_version string entry in the resources object:
iOS
Inside ios/project-name/Supporting/Expo.plist, you'll see the following additions:
<key>EXUpdatesRuntimeVersion</key> <string>1.0.0</string> <key>EXUpdatesURL</key> <string>https://u.expo.dev/your-project-id</string>
The EXUpdatesURL value should contain your project's ID.
If you use Xcode to create project builds, make sure that theExpo.plistfile is added to your Xcode project.
4
Configure the update 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.
If you are using EAS Build, eas update:configure will set the update channel property on the preview and production profiles in eas.json. Set them manually if you use different profile names.
Example channel configuration in eas.json
{ "build": { "preview": { "channel": "preview" // ... }, "production": { "channel": "production" // ... } } }
If you are not using EAS Build, then you will need to configure the channel in app.json or in your native projects, depending on whether you use CNG. When you create a build for a different environment, you will need to modify the channel to ensure your build pulls updates from the correct channel. Learn more using EAS Update as a standalone service.
Configure update channels in app.json
If you use Continuous Native Generation (CNG), then you can configure the channel with the updates.requestHeaders property in your app.json:
{ "expo": { %%placeholder-start%%... %%placeholder-end%% "updates": { %%placeholder-start%%... %%placeholder-end%% "requestHeaders": { "expo-channel-name": "your-channel-name" } %%placeholder-start%%... %%placeholder-end%% } %%placeholder-start%%... %%placeholder-end%% } }
The configuration will be applied the next time you run npx expo prebuild.
Configure update channels in an Android native project
In AndroidManifest.xml, you will need to add replace your-channel-name with the channel that matches your project:
<meta-data android:name="expo.modules.updates.UPDATES_CONFIGURATION_REQUEST_HEADERS_KEY" android:value="{"expo-channel-name":"your-channel-name"}"/>
Configure update channels in an iOS native project
In Expo.plist, you'll need to add the following, replacing your-channel-name with the channel that matches your project:
<key>EXUpdatesRequestHeaders</key> <dict> <key>expo-channel-name</key> <string>your-channel-name</string> </dict>
If you use Xcode to create project builds, make sure that theExpo.plistfile is added to your Xcode project.
5
Create a build for the project
You need to create a build for Android or iOS. We recommend creating a build with the preview build profile first. See Create your first build on how to get started and set up Internal distribution for your device or simulator.
Once you have a build running on your device or a simulator, you are ready to send an update.
6
7
Publish an update
Publishing an update allows:
- Fixing bugs and quickly updating non-native parts of a project instead of creating a new build
- Sharing a preview version of an app using internal distribution
To publish an update with changes from your project, use the eas update command, and specify a name for the channel, a message to describe the update and if using EAS environment variables, the environment pull variables from (leave unspecified to use the .env files present in your project directory):
- eas update --channel [channel-name] --message "[message]" --environment [environment-name]发布更新是如何进行的?
当你使用 eas update 命令发布更新时,它会生成新的更新包并将其上传到 EAS 服务器。通道名称用于从其他更新分支定位正确的分支以发布新更新。这类似于 Git 提交的工作方式,每次提交都在一个 Git 分支上。
🌐 When you publish an update with the eas update command, it generates a new update bundle and uploads it to the EAS servers. The channel name is used to locate the correct branch to publish a new update from other update branches. It is similar to how Git commit works, where every commit is on a Git branch.
例如,当一个应用设置为从 preview 渠道获取更新时,你可以使用 eas update --channel preview 发布该构建的更新。这将在 preview 渠道上创建一个分支(默认称为 preview)。在后台,这个命令会运行 npx expo export 来生成一个 dist 目录并创建一个本地更新包。该更新包随后会上传到 EAS Update 服务器。
🌐 For example, when an app is set to pull updates from the preview channel, you can publish an update for that build with eas update --channel preview. This will create a branch (called preview by default) on the preview channel. Behind the scenes, this command runs npx expo export to generate a dist directory and create a local update bundle. This update bundle is uploaded to EAS Update servers.
深入了解 EAS 更新的工作原理。
8
测试更新
🌐 Test the update
在将更新上传到 EAS 更新后,你可以使用以下方法之一来测试更新:
🌐 After the update is uploaded to EAS Update, you can use one of the following methods to test the update:
- 在开发版本中使用扩展选项卡来加载更新。
- 使用 Expo Orbit 在开发版本中安装并启动更新。
- 使用 Updates API 和 应用配置 实现自定义策略,以在应用中以编程方式加载更新。
- 通过强制关闭并重新打开你的应用的发布版本最多两次来手动测试更新,以下载并应用更新。非开发版本(预览版或正式版)的更新会在应用启动并请求任何新更新时自动下载到设备后台。下载完成后,更新将在应用重新启动时应用。
有东西不工作吗?
If your app is not updating as expected, see the debugging guide for techniques to validate your configuration.
下一步
🌐 Next steps
学习通过分享 QA 和测试更新来快速迭代。
了解使用 EAS 更新时项目的不同部署模式。