設定基礎模組

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

應用程式套件與 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 以簽署應用程式
  • 程式碼縮減:如果您想為整個應用程式專案(包括其功能模組)啟用程式碼縮減功能,則必須從基礎模組的版本.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 程式庫執行隨選的語言下載。