設定基礎模組

應用程式套件與 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 程式庫導入隨選下載語言的功能。