ベースライン プロファイルの生成を構成する

ベースライン プロファイル Gradle プラグインを使用すると、ベースライン プロファイルの生成とメンテナンスが容易になります。これは、次のタスクを行うのに役立ちます。

このページでは、ベースライン プロファイル Gradle プラグインを使用して、ベースライン プロファイルの生成をカスタマイズする方法について説明します。

プラグインの要件

Gradle で管理されているデバイスを使用してベースライン プロファイルを生成する

Gradle で管理されているデバイス(GMD)を使用してベースライン プロファイルを生成するには、プロファイル プロデューサー モジュール(:baselineprofile テスト モジュールなど)の build.gradle.kts 構成に GMD を追加します。以下に例を示します。

Kotlin

android {
   testOptions.managedDevices.devices {
       create<com.android.build.api.dsl.ManagedVirtualDevice>("pixel6Api31") {
           device = "Pixel 6"
           apiLevel = 31
           systemImageSource = "aosp"
       }
   }
}

Groovy

android {
   testOptions.managedDevices.devices {
       pixel6Api31(ManagedVirtualDevice) {
           device 'Pixel 6'
           apiLevel = 31
           systemImageSource 'aosp'
       }
   }
}

次のようにベースライン プロファイル Gradle プラグイン構成に GMD を追加することで、GMD を使用してベースライン プロファイルを生成します。

Kotlin

baselineProfile {
    managedDevices += "pixel6Api31"
}

Groovy

baselineProfile {
    managedDevices = ['pixel6Api31']
}

GMD を使用してベースライン プロファイルを生成する場合は、useConnectedDevicesfalse に設定します。

Kotlin

baselineProfile {
    ...
    useConnectedDevices = false
}

Groovy

baselineProfile {
    ...
    useConnectedDevices false
}

さまざまなバリアントのベースライン プロファイルを生成する

ベースライン プロファイルは、バリアントごと、フレーバーごと、またはすべてのバリアントに使用する単一のファイルとして生成できます。この動作は、次の例に示すように、マージ設定で制御できます。

Kotlin

baselineProfile {
    mergeIntoMain = true
}

Groovy

baselineProfile {
    mergeIntoMain true
}

すべてのバリアントで生成されたプロファイルを 1 つのプロファイルに統合するには、mergeIntoMaintrue に設定します。この設定が true の場合、バリアントごとのベースライン プロファイルを生成できないため、generateBaselineProfile という単一の Gradle タスクのみが存在します。プロファイルは src/main/generated/baselineProfiles に出力されます。

マージを無効にして、バリアントごとに 1 つのプロファイルを生成するには、mergeIntoMainfalse に設定します。この場合、バリアント固有の Gradle タスクが複数存在します。たとえば、2 つのフレーバー(無料と有料など)があり、リリースビルド タイプが 1 つの場合、生成されるタスクは次のものになります。

* `generateFreeReleaseBaselineProfile`
* `generatePaidReleaseBaselineProfile`
* `generateReleaseBaselineProfile`

バリアントごとにマージ動作を指定するには、次のコードを使用します。

Kotlin

baselineProfile {
    variants {
        freeRelease {
            mergeIntoMain = true
        }
    }
}

Groovy

baselineProfile {
    variants {
        freeRelease {
            mergeIntoMain true
        }
    }
}

上記の例では、フラグが true に設定されているバリアントはすべて src/main/generated/baselineProfiles にマージされ、フラグが false に設定されているバリアントのプロファイルはフォルダ src/<variant>/generated/baselineProfiles に保持されます。

デフォルトでは、mergeIntoMain がライブラリでは true に、アプリでは false に設定されています。

新しいリリースのアセンブル時にベースライン プロファイルを自動生成する

タスク generateBaselineProfile を手動で使用する代わりに、リリースビルドごとにベースライン プロファイルを自動生成するように構成できます。自動生成では、最新のプロファイルがリリースビルドに含められます。

リリースビルドの自動生成を有効にするには、automaticGenerationDuringBuild フラグを使用します。

Kotlin

baselineProfile {
    automaticGenerationDuringBuild = true
}

Groovy

baselineProfile {
    automaticGenerationDuringBuild true
}

automaticGenerationDuringBuild フラグを true に設定すると、リリース アセンブリごとに新しいベースライン プロファイルの生成がトリガーされます。つまり、./gradlew:app:assembleRelease などのアセンブル リリースビルド タスクを実行すると、:app:generateReleaseBaselineProfile もトリガーされ、ベースライン プロファイルのインストルメンテーション テストが開始され、その実行対象となるベースライン プロファイル ビルドがビルドされます。自動生成により、ユーザーが得られるパフォーマンス上のメリットは最大となりますが、ビルドとインストルメンテーション テストが二重になるため、ビルド時間も長くなります。

次の例に示すように、この動作をバリアントごとに指定することもできます。

Kotlin

baselineProfile {
    variants {
        freeRelease {
            automaticGenerationDuringBuild = true
        }
    }
}

Groovy

baselineProfile {
    variants {
        freeRelease {
            automaticGenerationDuringBuild true
        }
    }
}

上記の例では、assembleFreeRelease の開始時にタスク generateFreeReleaseBaselineProfile が実行されます。これは、ビルド時に常にプロファイルを生成する配布ビルド用の release や、内部テスト用の releaseWithoutProfile ビルドなどをユーザーが必要としている場合に役立ちます。

ベースライン プロファイルをソースに保存する

saveInSrc フラグを使用すると、ベースライン プロファイルをソース ディレクトリに格納できます。

  • true: ベースライン プロファイルは src/<variant>/generated/baselineProfiles に保存されます。これにより、最後に生成されたプロファイルをソースに commit できます。
  • false: ベースライン プロファイルは、ビルド ディレクトリにある中間ファイルに保存されます。こうすると、コードを commit するときに、生成された最新のプロファイルは保存されません。

Kotlin

baselineProfile {
    saveInSrc = true
}

Groovy

baselineProfile {
    saveInSrc true
}

この動作をバリアントごとに指定することもできます。

Kotlin

baselineProfile {
    variants {
        freeRelease {
            saveInSrc = true
        }
    }
}

Groovy

baselineProfile {
    variants {
        freeRelease {
            saveInSrc true
        }
    }
}

プロファイル ルールをフィルタする

ベースライン プロファイル Gradle プラグインを使用すると、生成されたベースライン プロファイル ルールをフィルタできます。特にライブラリで、サンプルアプリやライブラリ自体の依存関係に含まれているクラスやメソッドでプロファイル ルールを除外する場合に便利です。フィルタでは、特定のパッケージやクラスを含めたり、除外したりできます。除外のみを指定した場合、一致するベースライン プロファイル ルールのみが除外され、その他はすべて包含されます。

フィルタ指定は次のいずれかです。

  • 指定されたパッケージとすべてのサブパッケージと一致する、末尾が二重のワイルドカードのパッケージ名。たとえば、com.example.**com.example.method および com.example.method.bar と一致します。
  • 指定されたパッケージのみに一致する、末尾がワイルドカードのパッケージ名。たとえば、com.example.*com.example.method と一致しますが、com.example.method.bar とは一致しません。
  • 特定のクラスと一致するクラス名(例: com.example.MyClass)。

次の例は、特定のパッケージを包含または除外する方法を示しています。

Kotlin

baselineProfile {
    filter {
        include("com.somelibrary.widget.grid.**")
        exclude("com.somelibrary.widget.grid.debug.**")
        include("com.somelibrary.widget.list.**")
        exclude("com.somelibrary.widget.list.debug.**")
        include("com.somelibrary.widget.text.**")
        exclude("com.somelibrary.widget.text.debug.**")
    }
}

Groovy

baselineProfile {
    filter {
        include 'com.somelibrary.widget.grid.**'
        exclude 'com.somelibrary.widget.grid.debug.**'
        include 'com.somelibrary.widget.list.**'
        exclude 'com.somelibrary.widget.list.debug.**'
        include 'com.somelibrary.widget.text.**'
        exclude 'com.somelibrary.widget.text.debug.**'
    }
}

以下のように、さまざまなバリアントのフィルタルールをカスタマイズします。

Kotlin

// Non-specific filters applied to all the variants.
baselineProfile {
    filter { include("com.myapp.**") }
}

// Flavor-specific filters.
baselineProfile {
    variants {
        free {
            filter {
                include("com.myapp.free.**")
            }
        }
        paid {
            filter {
                include("com.myapp.paid.**")
            }
        }
    }
}

// Build-type-specific filters.
baselineProfile {
    variants {
        release {
            filter {
                include("com.myapp.**")
            }
        }
    }
}

// Variant-specific filters.
baselineProfile {
    variants {
        freeRelease {
            filter {
                include("com.myapp.**")
            }
        }
    }
}

Groovy

// Non-specific filters applied to all the variants.
baselineProfile {
    filter { include 'com.myapp.**' }
}

// Flavor-specific filters.
baselineProfile {
    variants {
        free {
            filter {
                include 'com.myapp.free.**'
            }
        }
        paid {
            filter {
                include 'com.myapp.paid.**'
            }
        }
    }
}

// Build-type specific filters.
baselineProfile {
    variants {
        release {
            filter {
                include 'com.myapp.**'
            }
        }
    }
}

// Variant-specific filters.
baselineProfile {
    variants {
        freeRelease {
            filter {
                include 'com.myapp.**'
            }
        }
    }
}

また、BaselineProfileRule.collect()filterPredicate 引数を使用してもルールをフィルタできますが、サブパッケージを簡単にフィルタでき、1 か所でモジュール全体を構成できるため、Gradle プラグインを使用してフィルタすることをおすすめします。

ベンチマークとベースライン プロファイルのビルドタイプをカスタマイズする

ベースライン プロファイル Gradle プラグインは、プロファイルを生成してベンチマークを実行するための追加のビルドタイプを作成します。これらのビルドタイプには接頭辞 benchmarknonMinified が付いています。たとえば、release ビルドタイプの場合、プラグインは benchmarkRelease ビルドタイプと nonMinifiedRelease ビルドタイプを作成します。これらのビルドタイプは、特定のユースケースに合わせて自動的に設定されるため、通常はカスタマイズは必要ありません。ただし、別の署名設定を適用する場合など、カスタム オプションを適用すると便利な場合もあります。

ビルドタイプ プロパティのサブセットを使用して、自動生成されたビルドタイプをカスタマイズできます。使用できないプロパティはオーバーライドされます。次の例は、追加のビルドタイプとオーバーライドするプロパティをカスタマイズする方法を示しています。

Kotlin

android {
    buildTypes {
        release {
            ...
        }
        benchmarkRelease {
            // Customize properties for the `benchmarkRelease` build type here.
            // For example, you can change the signing config (by default
            // it's the same as for the `release` build type).
            signingConfig = signingConfigs.getByName("benchmarkRelease")
        }
        nonMinifiedRelease {
            // Customize properties for the `nonMinifiedRelease` build type here.
            signingConfig = signingConfigs.getByName("nonMinifiedRelease")

            // From Baseline Profile Gradle plugin 1.2.4 and higher, you can't
            // customize the following properties, which are always overridden to
            // avoid breaking Baseline Profile generation:
            //
            // isJniDebuggable = false
            // isDebuggable = false
            // isMinifyEnabled = false
            // isShrinkResources = false
            // isProfileable = true
            // enableAndroidTestCoverage = false
            // enableUnitTestCoverage = false
        }
    }
}

Groovy

android {
    buildTypes {
        release {
            ...
        }
        benchmarkRelease {
            // Customize properties for the `benchmarkRelease` build type here.
            // For example, you can change the signing config (by default it's the
            // same as for the `release` build type.)
            signingConfig = signingConfigs.benchmarkRelease
        }
        nonMinifiedRelease {
            // Customize properties for the `nonMinifiedRelease` build type here.
            signingConfig = signingConfigs.nonMinifiedRelease

            // From Baseline Profile Gradle plugin 1.2.4 and higher, you can't use
            // the following properties, which are always overridden to avoid breaking
            // Baseline Profile generation:
            //
            // isJniDebuggable = false
            // isDebuggable = false
            // isMinifyEnabled = false
            // isShrinkResources = false
            // isProfileable = true
            // enableAndroidTestCoverage = false
            // enableUnitTestCoverage = false       
        }
    }
}

その他の注意事項

ベースライン プロファイルを作成する際は、次の点にもご注意ください。

  • コンパイル済みのベースライン プロファイルは、1.5 MB 未満でなければなりません。これは、コンパイルの前よりもはるかに大きいことが一般的であるソースファイル内のテキスト形式には適用されません。バイナリ形式のベースライン プロファイルのサイズを、assets/dexopt/baseline.prof(APK の場合)または BUNDLE-METADATA/com.android.tools.build.profiles/baseline.prof(AAB の場合)の出力アーティファクトで確認してください。

  • アプリのコンパイルが多すぎる広範なルールでは、ディスク アクセスの増加により起動が遅くなる可能性があります。ベースライン プロファイルを使い始めたばかりの場合は、心配する必要はありません。ただし、ジャーニーを多数追加した場合、アプリやジャーニーのサイズと数によっては、最適なパフォーマンスを得られなくなることもあります。別のプロファイルを試してアプリのパフォーマンスをテストし、追加によってパフォーマンスが退行しないことを確認してください。

Codelabs

パフォーマンスを測定するための Macrobenchmark について詳しく見てみましょう。
Android アプリに合わせたカスタム ベースライン プロファイルを生成して、その効果を検証します。