为 Android 模拟器和设备构建 APK
了解如何在使用 EAS Build 时为 Android 模拟器和设备配置和安装 .apk。
使用 EAS Build 构建 Android 应用时使用的默认文件格式是 安卓应用包 (AAB/.aab)。此格式针对分发到 Google Play 商店进行了优化。但是,AAB 无法直接安装在你的设备上。要将构建直接安装到 Android 设备或模拟器,你需要构建 安卓包 (APK/.apk)。
¥The default file format used when building Android apps with EAS Build is an Android App Bundle (AAB/.aab). This format is optimized for distribution to the Google Play Store. However, AABs can't be installed directly on your device. To install a build directly to your Android device or emulator, you need to build an Android Package (APK/.apk) instead.
配置配置文件来构建 APK
¥Configuring a profile to build APKs
要生成 .apk,请通过在构建配置文件中添加以下属性之一来修改 eas.json:
¥To generate an .apk, modify the eas.json by adding one of the following properties in a build profile:
-
developmentClient
至true
(默认)¥
developmentClient
totrue
(default) -
distribution
至internal
¥
distribution
tointernal
-
android.buildType
至apk
¥
android.buildType
toapk
-
android.gradleCommand
到:app:assembleRelease
、:app:assembleDebug
或任何其他生成 .apk 的 gradle 命令¥
android.gradleCommand
to:app:assembleRelease
,:app:assembleDebug
or any other gradle command that produces .apk
{
"build": {
"preview": {
"android": {
"buildType": "apk"
}
},
"preview2": {
"android": {
"gradleCommand": ":app:assembleRelease"
}
},
"preview3": {
"developmentClient": true
},
"preview4": {
"distribution": "internal"
},
"production": {}
}
}
现在你可以使用以下命令运行构建:
¥Now you can run your build with the following command:
-
eas build -p android --profile preview
请记住,你可以根据自己的喜好命名配置文件。我们将该配置文件命名为 preview
。但是,你可以将其称为 local
、emulator
或任何对你来说最有意义的名称。
¥Remember that you can name the profile whatever you like. We named the profile preview
. However, you can call it local
, emulator
, or whatever makes the most sense for you.
安装你的版本
¥Installing your build
模拟器(虚拟设备)
¥Emulator (virtual device)
如果你之前尚未安装或运行 Android 模拟器,请先按照 Android Studio 模拟器指南 操作,然后再继续。
¥If you haven't installed or run an Android Emulator before, follow the Android Studio emulator guide before proceeding.
构建完成后,CLI 将提示你自动下载并将其安装到 Android 模拟器上。出现提示时,按 Y 直接安装到模拟器上。
¥Once your build is completed, the CLI will prompt you to automatically download and install it on the Android Emulator. When prompted, press Y to directly install it on the emulator.
如果你有多个版本,你还可以随时运行 eas build:run
命令来下载特定版本并将其自动安装在 Android 模拟器上:
¥In case you have multiple builds, you can also run the eas build:run
command at any time to download a specific build and automatically install it on the Android Emulator:
-
eas build:run -p android
该命令还显示项目的可用构建列表。你可以从此列表中选择要安装在模拟器上的版本。列表中的每个构建都有一个构建 ID、自构建创建以来经过的时间、构建号、版本号和 git 提交信息。如果项目有无效构建,该列表还会显示无效构建。
¥The command also shows a list of available builds of your project. You can select the build to install on the emulator from this list. Each build in the list has a build ID, the time elapsed since the build creation, the build number, the version number, and the git commit information. The list also displays invalid builds if a project has any.
例如,下图列出了项目的构建:
¥For example, the image below lists the build of a project:
构建安装完成后,它将出现在主屏幕上。如果是开发版本,请打开终端窗口并通过运行命令 npx expo start
启动开发服务器。
¥When the build's installation is complete, it will appear on the home screen. If it's a development build, open a terminal window and start the development server by running the command npx expo start
.
运行最新版本
¥Running the latest build
将 --latest
标志传递给 eas build:run
命令以在 Android 模拟器上下载并安装最新版本:
¥Pass the --latest
flag to the eas build:run
command to download and install the latest build on the Android Emulator:
-
eas build:run -p android --latest
物理设备
¥Physical device
直接下载到设备
¥Download directly to the device
-
构建完成后,从构建详细信息页面或
eas build
完成时提供的链接将 URL 复制到 APK。¥Once your build is completed, copy the URL to the APK from the build details page or the link provided when
eas build
is done. -
将该 URL 发送到你的设备。也许通过电子邮件?由你决定。
¥Send that URL to your device. Maybe by email? Up to you.
-
在你的设备上打开 URL,安装 APK 并运行它。
¥Open the URL on your device, install the APK and run it.
使用 adb
安装
¥Install with adb
-
安装亚行(如果你尚未安装)。
¥Install adb if you don't have it installed already.
-
将你的设备连接到计算机和 在你的设备上启用 adb 调试(如果尚未连接)。
¥Connect your device to your computer and enable adb debugging on your device if you haven't already.
-
构建完成后,从构建详细信息页面或
eas build
完成时提供的链接下载 APK。¥Once your build is completed, download the APK from the build details page or the link provided when
eas build
is done. -
运行
adb install path/to/the/file.apk
。¥Run
adb install path/to/the/file.apk
. -
在你的设备上运行该应用。
¥Run the app on your device.