为 Android 模拟器和设备构建 APK
了解如何在使用 EAS Build 时为 Android 模拟器和设备配置和安装 .apk。
使用 EAS Build 构建 Android 应用时,默认的文件格式是 Android App Bundle(AAB/.aab)。这种格式已针对 Google Play 商店的分发进行了优化。然而,AAB 无法直接安装到设备上。要将构建直接安装到 Android 设备或模拟器上,需要改为构建 Android Package(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(默认)distribution到internalandroid.buildType到apkandroid.gradleCommand到:app:assembleRelease、:app:assembleDebug、:app:assembleDebugOptimized(适用于 SDK 54 及更高版本),或任何其他生成 .apk 的 Gradle 命令
{ "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 模拟器指南 操作。
一旦构建完成,CLI 将提示你自动下载并安装到 Android 模拟器上。提示出现时,按 Y 就可以直接安装到模拟器上。
如果你有多个版本,你也可以随时运行 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完成时提供的链接中复制 APK 的 URL。 - 把那个网址发送到你的设备上。也许可以通过电子邮件?随你决定。
- 在你的设备上打开 URL,安装 APK 并运行它。
使用 adb 安装
🌐 Install with adb