配置基本模块
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
app bundle 与 APK 的不同之处在于,您无法将其部署到设备。相反,它是一种发布格式,将您应用的所有经过编译的代码和资源包含在一个构建工件中。因此,在您上传已签名的 app Bundle 后,Google Play 就具备了构建和签署应用 APK 并将其提供给用户所需的一切。
开始使用
对于大多数应用项目而言,支持 Android App Bundle 并不费力。这是因为,包含应用基本 APK 所需代码和资源的模块是标准应用模块,当您在 Android Studio 中创建新的应用项目时,会默认获得该模块。也就是说,将下面的 application
插件应用到其 build.gradle
文件的模块,会提供应用的基本功能所需的代码和资源。
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")
}
基础模块除了为应用提供核心功能,还提供许多影响整个应用项目的 build 配置和清单条目。
基础模块 build 配置
对于大多数现有的应用项目,您无需更改基础模块 build 配置中的任何内容。但是,如果您想要在应用项目中添加功能模块,或如果您先前使用多个 APK 发布了应用,则需要注意关于基础模块 build 配置的一些方面。
版本代码和应用更新
借助 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"
}
}
在您上传 app bundle 后,Google Play 会将您的基本模块中的版本代码分配给它从该 bundle 生成的所有 APK。也就是说,当设备下载并安装您的应用时,该应用的所有拆分 APK 会共用同一版本代码。
如果您想用新的代码或资源更新应用,则必须更新应用的基本模块中的版本代码,并重新构建整个 app bundle。在您将新 app bundle 上传到 Google Play 后,Google Play 会根据基本模块指定的版本代码生成一组新的 APK。随后,当用户更新应用时,Google Play 会向他们提供当前在设备上安装的所有 APK 的更新版本。也就是说,所有已安装的 APK 都会更新为新版本代码。
其他注意事项
- 应用签名:如果您在 build 文件中包含签名信息,则仅应将其包含在基础模块的 build 配置文件中。
如需了解详情,请参阅配置 Gradle 为您的应用签名。
- 代码缩减:如果需要为整个应用项目(包括其功能模块)启用代码缩减,您必须从基本模块的 build.gradle 文件实现。也就是说,您可以在功能模块中包含自定义 ProGuard 规则,但是功能模块构建配置中的
minifyEnabled
属性将被忽略。
- 忽略
splits
块:构建 app bundle 时,Gradle 会忽略 android.splits
块中的属性。如果您想控制 app bundle 所支持的配置 APK 的类型,请改为使用 android.bundle
停用配置 APK 的类型。
- 应用版本控制:基本模块将确定整个应用项目的版本代码和版本名称。如需了解详情,请转到介绍如何管理应用更新的部分。
重新启用或停用配置 APK 的类型
默认情况下,在您构建 app 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 库实现按需语言下载
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Configure the base module\n\nAn app bundle is different from an APK in that you can't deploy one to a\ndevice. Rather, it's a publishing format that includes all your app's compiled\ncode and resources in a single build artifact. So, after you upload your\nsigned app bundle, Google Play has everything it needs to build and sign your\napp's APKs, and serve them to users.\n\nGet started\n-----------\n\nMost app projects won't require much effort to support Android App Bundles.\nThat's because the module that includes code and resources for your app's base\nAPK is the standard app module, which you get by default when you\n[create a new app project in Android Studio](/studio/projects/create-project).\nThat is, the module that applies the `application` plugin below to its\n`build.gradle` file provides the code and resources for the base functionality\nof your app. \n\n### Groovy\n\n```groovy\n// The standard application plugin creates your app's base module.\nplugins {\n id 'com.android.application'\n}\n```\n\n### Kotlin\n\n```kotlin\nplugins {\n // The standard application plugin creates your app's base module.\n id(\"com.android.application\")\n}\n```\n\nIn addition to providing the core functionality for your app, the base module\nalso provides many of the build configurations and manifest entries that\naffect your entire app project.\n\nThe base module build configuration\n-----------------------------------\n\nFor most existing app projects, you don't need to change anything in your base\nmodule's build configuration. However, if you are considering adding\nfeature modules to your app project or if you previously released your app using\nmultiple APKs, there are some aspects to the base module's build configuration\nthat you should keep in mind.\n\n### Version code and app updates\n\nWith Android App Bundles, you no longer have to manage\nversion codes for multiple APKs that you upload to Google Play. Instead, you\nmanage only one version code in the base module of your app, as shown below: \n\n // In your base module build.gradle file\n android {\n defaultConfig {\n ...\n // You specify your app's version code only in the base module.\n versionCode 5\n versionName \"1.0\"\n }\n }\n\nAfter you upload your app bundle, Google Play uses the version code in your\nbase module to assign the same version code to all the APKs it generates from\nthat bundle. That is, when a device downloads and installs your app, all split\nAPKs for that app share the same version code.\n\nWhen you want to update your app with new code or resources, you must update\nthe version code in your app's base module, and build a new, full app bundle.\nWhen you upload that app bundle to Google Play, it generates a new set of APKs\nbased on the version code the base module specifies. Subsequently, when users\nupdate your app, Google Play serves them updated versions of all APKs\ncurrently installed on the device. That is, all installed APKs are updated to\nthe new version code.\n\n### Other considerations\n\n- **App signing:** If you include signing information in your build files, you should only include it in the base module's build configuration file. For more information, see [Configure Gradle to sign your app](/studio/publish/app-signing#gradle-sign).\n- **Code shrinking:** If you want to [enable code shrinking](/studio/build/shrink-code#shrink-code) for your entire app project (including its feature modules), you must do so from the base module's build.gradle file. That is, you can include custom ProGuard rules in a feature module, but the `minifyEnabled` property in feature module build configurations is ignored.\n- **The `splits` block is ignored:** When building an app bundle, Gradle ignores properties in the `android.splits` block. If you want to control which types of configuration APKs your app bundle supports, instead use `android.bundle` to [disable types of configuration APKs](#disable_config_apks).\n- **App versioning:** The base module determines the version code and version name for your entire app project. For more information, go to the section about how to [Manage app updates](#manage_app_updates).\n\n### Re-enable or disable types of configuration APKs\n\nBy default, when you build an app bundle, it supports generating configuration\nAPKs for each set of language resources, screen density resources, and ABI\nlibraries. Using the `android.bundle` block in your base module's\n`build.gradle` file, as shown below, you can disable support for one or more\ntypes of configuration APKs: \n\n### Groovy\n\n```groovy\nandroid {\n // When building Android App Bundles, the splits block is ignored.\n // You can remove it, unless you're going to continue to build multiple\n // APKs in parallel with the app bundle\n splits {...}\n\n // Instead, use the bundle block to control which types of configuration APKs\n // you want your app bundle to support.\n bundle {\n language {\n // This property is set to true by default.\n // You can specify `false` to turn off\n // generating configuration APKs for language resources.\n // These resources are instead packaged with each base and\n // feature APK.\n // Continue reading below to learn about situations when an app\n // might change setting to `false`, otherwise consider leaving\n // the default on for more optimized downloads.\n enableSplit = false\n }\n density {\n // This property is set to true by default.\n enableSplit = true\n }\n abi {\n // This property is set to true by default.\n enableSplit = true\n }\n }\n}\n```\n\n### Kotlin\n\n```kotlin\nandroid {\n // When building Android App Bundles, the splits block is ignored.\n // You can remove it, unless you're going to continue to build multiple\n // APKs in parallel with the app bundle\n splits {...}\n\n // Instead, use the bundle block to control which types of configuration APKs\n // you want your app bundle to support.\n bundle {\n language {\n // This property is set to true by default.\n // You can specify `false` to turn off\n // generating configuration APKs for language resources.\n // These resources are instead packaged with each base and\n // feature APK.\n // Continue reading below to learn about situations when an app\n // might change setting to `false`, otherwise consider leaving\n // the default on for more optimized downloads.\n enableSplit = false\n }\n density {\n // This property is set to true by default.\n enableSplit = true\n }\n abi {\n // This property is set to true by default.\n enableSplit = true\n }\n }\n}\n```\n\n### Handling language changes\n\nGoogle Play determines which language resources to install with the app based\non the language selection in the user's device settings. Consider a user who\nchanges their default system language after already downloading your app.\nIf your app supports that language, the device requests and downloads additional\nconfiguration APKs for those language resources from Google Play.\n\nFor apps that offer a language picker inside the application and dynamically\nchange the app's language, independent of the system level language setting,\nyou must make some changes to prevent crashes due to missing resources. Either\nset the `android.bundle.language.enableSplit` property to `false`, or consider\nimplementing on-demand language downloads using the Play Core library as\ndescribed in [Download additional language resources](/guide/playcore/feature-delivery/on-demand#lang_resources)"]]