設定基礎模組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
應用程式套件與 APK 的差異為無法部署至裝置。相反地,這是一種發布格式,當中包含了一個建構成果,其中具有應用程式的所有編譯程式碼和資源。因此,在您上傳已簽署的應用程式套件後,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")
}
基本模組可支援應用程式的核心功能,還會提供許多建構設定與資訊清單項目,影響整個應用程式專案。
基本模組建構設定
大部分現有的應用程式專案都不需要變更基本模組的建構設定。不過,如果您打算在應用程式專案中新增功能模組,或者之前發行的應用程式使用了多個 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"
}
}
在您上傳應用程式套件後,Google Play 會使用基本模組中的版本號碼,將同一個版本號碼指派給該套件產生的所有 APK。也就是說,裝置下載及安裝應用程式時,該應用程式的所有分割 APK 都會使用相同的版本號碼。
若想要以新的程式碼或資源更新應用程式,您必須更新應用程式基本模組中的版本號碼,並建構一個新的完整應用程式套件。當您將該應用程式套件上傳至 Google Play 時,將會根據基本模組指定的版本號碼,產生一組新的 APK。之後當使用者更新應用程式時,Google Play 就會根據裝置上目前安裝的 APK 提供更新後的版本。也就是說,系統會將所有已安裝的 APK 更新為最新的版本號碼。
其他考量
- 應用程式簽署:如果建構檔案中具有簽章資訊,則建議僅將其列在基本模組的建構設定檔中。詳情請參閱「設定 Gradle 以簽署應用程式」。
- 程式碼縮減:如果您想為整個應用程式專案 (包括其功能模組) 啟用程式碼縮減功能,則必須從基本模組的 build.gradle 檔案開始著手。也就是說,您可以在功能模組中納入自訂的 ProGuard 規則,但系統會忽略功能模組建構設定中的
minifyEnabled
屬性。
- 系統會忽略
splits
區塊:建立應用程式套件時,Gradle 會忽略 android.splits
區塊中的屬性。如要控制應用程式套件支援的設定 APK 類型,請改用 android.bundle
以停用設定 APK 的類型。
- 應用程式版本管理:基本模組決定整個應用程式專案的版本號碼及版本名稱。詳情請參閱「管理應用程式更新」一節。
重新啟用或停用設定 APK 的類型
根據預設,當您建立應用程式套件時,它會支援產生設定
適用於各語言資源、螢幕密度資源和 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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)"]]