ベース モジュールの設定

App Bundle はデバイスにデプロイできない点で APK とは異なります。1 つのビルド アーティファクト内にアプリのコンパイル済みコードとリソースをすべて含む公開形式です。そのため、署名付き App Bundle をアップロードすると、Google Play がアプリの APK をビルドして署名し、ユーザーに配信するために必要なものがすべて揃います。

始める

ほとんどのアプリ プロジェクトでは、Android App Bundle をサポートするのにそれほど労力はかかりません。Android Studio で新しいアプリ プロジェクトを作成する際に、アプリのベース APK 用のコードとリソースを含むモジュールが標準のアプリ モジュールとしてデフォルトで生成されるからです。つまり、以下の 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 のバージョン コードを管理する必要がなくなりました。下記に示すように、アプリのベース モジュールのバージョン コードを 1 つ管理するだけで済みます。

// 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 がベース モジュール内のバージョン コードを使って、そのバンドルから生成されるすべての APK に同じバージョン コードを割り当てます。それにより、デバイスにアプリがダウンロード、インストールされると、そのアプリの分割 APK はすべて同じバージョン コードを共有します。

新しいコードやリソースを含めてアプリを更新する際は、アプリのベース モジュールのバージョン コードを更新して、新たに App Bundle 全体をビルドする必要があります。その App Bundle を Google Play にアップロードすると、ベース モジュールが指定するバージョン コードに基づいて APK の新しいセットが生成されます。その後で、ユーザーがアプリを更新すると、そのデバイスに現在インストールされているすべての APK の更新版が Google Play から配信されます。それによって、インストールされているすべての APK が新しいバージョン コードに更新されます。

その他の考慮事項

  • アプリ署名: ビルドファイルに署名情報を含める場合は、ベース モジュールのビルド構成ファイルにのみ含めるようにしてください。詳しくは、Gradle を設定してアプリに署名するをご覧ください。
  • コードの圧縮: アプリ プロジェクト全体で(機能モジュールも含めて)コードの圧縮を有効にする場合は、ベース モジュールの build.gradle ファイルで有効にする必要があります。つまり、機能モジュールにカスタム ProGuard ルールを含めることはできますが、機能モジュールのビルド構成内の minifyEnabled プロパティは無視されます。
  • splits ブロックは無視される: App Bundle をビルドする際、android.splits ブロック内のプロパティは Gradle に無視されます。どのタイプの設定 APK を App Bundle がサポートするかを管理したい場合、代わりに 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 は、ユーザーのデバイス設定で選択した言語に基づいて、アプリでインストールする言語リソースを決定します。アプリをダウンロードした後にデフォルトのシステム言語を変更した場合について考えてみましょう。アプリがその言語をサポートしている場合、デバイスはその言語リソースの追加の設定 APK を Google Play にリクエストしてダウンロードします。

アプリ内に言語選択ツールがあり、システムレベルの言語設定とは無関係にアプリの言語を動的に変更するアプリでは、リソース不足によるクラッシュを防ぐために、いくつかの変更を行う必要があります。android.bundle.language.enableSplit プロパティを false に設定するか、追加の言語リソースをダウンロードするで説明しているように、Play Core ライブラリを使用してオンデマンド言語のダウンロードを実装することを検討してください。