ベース モジュールの設定

App Bundle は、デバイスにデプロイできない点で APK とは異なります。1 つのビルド アーティファクト内にアプリのコンパイル済みコードとリソースをすべて含む公開形式です。そのため、署名付き 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")
}

ベース モジュールは、アプリのコア機能を提供するだけでなく、アプリ プロジェクト全体に影響するビルド構成とマニフェスト エントリの多くも提供します。

ベース モジュールのビルド構成

既存のほとんどのアプリ プロジェクトでは、ベース モジュールのビルド構成を変更する必要はありません。ただし、アプリ プロジェクトに機能モジュールを追加することを検討している場合や、以前に複数の 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 をビルドするとき、Gradle は android.splits ブロックのプロパティを無視します。App Bundle でサポートする設定 APK のタイプを制御する場合は、代わりに android.bundle を使用して設定 APK のタイプを無効にします。
  • アプリのバージョニング: ベース モジュールによって、アプリ プロジェクト全体のバージョン コードとバージョン名が決まります。詳しくは、バージョン コードとアプリ アップデートをご覧ください。

設定 APK のタイプを無効にするまたは再度有効にする

デフォルトでは、App Bundle をビルドすると、言語リソース、画面密度リソース、ABI ライブラリの各セットの設定 APK の生成がサポートされます。以下に示すように、ベース モジュールの build.gradle ファイルで android.bundle ブロックを使用すると、1 つ以上のタイプの設定 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 ライブラリを使用してオンデマンドの言語ダウンロードを実装することを検討してください。