首页指南参考教程

本地应用开发

了解如何在使用 Expo 时在本地编译和构建应用。


要使用你的计算机在本地将项目构建为应用,你必须在测试调试版本或创建生产版本以将其提交到应用商店之前手动生成原生代码。你可以通过两种方式在本地构建应用。本指南简要介绍了创建此工作流程所需的方法和其他指南的参考。

¥To build your project into an app locally using your machine, you have to manually generate native code before testing the debug build or creating a production build for it to submit to the app store. There are two ways you can build your app locally. This guide provides a brief introduction to both methods and references to other guides that are necessary to create this workflow.

先决条件

¥Prerequisites

要按照本指南中描述的步骤操作,请参阅下面有关如何设置开发环境的说明。

¥To follow the steps described in this guide, see the instructions below on how to set your development environment.

Android

要在 macOS 上为 Android 设置开发环境,你需要安装 Node.js、Watchman、JDK 和 Android Studio。

¥To set up your development environment on macOS for Android, you will install Node.js, Watchman, JDK, and Android Studio.

先决条件

¥Prerequisites

使用包管理器(例如 Homebrew)安装以下依赖。

¥Use a package manager such as Homebrew to install the following dependencies.

安装依赖

¥Install dependencies

1

使用 版本管理工具(例如 nvmvolta)安装 Node.js 的 LTS 版本。

¥Install the LTS release of Node.js using a version management tool such as nvm or volta.

2

安装守望者 使用 Homebrew 等工具:

¥Install Watchman using a tool such as Homebrew:

Terminal
brew install watchman

3

使用 Homebrew 安装名为 Azul Zulu 的 OpenJDK 发行版。该发行版提供适用于 M1 和 Intel Mac 的 JDK。

¥Install OpenJDK distribution called Azul Zulu using Homebrew. This distribution offers JDKs for both M1 and Intel Macs.

我们推荐 JDK 17。在终端窗口中运行以下命令来安装它:

¥We recommend JDK 17. Run the following commands in a terminal window to install it:

Terminal
brew install --cask zulu@17

安装 JDK 后,在 ~/.bash_profile 中添加 JAVA_HOME 环境变量(如果使用 Zsh,则在 ~/.zshrc 中):

¥After you install the JDK, add the JAVA_HOME environment variable in ~/.bash_profile (or ~/.zshrc if you use Zsh):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home

我们推荐 JDK 11(更高版本的 JDK 可能会遇到问题)。在终端窗口中运行以下命令来安装它:

¥We recommend JDK 11 (you may encounter problems with higher JDK versions). Run the following commands in a terminal window to install it:

Terminal
brew install --cask zulu@11

安装 JDK 后,在 ~/.bash_profile 中添加 JAVA_HOME 环境变量(如果使用 Zsh,则在 ~/.zshrc 中):

¥After you install the JDK, add the JAVA_HOME environment variable in ~/.bash_profile (or ~/.zshrc if you use Zsh):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home

安装和配置 Android Studio

¥Install and configure Android Studio

Android Studio 允许安装 Android SDK、Android SDK 平台和 Android 虚拟设备。他们需要设置 Android 开发环境。按照 Android Studio 模拟器上的说明 安装这些工具。

¥Android Studio allows installing Android SDK, Android SDK Platform, and Android Virtual Device. They are required to set up the Android development environment. Follow the instructions on the Android Studio Emulator to install these tools.

要在 Windows 上为 Android 设置开发环境,你需要安装 Node.js、JDK 和 Android Studio。

¥To set up your development environment on Windows for Android, you will install Node.js, JDK, and Android Studio.

先决条件

¥Prerequisites

使用包管理器(例如 Chocolatey)安装以下依赖。

¥Use a package manager such as Chocolatey to install the following dependencies.

安装依赖

¥Install dependencies

1

使用 Chocolatey 安装 LTS 版本的 Node.js。如果你希望能够在不同版本之间切换,请使用版本管理器(例如 nvm-windows)安装 Node.js

¥Install the LTS version of Node.js using Chocolatey. If you want to be able to switch between different versions, use a version manager such as nvm-windows to install Node.js

Terminal
choco install -y nodejs

2

安装 Java SE 开发工具包 (JDK)

¥Install Java SE Development Kit (JDK):

对于 SDK 50 及更高版本,请安装 JDK 版本 17。

¥For SDK 50 and above, install JDK version 17.

Terminal
choco install -y microsoft-openjdk17

对于 SDK 49 及更低版本,请安装 JDK 版本 11。

¥For SDK 49 and below, install JDK version 11.

Terminal
choco install -y microsoft-openjdk11

安装和配置 Android Studio

¥Install and configure Android Studio

Android Studio 允许安装 Android SDK、Android SDK 平台和 Android 虚拟设备。他们需要设置 Android 开发环境。按照 Android Studio 模拟器上的说明 安装这些工具。

¥Android Studio allows installing Android SDK, Android SDK Platform, and Android Virtual Device. They are required to set up the Android development environment. Follow the instructions on the Android Studio Emulator to install these tools.

要在 Linux for Android 上设置开发环境,你需要安装 Node.js、Watchman、JDK 和 Android Studio。

¥To set up your development environment on Linux for Android, you will install Node.js, Watchman, JDK, and Android Studio.

安装依赖

¥Install dependencies

1

按照 适用于你的 Linux 发行版的说明 安装 Node.js LTS。

¥Install Node.js LTS by following instructions for your Linux distribution.

2

按照 Watchman 文档 中的说明从源代码编译和安装它。

¥Follow instructions from Watchman documentation to compile and install it from the source.

3

安装 Java SE 开发工具包 (JDK)

¥Install Java SE Development Kit (JDK):

对于 SDK 50 及更高版本,请安装 JDK 版本 17。你可以从 AdoptOpenJDK 或你的系统打包程序下载并安装 OpenJDK

¥For SDK 50 and above, install JDK version 17. You can download and install OpenJDK from AdoptOpenJDK or your system packager.

对于 SDK 49 及更低版本,请安装 JDK 版本 11。你可以从 AdoptOpenJDK 或你的系统打包程序下载并安装 OpenJDK

¥For SDK 49 and below, install JDK version 11. You can download and install OpenJDK from AdoptOpenJDK or your system packager.

安装和配置 Android Studio

¥Install and configure Android Studio

Android Studio 允许安装 Android SDK、Android SDK 平台和 Android 虚拟设备。他们需要设置 Android 开发环境。按照 Android Studio 模拟器上的说明 安装这些工具。

¥Android Studio allows installing Android SDK, Android SDK Platform, and Android Virtual Device. They are required to set up the Android development environment. Follow the instructions on the Android Studio Emulator to install these tools.

iOS
需要 macOS 才能使用 iOS 的原生代码构建项目。你可以创建 开发构建 以在 iOS 设备上安装该版本。

要在 macOS for iOS 上设置开发环境,你需要安装 Node.js、Watchman、Xcode 和 CocoaPods。

¥To set up your development environment on macOS for iOS, you'll need to install Node.js, Watchman, Xcode, and CocoaPods.

先决条件

¥Prerequisites

使用包管理器(例如 Homebrew)安装以下依赖。

¥Use a package manager such as Homebrew to install the following dependencies.

安装依赖

¥Install dependencies

1

使用 版本管理工具(例如 nvmvolta)安装 Node.js 的 LTS 版本。

¥Install the LTS release of Node.js using a version management tool such as nvm or volta.

2

安装守望者 使用 Homebrew 等工具:

¥Install Watchman using a tool such as Homebrew:

Terminal
brew install watchman

3

使用 应用商店 安装 Xcode。该工具还将安装 iOS 模拟器。你还需要 安装命令行工具

¥Install Xcode using the App Store. This tool will also install the iOS Simulator. You will also need to install Command Line Tools.

4

安装 CocoaPods。你可以使用最新 macOS 版本附带的 Ruby 版本进行安装。

¥Install CocoaPods. You can install using the version of Ruby that ships with the latest macOS version.

本地应用编译

¥Local app compilation

要在本地构建项目,你可以使用 Expo CLI 中的编译命令来生成 android 和 ios 目录:

¥To build your project locally you can use compile commands from Expo CLI which generates the android and ios directories:

Terminal
# Build native Android project
npx expo run:android
# Build native iOS project
npx expo run:ios

上述命令使用本地安装的 Android SDK 或 Xcode 将你的项目编译为应用的调试版本。

¥The above commands compile your project, using your locally installed Android SDK or Xcode, into a debug build of your app.

  • 这些编译命令最初运行 npx expo prebuild 以在构建之前生成原生目录(android 和 ios)(如果它们尚不存在)。如果它们已经存在,这将被跳过。

    ¥These compilation commands initially run npx expo prebuild to generate native directories (android and ios) before building, if they do not exist yet. If they already exist, this will be skipped.

  • 你还可以添加 --device 标志来选择运行应用的设备 - 你可以选择物理连接的设备或模拟器/模拟器。

    ¥You can also add the --device flag to select a device to run the app on — you can select a physically connected device or emulator/simulator.

  • 你可以传入 --variant release (Android) 或 --configuration Release (iOS) 来构建 你的应用的生产版本

    ¥You can pass in --variant release (Android) or --configuration Release (iOS) to build a production version of your app.

要在第一次构建后修改项目的配置或原生代码,你必须重新构建项目。再次运行 npx expo prebuild 会将更改分层到现有文件之上。构建后也可能会产生不同的结果。

¥To modify your project's configuration or native code after the first build, you will have to rebuild your project. Running npx expo prebuild again layers the changes on top of existing files. It may also produce different results after the build.

为了避免这种情况,请将原生目录添加到项目的 .gitignore 并使用 npx expo prebuild --clean 命令。这确保了项目始终受到管理,并且 --clean 将在重新生成现有目录之前删除它们。你可以使用 应用配置 或创建 配置插件 来修改原生目录内的项目配置或代码。

¥To avoid this, add native directories to the project's .gitignore and use npx expo prebuild --clean command. This ensures that the project is always managed, and the --clean flag will delete existing directories before regenerating them. You can use app config or create a config plugin to modify your project's configuration or code inside the native directories.

要了解有关编译和预构建如何工作的更多信息,请参阅以下指南:

¥To learn more about how compilation and prebuild works, see the following guides:

使用 Expo CLI 进行编译

Learn how Expo CLI uses run commands to compile your app locally, arguments you can pass to the CLI and more.

预建

了解 Expo CLI 在编译项目之前如何生成项目的原生代码。

使用 expo-dev-client 进行本地构建

¥Local builds with expo-dev-client

如果你将 expo-dev-client 安装到项目中,那么项目的调试版本将包括 expo-dev-client UI 和工具,我们将这些版本称为开发版本。

¥If you install expo-dev-client to your project, then a debug build of your project will include the expo-dev-client UI and tooling, and we call these development builds.

Terminal
npx expo install expo-dev-client

要创建开发版本,你可以使用 本地应用编译 命令 (npx expo run:[android|ios]),这将创建调试版本并启动开发服务器。

¥To create a development build, you can use local app compilation commands (npx expo run:[android|ios]) which will create a debug build and start the development server.

使用 EAS 进行本地构建

¥Local builds with EAS

在你的基础架构上运行构建

Learn how to run EAS Build on your custom infrastructure or locally on your machine with the --local flag.

Expo 中文网 - 粤ICP备13048890号