从 Expo Go 切换到开发构建
如何将你的 Expo Go 项目切换为使用开发构建。
要从 Expo Go 切换到开发版本,你需要按照以下步骤操作:
🌐 To switch from Expo Go to a development build, you'll need to follow the steps below:
1
安装 expo-dev-client
🌐 Install the expo-dev-client
Expo Dev Client 库包括启动器界面(如下面截图所示)、开发者菜单、用于测试 OTA 更新的扩展以及更多功能。Expo Go 应用内置了开发者菜单,因此在开发构建中你需要单独安装它。
🌐 The Expo Dev Client library includes the launcher UI (shown in the screenshots below), dev menu, extensions to test over-the-air updates, and more. The Expo Go app has the dev menu built in, and that's why you need to install it separately for a development build.
- npx expo install expo-dev-client我们建议使用
expo-dev-client来获得最佳的开发体验,但也可以在不安装此库的情况下使用开发版本。如果不使用开发客户端,请在步骤 3中使用--dev-client启动打包器。否则,它将默认在 Expo Go 中打开。
2
构建你的原生应用
🌐 Build your native app
使用 Expo Go 时,你只需要构建 JavaScript 包,但使用开发构建时,你还需要编译原生应用。使用 Expo,构建原生应用有两个部分:
🌐 With Expo Go, you only needed to build the JavaScript bundle, but with development builds you also need to compile the native app. With Expo, there are two parts to building your native app:
- 生成本地 android 和/或 ios 目录(了解更多 何时以及如何需要执行此操作)
- 使用本地构建工具来编译本地应用
一旦你构建了本地应用,除非你添加或更新包含本地代码的库,或者更改任何本地代码或配置(例如应用名称),否则不需要再次构建它。
🌐 Once you've built your native app, you won't need to build it again unless you add or update a library with native code, or change any native code or configuration, such as the app name.
当你创建一个新项目时,android 和 ios 目录会自动添加到 .gitignore 文件中,因此它们不会被提交到 Git。这确保了你可以在本地或使用 CNG 在 CI 上随时重新生成代码,而无需手动编辑原生代码。
选项 1:在本地计算机上构建
🌐 Option 1: Build on your local machine
要在本地计算机上构建原生应用,请按照 Android 和 iOS 平台的环境设置指南进行操作。这涉及设置和配置原生构建工具,例如 Android 的 Android Studio 和 iOS 的 Xcode。
🌐 To build a native app on your local machine, follow the setup your environment guides for Android and iOS platforms. This involves setting up and configuring native build tools like Android Studio for Android and Xcode for iOS.
一旦你完成所有设置,运行以下命令:
🌐 Once you have everything set up, run the following command:
- npx expo run:android- npx expo run:ios默认情况下,这将会在 Android 模拟器或 iOS 模拟器上构建并安装应用。如果你需要在手机上运行构建,请将其连接到电脑(在 Android 上,如果出现提示,请选择信任该设备并允许 USB 调试;在 iOS 上,启用开发者模式),然后使用 --device 标志运行上述命令。
🌐 By default, this will build and install the app on an Android Emulator/iOS Simulator. If you need to run the build on your phone, plug it into your computer (on Android, select trust device and allow USB debugging if prompted, and on iOS, enable developer mode) and run the above commands with the --device flag.
选项 2:基于 EAS 构建
🌐 Option 2: Build on EAS
在以下情况下使用 EAS 服务器进行构建是有用的:
🌐 Building on EAS servers is useful when:
- 你不能或不想设置本地开发环境
- 你想开发一款 iOS 应用,但没有 Mac 电脑
- 你想要与团队分享开发版本
如何在 EAS 上创建开发构建
3
启动打包器
🌐 Start the bundler
在本地构建后,npx expo run:android|ios 会自动启动打包工具。但如果你关闭了打包工具,或者正在使用之前构建的开发客户端,请使用以下命令(重新)启动 Metro 打包工具:
🌐 After building locally, npx expo run:android|ios will start the bundler automatically. But if you closed the bundler or are working on a dev client you built earlier, (re)start the Metro bundler with:
- npx expo start当你的项目安装了 expo-dev-client 时,打包工具会显示 使用开发版本,并且它显示的二维码将链接到你创建的开发版本,而不是 Expo Go。
🌐 When your project has expo-dev-client installed, the bundler will print out Using development build, and the QR code it shows will link into the development build you created instead of Expo Go.
预建
🌐 Prebuild
预构建 是 Expo 项目特有的概念。它指的是根据你的本地配置和属性生成 android 和 ios 目录的过程。
你应该什么时候运行预构建
🌐 When should you run prebuild
如果你通过 npx expo run:android|ios 构建,则需要在本地运行 prebuild,并更改任何本地依赖或配置,例如:
🌐 You will need to run prebuild locally if you are building via npx expo run:android|ios, and change any native dependencies or configuration, such as:
- 安装或更新包含本地代码的库
- 更改 应用配置(
app.json) - 升级你的 Expo SDK 版本
在这些情况下,你需要使用以下命令重建本地目录:
🌐 In these cases, you'll want to rebuild the native directories with:
- npx expo prebuild --clean然后,使用更新的原生代码重新构建你的应用,命令如下:
🌐 Then, rebuild your app with the updated native code, with:
- npx expo run:android- npx expo run:ios何时不需要运行预构建
🌐 When you don't need to run prebuild
如果未找到现有的原生文件夹,所有 Expo 构建工具(npx expo run:android|ios 和 eas build)将会自动预构建。这意味着当你第一次运行 npx expo run:android|ios 或 eas build 时,无需手动运行预构建。
🌐 All Expo build tools (npx expo run:android|ios and eas build) will prebuild automatically if no existing native folders are found. This means that there is no need to run prebuild manually when you're running npx expo run:android|ios for the first time or eas build.
了解连续本地生成(CNG)和预构建的理念及其好处