从 Expo 转换到开发版本
如何迁移你的 Expo Go 项目以使用开发版本。
要从 Expo Go 迁移到开发版本,你需要按照以下步骤操作:
¥To migrate from Expo Go to a development build, you'll need to follow the steps below:
1
Install the expo-dev-client
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
We recommend using the
expo-dev-client
for the best development experience, but it is possible to use development builds without installing this library. If not using the dev client, in Step 3, start the bundler with--dev-client
. Otherwise, it will default to opening in Expo Go.
2
Build your native app
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:
- Generate the native android and/or ios directories (read more on when and how you'll need to do this)
- Use native build tools to compile the native app(s)
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.
After generating your android and/or ios directories, make sure you add them to .gitignore instead of checking them into Git. This ensures you can always regenerate the code locally or on CI when needed and never have to edit native code manually.
Option 1: Build on your local machine
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
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.
Option 2: Build on EAS
Building on EAS servers is useful when:
- You can't or don't want to set up your local development environment
- You want to build an iOS app but don't own a Mac
- You want to share the development builds with your team
如何在 EAS 上创建开发版本
3
Start the bundler
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
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 目录的过程。
¥Prebuild is a concept unique to Expo projects. It refers to the process of generating the android and ios directories based on your local configuration and properties.
何时应该运行预构建
¥When should you run prebuild
如果你通过 npx expo run:android|ios
构建,则需要在本地运行预构建,并更改任何原生依赖或配置,例如:
¥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:
-
安装或更新包含原生代码的库
¥Installing or updating a library containing native code
-
更改 应用配置(
app.json
)¥Changing app config(
app.json
) -
升级你的 Expo SDK 版本
¥Upgrading your Expo SDK version
在这种情况下,你需要使用以下方法重建原生目录:
¥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) 和预构建的理念和优势。