了解如何使用 EAS Build 配置项目的开发构建。
在本章中,我们将使用 EAS 为示例应用设置和配置开发构建。
¥In this chapter, we'll set up and configure a development build with EAS for our example app.
¥Understanding development builds
让我们首先了解什么是开发构建以及为什么我们需要它们。
¥Let's start by learning about what are development builds and why we need them.
开发构建 是我们项目的调试版本。它针对创建应用时的快速迭代进行了优化。它包含 expo-dev-client
库,提供强大且完整的开发环境。此设置允许我们根据需要集成任何原生库或更改 原生目录 内的代码。
¥A development build is a debug version of our project. It is optimized for quick iterations when creating an app. It contains the expo-dev-client
library, which offers a robust and complete development environment. This setup allows us to integrate any native library or change code inside the native directories as required.
¥Key highlights
特性 | 开发构建 | Expo |
---|---|---|
开发阶段 | 为移动应用开发提供类似 Web 的迭代速度。 | 允许使用客户端应用快速迭代和测试 Expo SDK 项目。 |
合作 | 通过共享原生运行时促进团队测试。 | 通过设备上的二维码轻松共享项目。 |
第三方库支持 | 完全支持任何 第三方库,包括那些需要自定义原生代码的。 | 仅限于 Expo SDK 内的库,不适合自定义原生依赖。 |
定制化 | 使用 配置插件 进行广泛的定制并直接访问原生代码。 | 有限的定制,重点关注 Expo SDK 功能,无需直接修改原生代码。 |
有可能的使用 | 非常适合针对商店部署的成熟应用开发,提供完整的开发环境和工具。 | 非常适合学习、原型设计和实验。不建议用于生产应用。 |
1
¥Install expo-dev-client library
要初始化我们的项目以进行开发构建,请在项目目录中执行 cd
并运行以下命令来安装库:
¥To initialize our project for a development build, let's cd
inside our project directory and run the following command to install the library:
-
npx expo install expo-dev-client
¥Start the development server
运行 npx expo start
来启动 开发服务器:
¥Run the npx expo start
to start the development server:
-
npx expo start
此命令启动 Metro 打包程序。在终端窗口中,我们看到 QR 码,后跟 Metro waiting on...
和清单 URL:
¥This command starts the metro bundler. In the terminal window, we see the QR code followed by the Metro waiting on...
and a manifest URL:
让我们注意安装 expo-dev-client
库的变化:
¥Let's notice the changes installing the expo-dev-client
library:
清单 URL 包含 expo-development-client
以及应用方案
¥The manifest URL contains expo-development-client
along with the app scheme
开发服务器现在用于开发构建(而不是 Expo Go)。
¥The development server now operates for a development build (instead of Expo Go).
由于我们没有在我们的设备或模拟器/模拟器上安装开发版本,因此我们还无法运行我们的项目。
¥Since we do not have a development build installed on one of our devices or an emulator/simulator, we can't run our project yet.
2
¥Initialize a development build
¥Install EAS CLI
我们需要安装 EAS 命令行接口 (CLI) 工具作为本地计算机的全局依赖。运行以下命令:
¥We need to install the EAS Command Line Interface (CLI) tool as a global dependency on our local machine. Run the following command:
-
npm install -g eas-cli
¥Log in or sign up for an Expo account
如果你有 Expo 账户并使用 Expo CLI 登录,请跳过此步骤。如果你没有 Expo 账户,请 在此注册 并继续执行下述登录命令。
¥If you have an Expo account and are signed in using Expo CLI, skip this step. If you don't have an Expo account, sign up here and proceed with the login command described below.
要登录,请运行以下命令:
¥To log in, run the following command:
-
eas login
此命令要求提供我们的 Expo 账户电子邮件或用户名和密码以完成登录。
¥This command asks for our Expo account email or username and password to complete the login.
¥Initialize and link the project to EAS
对于任何新项目,第一步是初始化并将其链接到 EAS 服务器。运行以下命令:
¥For any new project, the first step is to initialize and link it to the EAS servers. Run the following command:
-
eas init
运行时,此命令:
¥On running, this command:
通过输入我们的 Expo 账户凭据来请求验证账户所有者,并询问我们是否要创建新的 EAS 项目:
¥Requests verification of the account owner by entering our Expo account credentials and asks if we want to create a new EAS project:
# Output after running eas init
✔ Which account should own this project? > your-username
✔ Would you like to create a project for @your-username/sticker-smash? … yes
✔ Created @your-username/sticker-smash
✔ Project successfully linked (ID: XXXX-XX-XX-XXXX) (modified app.json)
创建 EAS 项目并提供该项目的链接,我们可以在 Expo 仪表板中打开该链接:
¥Creates EAS project and provides a link to that project which we can open in the Expo dashboard:
生成唯一的 projectId
并将该 EAS 项目链接到我们开发计算机上的示例应用。
¥Generates a unique projectId
and links this EAS project to the example app on our development machine.
修改 app.json 以包含 extra.eas.projectId
并使用创建的唯一 ID 更新其值。
¥Modifies app.json to include extra.eas.projectId
and updates its value with the unique ID created.
projectId
in app.json?当 eas init
运行时,它会在 extra.eas.projectId
下的 app.json 中关联我们项目的唯一标识符。该属性的值用于标识我们在 EAS 服务器上的项目。
¥When eas init
runs, it associates a unique identifier for our project in app.json under extra.eas.projectId
. The value of this property is used to identify our project on EAS servers.
{
"extra": {
"eas": {
"projectId": "0cd3da2d-xxx-xxx-xxx-xxxxxxxxxx"
}
}
}
3
¥Configure project for EAS Build
要为 EAS Build 设置我们的项目,请运行以下命令:
¥To set up our project for EAS Build, run the following command:
-
eas build:configure
运行时,此命令:
¥On running, this command:
提示选择平台:Android、iOS 或全部。由于我们正在创建 Android 和 iOS 应用,因此我们选择“全部”。
¥Prompts to select a platform: Android, iOS, or All. Since we are creating Android and iOS apps, let's select All.
使用以下配置在项目目录的根目录中创建 eas.json:
¥Creates eas.json in the root of our project's directory with the following configuration:
{
"cli": {
"version": ">= 8.0.0"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal"
},
"preview": {
"distribution": "internal"
},
"production": {}
},
"submit": {
"production": {}
}
}
这是新项目中 eas.json 的默认配置。它做了两件事:
¥This is the default configuration for eas.json in a new project. It does two things:
定义当前 EAS CLI 版本。
¥Defines the current EAS CLI version.
添加三个 建立档案:development
、preview
和 production
。
¥Adds three build profiles: development
, preview
, and production
.
eas.json 是不同构建配置文件的集合。每个配置文件都采用不同的配置进行定制,以生成特定的构建类型。这些配置文件还可以包括 Android 或 iOS 平台特定的设置。
¥eas.json is a collection of different build profiles. Each profile is tailored with distinct configurations to produce specific build types. These profiles can also include platform-specific settings for Android or iOS.
目前,我们的重点是 development
配置文件,其中包括以下配置:
¥Currently, our focus is on the development
profile, which includes the following configuration:
developmentClient
:启用 (true
) 以创建调试版本。它使用 expo-dev-client
库加载应用,该库提供开发工具并生成用于设备或模拟器/模拟器安装的构建工件,并允许使用该应用进行本地开发,因为它支持动态更新 JavaScript。
¥developmentClient
: Enabled (true
) for creating a debug build. It loads the app using the expo-dev-client
library, which provides development tools and generates a build artifact for device or emulator/simulator installation and allows using the app for local development as it supports updating the JavaScript on the fly.
distribution
:配置为 internal
表示我们希望在内部共享构建(而不是将其上传到应用商店)。
¥distribution
: Configured as internal
to indicate that we want to share the build internally (instead of uploading it on app stores).
注意:构建提供了广泛的自定义选项,包括特定于平台的设置以及跨不同构建配置文件扩展配置的能力。了解有关 自定义构建配置文件 的更多信息。
¥Note: Builds offer extensive customization options, including platform-specific settings and the ability to extend configurations across different build profiles. Learn more about customizing build profiles.
¥Summary
Chapter 1: Configure development build in cloud
We successfully used the EAS CLI to initialize, and configure our project, link it to EAS servers, and prepare a development build.
In the next chapter, let's create a development build for Android, install it on a device and an emulator, and get it running with the development server.