ベースライン プロファイルを作成する

アプリ デベロッパーは、Jetpack Macrobenchmark ライブラリBaselineProfileRule を使用して、アプリのリリースごとにプロファイルを自動的に生成できます。ベースライン プロファイルを使用する場合は、ビルドが改善されている com.android.tools.build:gradle:8.0.0 以降を使用することをおすすめします。

ベースライン プロファイル ジェネレータを定義する

Macrobenchmark ライブラリを使用してベースライン プロファイルを作成する手順は、次のとおりです。

  1. Gradle プロジェクトに Macrobenchmark モジュールをセットアップします。

  2. BaselineProfileGenerator という新しいテストを次のように定義します。

    class BaselineProfileGenerator {
        @get:Rule
        val baselineProfileRule = BaselineProfileRule()
    
        @Test
        fun startup() = baselineProfileRule.collectBaselineProfile(
            packageName = "com.example.app",
            profileBlock = {
                startActivityAndWait()
            }
        )
    }
    

    ジェネレータには、アプリの起動以外の操作を含めることができます。これにより、リストのスクロール、アニメーションの実行、Activity 内での移動など、アプリのランタイム パフォーマンスの最適化が可能となります。@BaselineProfileRule を使用してクリティカル ユーザー ジャーニーを改善するテストについて、その他の例もご覧になれます。

  3. (省略可)ベースライン プロファイルを生成する際に難読化を無効にします。そのためには、アプリ モジュールで別の ProGuard ファイルを作成し、プロファイルの生成を担当する benchmark ビルドタイプに対してのみ、-dontobfuscate を追加します。

Kotlin

buildTypes {
    val benchmark by creating {
        // Only use benchmark Proguard rules.
        proguardFiles("benchmark-rules.pro")
        // ...
    }
}

Groovy

buildTypes {
    benchmark {
        // Only use benchmark Proguard rules.
        proguardFiles 'benchmark-rules.pro'
        // ...
    }
}

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

ジェネレータは、root 権限取得済みの実機、エミュレータ、Gradle で管理されているデバイスのいずれかで、インストルメンテーション テストとして実行します。管理されているデバイスをセットアップするには、build.gradle.kts ファイルを開きます。testOptions 構成ブロックに managedDevicesdevices を追加して、エミュレータの定義を作成します。ベースライン プロファイル ジェネレータには root 権限が必要であるため、aospsystemImageSource として使用します。

Kotlin

testOptions {
    managedDevices {
        devices {
            create ("pixel6Api31", ManagedVirtualDevice::class) {
                device = "Pixel 6"
                apiLevel = 31
                systemImageSource = "aosp"
            }
        }
    }
}

Groovy

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

Gradle により、指定したデバイス名とモジュールで利用可能なビルド バリアントに基づいて、必要なタスクが作成されます。形式は [emulator_name][flavor][build type]AndroidTest です。このタスクは、次のようなターミナルから実行できます。

./gradlew :benchmark:pixel6Api31BenchmarkAndroidTest

必要なコードパスをすべてキャプチャする

アプリの起動時間を測定するための主な 2 つの指標は、初期表示までの時間(TTID)完全表示までの時間(TTFD)です。TTID は、アプリの UI の最初のフレームを表示するのにかかる時間です。TTFD には、最初のフレームが表示された後に非同期で読み込まれるコンテンツを表示する時間も含まれます。

TTFD は、ComponentActivityreportFullyDrawn() メソッドが呼び出されると報告されます。reportFullyDrawn() が呼び出されない場合、代わりに TTID が報告されます。reportFullyDrawn() が呼び出されるタイミングを、非同期読み込みが完了するまで遅延することが必要になる場合があります。たとえば、UI に RecyclerView遅延リストなどの動的リストが含まれている場合、リストが最初に描画された後、つまり UI が完全に描画されたとマークされた後で完了するバックグラウンド タスクによって、データが入力される場合があります。このような場合、UI が完全に描画された状態になった後で実行されたコードは、ベースライン プロファイルに含まれません。

リストへのデータ入力をベースライン プロファイルに含めるには、getFullyDrawnReporter() を使用して FullyDrawnReporter を取得し、アプリコードでレポーターを追加します。バックグラウンド タスクでリストへのデータ入力が完了したらレポーターを解放します。すべてのレポーターが解放されるまで、FullyDrawnReporterreportFullyDrawn() メソッドを呼び出すことはありません。こうすることで、ベースライン プロファイルにリストへのデータ入力に必要なコードパスが含まれるようになります。これにより、ユーザーにとってのアプリの動作が変わることはありませんが、ベースライン プロファイルには必要なすべてのコードパスが含まれるようになります。

アプリで Jetpack Compose を使用している場合は、以下の API を使用して完全に描画された状態を示します。

  • ReportDrawn は、コンポーザブルで直ちにインタラクションが発生する準備が整っていることを示します。
  • ReportDrawnWhen は、コンポーザブルでインタラクションの準備が整ったタイミングを示す list.count > 0 などの述語を受け取ります。
  • ReportDrawnAfter は、完了時にインタラクションの準備が整ったことを示す停止中のメソッドを受け取ります。

ベースライン プロファイル Gradle プラグイン

ベースライン プロファイル Gradle プラグインを使用すると、ベースライン プロファイルの生成とメンテナンスが容易になります。このプラグインは、ベースライン プロファイルを生成して、アプリ モジュールにインストールするために必要な手順を実行します。

このプラグインを使用するには、プロジェクトにインストルメンテーション テスト モジュールを追加し、アプリを操作してクリティカル ユーザー ジャーニーをシミュレートする一連のテストを定義します。インストルメンテーション テストを実行すると、ベースライン プロファイル Gradle プラグインが、このようなユーザー ジャーニーの間に実行されたすべてのクラスとメソッドを追跡し、それらのクラスとメソッドに基づいてベースライン プロファイルを生成します。そして、生成されたベースライン プロファイルをアプリ モジュールにコピーします。

インストルメンテーション テスト モジュールが、プロファイル プロデューサーです。アプリ モジュールは、プロファイル コンシューマです。

力を注ぐべき主な領域は、初期セットアップと、クリティカル ユーザー ジャーニーをシミュレートするためのテストの作成です。

プラグインの使用に必要なもの

プラグインを使用する

以下の例では、:app という名前のアプリ モジュールが存在することを前提としています。

以下の例では、2 つのモジュールを使用しています。

  • プロファイル コンシューマ: プロファイルの生成対象となるアプリ モジュール。以下の例では、:app です。
  • プロファイル プロデューサー: プロファイルを生成するためのインストルメンテーション テストを含むベースライン プロファイル テスト モジュール。以下の例では、:baseline-profile という名前になっています。

このプラグインを使用する手順は以下のとおりです。

  1. 新しい com.android.test モジュールを作成します(例: :baseline-profile)。
  2. :baseline-profile 向けに build.gradle を設定します。
    1. androidx.baselineprofile プラグインを適用します。
    2. targetProjectPath:app モジュールを参照していることを確認します。
    3. 必要に応じて、GMD を追加します。以下の例では、pixel6Api31 です。指定しなかった場合、プラグインは接続されているデバイス(エミュレータまはた実機)を使用します。
    4. 次の例に示すように、必要な設定を適用します。

    Kotlin

    plugins {
        id("com.android.test")
        id("androidx.baselineprofile")
    }
    
    android {
    
        // There are no changes here. It's documented for completeness.
        defaultConfig {
            ...
        }
    
        // This must point to the app module.
        targetProjectPath = ":app"
    
        // This is the optional managed device.
        testOptions.managedDevices.devices {
            pixel6Api31(com.android.build.api.dsl.ManagedVirtualDevice) {
                device = "Pixel 6"
                apiLevel = 31
                systemImageSource = "aosp"
            }
        }
    }
    
    // There are no changes here. It's documented for completeness.
    dependencies { ... }
    
    // This is the plugin configuration. Everything is optional. Defaults are in the
    // comments. In this example, you use the GMD added earlier and disable
    // connected devices.
    baselineProfile {
    
        // This specifies the managed devices to use that you run the tests on. The
        // default is none.
        managedDevices += "pixel6Api31"
    
        // This enables using connected devices to generate profiles. The default is
        // true. When using connected devices, they must be rooted or API 33 and
        // higher.
        useConnectedDevices = false
    }
    

    Groovy

    plugins {
        id 'com.android.test'
        id 'androidx.baselineprofile'
    }
    
    android {
    
        // There are no changes here. It's documented for completeness.
        defaultConfig {
            ...
        }
    
        // This must point to the app module.
        targetProjectPath ':app'
    
        // This is the optional managed device.
        testOptions.managedDevices.devices {
            pixel6Api31(com.android.build.api.dsl.ManagedVirtualDevice) {
                device 'Pixel 6'
                apiLevel 31
                systemImageSource 'aosp'
            }
        }
    }
    
        // There are no changes here. It's documented for completeness.
    dependencies { ... }
    
    // This is the plugin configuration. Everything is optional. Defaults are in the
    // comments. In this example, you use the GMD added earlier and disable
    // connected devices.
    baselineProfile {
    
        // This specifies the managed devices to use that you run the tests on. The
        // default is none.
        managedDevices ['pixel6Api31']
    
        // This enables using connected devices to generate profiles. The default is
        // true. When using connected devices, they must be rooted or API 33 and
        // higher.
        useConnectedDevices false
    }
    
  3. :baseline-profile テスト モジュールでベースライン プロファイル テストを作成します。次の例は、アプリを起動してアイドル状態になるのを待つテストです。
  4. Kotlin

    class BaselineProfileGenerator {
    
        @get:Rule
        val baselineRule = BaselineProfileRule()
    
        @Test
        fun startupBaselineProfile() {
            baselineRule.collectBaselineProfile("com.myapp") {
                startActivityAndWait()
            }
        }
    }
    

    Java

    public class BaselineProfileGenerator {
    
        @Rule
        Public BaselineProfileRule baselineRule = new BaselineProfileRule();
    
        @Test
        Public void startupBaselineProfile() {
            baselineRule.collectBaselineProfile(
                "com.myapp",
                (scope -> {
                    scope.startActivityAndWait();
                    Return Unit.INSTANCE;
                })
            }
        }
    }
    
  5. アプリ モジュール :appbuild.gradle の設定を更新します。
    1. プラグイン androidx.baselineprofile を適用します。
    2. baselineProfile 依存関係を :baseline-profile モジュールに追加します。

    Kotlin

    plugins {
        id("com.android.application")
        id("androidx.baselineprofile")
    }
    
    android {
        // There are no changes to the `android` block.
        ...
    }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the :baseline-profile module.
        baselineProfile(project(":baseline-profile"))
    }
    

    Groovy

    plugins {
        id 'com.android.application'
        id 'androidx.baselineprofile'
    }
    
    android {
        // No changes to the `android` block.
        ...
    }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the :baseline-profile module.
        baselineProfile ':baseline-profile"'
    }
    
  6. コード「./gradlew :app:generateBaselineProfile」を実行してプロファイルを生成します。

生成タスクが終了すると、ベースライン プロファイルが app/src/release/generated/baselineProfiles に格納されます。

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

以下の例では、ライブラリ モジュールの名前が :library で、ライブラリを利用するアプリが含まれているアプリ モジュールの名前が :sample-app です。

この例では、3 つのモジュールが必要です。

  • アプリ ターゲット: サンプルアプリを含むアプリ モジュール。以下の例では、:sample-app です。
  • プロファイル コンシューマ: プロファイルの生成対象となるライブラリ モジュール。以下の例では、:library です。
  • プロファイル プロデューサー: モジュールを生成するためのインストルメンテーション テストを含むベースライン プロファイル テスト モジュール。

ライブラリのベースライン プロファイルを生成する手順は次のとおりです。

  1. 新しい com.android.test モジュールを作成します(例: :baseline-profile)。
  2. :baseline-profile 向けに build.gradle を設定します。
    1. androidx.baselineprofile プラグインを適用します。
    2. targetProjectPath:sample-app モジュールを参照していることを確認します。
    3. 必要に応じて、GMD を追加します。以下の例では、pixel6Api31 です。
    4. 次の例に示すように、必要な設定を適用します。

    Kotlin

    plugins {
        id("com.android.test")
        id("androidx.baselineprofile")
    }
    
    android {
    
        // There are no changes here. It's reported for completeness.
        defaultConfig {
            minSdkVersion 23
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        // This must point to the app module.
        targetProjectPath = ":app"
    
        // This is the optional managed device.
        testOptions.managedDevices.devices {
            create("pixel6Api31") {
                device = "Pixel 6"
                apiLevel = 31
                systemImageSource = "aosp"
            }
        }
    }
    
    // There's nothing to change here.
    dependencies { ... }
    
    // This is the plugin configuration. Everything is optional. Defaults are in the
    // comments. In this example, you use the GMD added earlier and disable
    // connected devices.
    baselineProfile {
    
        // This specifies the managed devices to use that you run the tests on. The
        // default is none.
        managedDevices += "pixel6Api31"
    
        // This enables using connected devices to generate profiles. The default is
        // true. When using connected devices, they must be rooted or API 33 and
        // higher.
        useConnectedDevices = false
    }
    

    Groovy

    plugins {
        id 'com.android.test'
        id 'androidx.baselineprofile'
    }
    
    android {
    
        // There are no changes here. It's reported for completeness.
        defaultConfig {
            ...
        }
    
        // This must point to the app module.
        targetProjectPath ':app'
    
        // This is the optional managed device.
        testOptions.managedDevices.devices {
            pixel6Api31(com.android.build.api.dsl.ManagedVirtualDevice) {
                device 'Pixel 6'
                apiLevel 31
                systemImageSource 'aosp'
            }
        }
    }
    
    // There's nothing to change here.
    dependencies { ... }
    
    // This is the plugin configuration. Everything is optional. Defaults are in the
    // comments. In this example, you use the GMD added earlier and disable
    // connected devices.
    baselineProfile {
    
        // This specifies the managed devices to use that you run the tests on. The
        // default is none.
        managedDevices ['pixel6Api31']
    
        // This enables using connected devices to generate profiles. The default is
        // true. When using connected devices, they must be rooted or API 33 and
        // higher.
        useConnectedDevices false
    }
    
  3. :baseline-profile テスト モジュールでベースライン プロファイル テストを作成します。サンプルアプリに固有のもので、ライブラリのすべての機能を使用する必要があります。
  4. ライブラリ モジュール :librarybuild.gradle の設定を更新します。
    1. プラグイン androidx.baselineprofile を適用します。
    2. baselineProfile 依存関係を :baseline-profile モジュールに追加します。
    3. 次の例に示すように、必要なコンシューマ プラグイン設定を適用します。

    Kotlin

    plugins {
        id("com.android.library")
        id("androidx.baselineprofile")
    }
    
    // There are no changes to the android block.
    android { ... }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the :baseline-profile module.
        baselineProfile(project(":baseline-profile"))
    }
    
    // This is the plugin configuration.
    baselineProfile {
    
        // This filters the generated profile rules. In this example, you keep
        // all the classes in com.library and in all the subpackages.
        filter {
            include "com.mylibrary.**"
        }
    }
    

    Groovy

    plugins {
        id 'com.android.library'
        id 'androidx.baselineprofile'
    }
    
    // There are no changes to the android block.
    android { ... }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the :baseline-profile module.
        baselineProfile ':baseline-profile'
    }
    
    // This is the plugin configuration.
    baselineProfile {
    
        // This filters the generated profile rules. In this example, you keep
        // all the classes in com.library and in all the subpackages.
        filter {
            include 'com.mylibrary.**'
        }
    }
    
  5. アプリ モジュール :sample-appbuild.gradle の設定を更新し、androidx.baselineprofile プラグインを追加します。
  6. Kotlin

    plugins {
        ...
        id("androidx.baselineprofile")
    }
    // There are no other changes to the configuration.
    

    Groovy

    plugins {
        ...
        id 'androidx.baselineprofile'
    }
    // There are no other changes to the configuration.
    
  7. コード「./gradlew :library:generateBaselineProfile」を実行してプロファイルを生成します。

生成タスクが終了すると、ベースライン プロファイルが library/src/main/generated/baselineProfiles に格納されます。

DSL

このドキュメントでは、ベースライン プロファイル Gradle プラグインを 1 つのプラグインとして参照していますが、実際には、どのモジュールに適用されるかに応じて異なるタスクを実行する 3 つのプラグインがあります。このドキュメントのプラグイン ID は androidx.baselineprofile であり、3 つのプラグインのショートカットになっています。

このコンセプトを深く理解するには、ベースライン プロファイルの生成に必要な次のモジュールを確認してください。

  • ベースライン プロファイル テストの実行対象とするアプリ。これは、com.android.application が適用された Android アプリ モジュールです。ライブラリのプロファイルを生成する場合は、サンプルアプリの可能性があります。アプリのプロファイルを生成する場合は、アプリ自体です。以下の例では、androidx.baselineprofile プラグインが androidx.baselineprofile.apptarget を内部的に適用しています。
  • 実行するテストを含むベースライン プロファイルのテスト モジュール。これは、com.android.test が適用された Android テスト モジュールです。以下の例では、androidx.baselineprofile プラグインが androidx.baselineprofile.producer を内部的に適用しています。
  • ビルドプロセスで最終的にベースライン プロファイルを使用するモジュール。これは、Android アプリ com.android.application のモジュール、またはライブラリ com.android.library モジュールです。以下の例では、androidx.baselineprofile プラグインが androidx.baselineprofile.consumer を内部的に適用しています。

アプリ ターゲット(androidx.baselineprofile.apptarget)

これには設定がありません。

プロファイル プロデューサー(androidx.baselineprofile.producer)

Gradle で管理されているデバイスのサポート

Gradle で管理されているデバイスを使用するには、プロファイル プロデューサー モジュールの build.gradle 設定に追加します。次の例をご覧ください。

Kotlin

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

Groovy

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

作成された GMD に以下を追加すると、それを使用してベースライン プロファイルを生成できます。

Kotlin

baselineProfile {
    managedDevices += "pixel6Api31"
}

Groovy

baselineProfile {
    managedDevices = ['pixel6Api31']
}

次の例は、接続されているデバイスを有効または無効にして、ベースライン プロファイルを生成する追加のオプションです。

Kotlin

baselineProfile {
    ...
    useConnectedDevices = true
}

Groovy

baselineProfile {
    ...
    useConnectedDevices true
}

プロファイル コンシューマ(androidx.baselineprofile.consumer)

フレーバーごとのプロファイルと、すべてのバリアントに対する 1 つのプロファイルを生成する

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

Kotlin

baselineProfile {
    mergeIntoMain = true / false
}

Groovy

baselineProfile {
    mergeIntoMain true / false
}
  • mergeIntoMaintrue に設定すると、各バリアント用に生成されるすべてのプロファイルが 1 つのプロファイルにマージされます。この設定が true の場合、バリアントごとのベースライン プロファイルを生成できないため、generateBaselineProfile という名前の単一の生成タスクのみが存在します。プロファイル出力は src/main/generated/baselineProfiles です。
  • mergeIntoMainfalse に設定すると、マージが無効になり、バリアントごとに 1 つのプロファイルとなります。以下の例では、バリアントごとのベースライン プロファイルを生成できるので、複数の生成タスク(バリアントごとに 1 つ)が存在します。たとえば、2 つのフレーバー(無料と有料など)があり、リリース ビルドタイプが 1 つの場合、生成されるタスクは次のものになります。
    • generateFreeReleaseBaselineProfile
    • generatePaidReleaseBaselineProfile
    • generateReleaseBaselineProfile

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

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

Kotlin

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

Groovy

baselineProfile {
    variants {
        freeRelease {
            mergeIntoMain true / false
        }
    }
}

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

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

タスク generateBaselineProfile でベースライン プロファイルの生成を手動でトリガーすることも、リリースをビルドするときに自動でトリガーすることもできます。これは次のフラグによって制御されます。

Kotlin

baselineProfile {
    automaticallyGenerateDuringBuild = true / false
}

Groovy

baselineProfile {
    automaticallyGenerateDuringBuild true / false
}

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

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

Kotlin

baselineProfile {
    variants {
        freeRelease {
            automaticallyGenerateDuringBuild = true / false
        }
    }
}

Groovy

baselineProfile {
    variants {
        freeRelease {
            automaticallyGenerateDuringBuild true / false
        }
    }
}

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

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

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

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

Kotlin

baselineProfile {
    saveInSrc = true / false
}

Groovy

baselineProfile {
    saveInSrc true / false
}

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

Kotlin

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

Groovy

baselineProfile {
    variants {
        freeRelease {
            saveInSrc true / false
        }
    }
}
プロファイル ルールをフィルタする

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

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

  • 指定されたパッケージとすべてのサブパッケージと一致する、末尾が二重のワイルドカードのパッケージ名。たとえば、com.example.**com.example.foo および com.example.foo.bar と一致します。
  • 指定されたパッケージのみに一致する、末尾がワイルドカードのパッケージ名。たとえば、com.example.*com.example.foo と一致しますが、com.example.foo.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.**'
            }
        }
    }
}

生成したルールを適用する

ベースライン プロファイル ジェネレータにより、人が読める形式(HRF)のテキスト ファイルがデバイスで作成され、ホストマシンにコピーされます。生成されたプロファイルをコードに適用する手順は次のとおりです。

  1. プロファイルを生成したモジュールの build フォルダ([module]/build/outputs/managed_device_android_test_additional_output/[device])にある HRF ファイルを見つけます。

    プロファイルの命名パターンは [class name]-[test method name]-baseline-prof.txt となっています(例: BaselineProfileGenerator-startup-baseline-prof.txt)。

  2. 生成されたプロファイルを特定のフレーバー用としてアプリ モジュールの src/flavor/baselineProfiles にコピーします。すべてのフレーバーにこのプロファイルを適用するには、src/main/baselineProfiles にコピーします。

  3. アプリの build.gradle.kts ファイルで ProfileInstaller ライブラリに依存関係を追加し、クラウド プロファイルを利用できないローカルのベースライン プロファイルのコンパイルを有効にします。これは、ベースライン プロファイルをローカルでサイドローディングする唯一の方法です。

    dependencies {
         implementation("androidx.profileinstaller:profileinstaller:1.3.1")
    }
    
  4. アプリの製品版をビルドします。その際、適用された HRF ルールがバイナリ形式にコンパイルされて APK または AAB に追加されます。ビルドしたら、通常どおりアプリを配信します。

その他の注意事項

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

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

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

Codelabs

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