在云中配置开发构建

了解如何使用 EAS Build 配置项目的开发构建。


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

在本章中,我们将使用 EAS 为示例应用设置和配置开发构建。

🌐 In this chapter, we'll set up and configure a development build with EAS for our example app.

观看:如何配置开发构建
观看:如何配置开发构建

学习如何安装 expo-dev-client,在 eas.json 中配置构建配置文件,并使用 EAS Build 创建你的第一个开发构建。


了解开发构建

🌐 Understanding development builds

让我们首先了解什么是开发构建以及为什么我们需要它们。

🌐 Let's start by learning about what are development builds and why we need them.

A 开发版本 是我们项目的调试版本。它针对创建应用时的快速迭代进行了优化。它包含 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 Go,可以把开发构建看作是一个可定制的 Expo Go 版本,专门针对某个项目的需求。

功能开发版本Expo Go
开发阶段为移动应用开发提供类似网页的迭代速度。使用客户端应用可以快速迭代和测试 Expo SDK 项目。
协作通过共享本地运行时促进团队测试。通过设备上的二维码轻松共享项目。
第三方库支持完全支持任何 第三方库,包括需要自定义原生代码的库。限于 Expo SDK 内的库,不适合自定义原生依赖。
自定义通过 config 插件 和直接访问本地代码进行广泛的自定义。自定义有限,侧重于使用 Expo SDK 功能,而不直接修改本地代码。
预期用途适合面向商店部署的完整应用开发,提供完整的开发环境和工具。适合学习、原型设计和实验。不推荐用于生产应用。

1

安装 expo-dev-client 库

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

Terminal
npx expo install expo-dev-client

启动开发服务器

🌐 Start the development server

运行 npx expo start 来启动开发服务器

🌐 Run the npx expo start to start the development server:

Terminal
npx expo start

此命令启动Metro打包器。在终端窗口中,我们会看到二维码,接着是 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 以及应用方案
  • 开发服务器现在用于开发构建而不是 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

安装 EAS CLI

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

Terminal
npm install -g eas-cli

登录或注册一个 Expo 账户

🌐 Log in or sign up for an Expo account

如果你有 Expo 账号并且已通过 Expo CLI 登录,请跳过此步骤。如果你没有 Expo 账号,请 在此注册,然后按照下面描述的登录命令继续操作。

要登录,请运行以下命令:

🌐 To log in, run the following command:

Terminal
eas login

此命令要求我们输入 Expo 账户的电子邮件或用户名及密码以完成登录。

🌐 This command asks for our Expo account email or username and password to complete the login.

初始化并将项目链接到 EAS

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

Terminal
eas init

运行此命令时:

🌐 On running, this command:

  • 通过输入我们的 Expo 账户凭证来请求验证账户所有者,并询问我们是否想创建一个新的 EAS 项目:
Terminal
# 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 项目,并提供一个链接,我们可以在 EAS 仪表板中打开该项目:
  • 生成一个唯一的 projectId 并将此 EAS 项目链接到我们开发机器上的示例应用。
  • 修改 app.json 以包含 extra.eas.projectId 并使用创建的唯一 ID 更新其值。
app.json 中的 projectId 是什么?

eas init 运行时,它会在 app.jsonextra.eas.projectId 下为我们的项目关联一个唯一标识符。该属性的值用于在 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

为 EAS 构建配置项目

🌐 Configure project for EAS Build

要为我们的项目设置 EAS 构建,请运行以下命令:

🌐 To set up our project for EAS Build, run the following command:

Terminal
eas build:configure

运行此命令时:

🌐 On running, this command:

  • 请选择平台的提示:AndroidiOS全部。由于我们正在创建 Android 和 iOS 应用,我们选择 全部
  • 在我们项目目录的根目录中创建 eas.json,并使用以下配置:
eas.json
{ "cli": { "version": ">= 16.18.0", "appVersionSource": "remote" }, "build": { "development": { "developmentClient": true, "distribution": "internal" }, "preview": { "distribution": "internal" }, "production": { "autoIncrement": true } }, "submit": { "production": {} } }

这是新项目中 eas.json 的默认配置。它有两个作用:

🌐 This is the default configuration for eas.json in a new project. It does two things:

  • 定义当前的 EAS CLI 版本。
  • 添加三个构建配置developmentpreviewproduction
进一步探索发展概况

eas.json 是不同构建配置文件的集合。每个配置文件都采用了独特的设置,以生成特定类型的构建。这些配置文件还可以包含针对 Android 或 iOS 平台的特定设置。

目前,我们的重点是 development 配置文件,其包含以下配置:

🌐 Currently, our focus is on the development profile, which includes the following configuration:

  • developmentClient:已启用(true)以创建调试版本。它使用 expo-dev-client 库加载应用,该库提供开发工具并生成可用于设备或模拟器/模拟器安装的构建产物,同时允许将应用用于本地开发,因为它支持即时更新 JavaScript。
  • distribution:配置为 internal,表示我们希望在内部共享构建(而不是上传到应用商店)。

注意:构建提供了广泛的自定义选项,包括特定平台的设置以及跨不同构建配置扩展配置的能力。了解更多关于自定义构建配置的信息。

概括

🌐 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.

在下一章中,我们将为 Android 创建一个开发版本,将其安装到设备和模拟器上,并让它与开发服务器一起运行。

Next: 创建并运行 Android 云构建