了解如何在使用 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
你需要安装并设置 Android Studio 和 Xcode 以在本地机器上编译和运行 Android 和 iOS 项目。请参阅以下内容,了解如何设置这些工具:
¥You need to install and set up Android Studio and Xcode to compile and run Android and iOS projects on your local machine. See the following on how to set up these tools:
¥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:
# 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:
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.
-
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.
¥Local builds with EAS
Learn how to run EAS Build on your custom infrastructure or locally on your machine with the --local
flag.