应用 Bundle 与 APK 不同,因为它无法直接部署到设备上。它是一种发布格式,包含您的应用所有已编译的代码和资源,全部打包在一个构建工件中。因此,在您上传已签名的应用 Bundle 后,Google Play 便拥有了构建和签署您的应用的 APK 并将其分发给用户所需的一切。
开始使用
大多数应用项目无需太多工作即可支持 Android App Bundle。这是因为包含您的应用基础 APK 代码和资源的模块是标准应用模块,当您在 Android Studio 中创建新的应用项目时,默认即可获取该模块。也就是说,对 build.gradle
文件应用下方 application
插件的模块可提供应用基本功能的代码和资源。
Groovy
// The standard application plugin creates your app's base module. plugins { id 'com.android.application' }
Kotlin
plugins { // The standard application plugin creates your app's base module. id("com.android.application") }
除了提供应用的核心功能外,基本模块还提供了许多影响整个应用项目的构建配置和清单条目。
基本模块构建配置
对于大多数现有应用项目,您无需更改基本模块的构建配置。但是,如果您正考虑为应用项目添加功能模块,或者以前曾使用多个 APK 发布您的应用,那么基本模块的构建配置有一些方面需要您注意。
版本代码和应用更新
借助 Android App Bundle,您无需再管理上传到 Google Play 的多个 APK 的版本代码。相反,您只需在应用的基本模块中管理一个版本代码,如下所示:
// In your base module build.gradle file
android {
defaultConfig {
…
// You specify your app’s version code only in the base module.
versionCode 5
versionName "1.0"
}
}
上传应用 Bundle 后,Google Play 会使用基本模块中的版本代码,将相同的版本代码分配给从该 Bundle 生成的所有 APK。也就是说,当设备下载并安装您的应用时,该应用的所有拆分 APK 都共享相同的版本代码。
当您想用新代码或资源更新应用时,必须更新应用基本模块中的版本代码,并构建一个新的完整应用 Bundle。当您将该应用 Bundle 上传到 Google Play 后,它会根据基本模块指定的版本代码生成一组新的 APK。随后,当用户更新应用时,Google Play 会向他们提供设备上当前安装的所有 APK 的更新版本。也就是说,所有已安装的 APK 都会更新到新的版本代码。
其他注意事项
- 应用签名:如果您在构建文件中包含签名信息,则应仅将其包含在基本模块的构建配置文件中。有关详情,请参阅配置 Gradle 以签署您的应用。
- 代码缩减:如果您想为整个应用项目(包括其功能模块)启用代码缩减,则必须从基本模块的 build.gradle 文件中执行此操作。也就是说,您可以在功能模块中包含自定义 ProGuard 规则,但功能模块构建配置中的
minifyEnabled
属性将被忽略。 splits
块被忽略:构建应用 Bundle 时,Gradle 会忽略android.splits
块中的属性。如果您想控制应用 Bundle 支持哪些类型的配置 APK,请改用android.bundle
来停用某些类型的配置 APK。- 应用版本控制:基本模块决定了整个应用项目的版本代码和版本名称。有关详情,请参阅关于如何管理应用更新的部分。
重新启用或停用某些类型的配置 APK
默认情况下,在构建应用 Bundle 时,它支持为每组语言资源、屏幕密度资源和 ABI 库生成配置 APK。使用基本模块的 build.gradle
文件中的 android.bundle
块(如下所示),您可以停用对一种或多种类型的配置 APK 的支持。
Groovy
android { // When building Android App Bundles, the splits block is ignored. // You can remove it, unless you're going to continue to build multiple // APKs in parallel with the app bundle splits {...} // Instead, use the bundle block to control which types of configuration APKs // you want your app bundle to support. bundle { language { // This property is set to true by default. // You can specify `false` to turn off // generating configuration APKs for language resources. // These resources are instead packaged with each base and // feature APK. // Continue reading below to learn about situations when an app // might change setting to `false`, otherwise consider leaving // the default on for more optimized downloads. enableSplit = false } density { // This property is set to true by default. enableSplit = true } abi { // This property is set to true by default. enableSplit = true } } }
Kotlin
android { // When building Android App Bundles, the splits block is ignored. // You can remove it, unless you're going to continue to build multiple // APKs in parallel with the app bundle splits {...} // Instead, use the bundle block to control which types of configuration APKs // you want your app bundle to support. bundle { language { // This property is set to true by default. // You can specify `false` to turn off // generating configuration APKs for language resources. // These resources are instead packaged with each base and // feature APK. // Continue reading below to learn about situations when an app // might change setting to `false`, otherwise consider leaving // the default on for more optimized downloads. enableSplit = false } density { // This property is set to true by default. enableSplit = true } abi { // This property is set to true by default. enableSplit = true } } }
处理语言变更
Google Play 会根据用户设备设置中的语言选择来确定要与应用一起安装的语言资源。假设用户在已下载您的应用后更改了默认系统语言。如果您的应用支持该语言,则设备会从 Google Play 请求并下载这些语言资源的其他配置 APK。
对于在应用内提供语言选择器并可动态更改应用语言(独立于系统级语言设置)的应用,您必须进行一些更改,以防止因资源缺失而导致崩溃。您可以将 android.bundle.language.enableSplit
属性设置为 false
,或者考虑使用 Play Core 库实现按需语言下载,具体请参阅下载其他语言资源。