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

ライブラリのベースライン プロファイルを作成するには、ベースライン プロファイル Gradle プラグインを使用します。

ライブラリのベースライン プロファイルの作成には、次の 3 つのモジュールが使用されます。

  • サンプルアプリ モジュール: ライブラリを使用するサンプルアプリが含まれています。
  • ライブラリ モジュール: プロファイルを生成するモジュール。
  • ベースライン プロファイル モジュール: ベースライン プロファイルを生成するテスト モジュール。

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

  1. 新しい com.android.test モジュールを作成します(例: :baseline-profile)。
  2. :baseline-profile モジュールの build.gradle.kts ファイルを構成します。構成は基本的にアプリの場合と同じですが、必ず targetProjectPath をサンプルアプリ モジュールに設定してください。
  3. :baseline-profile テスト モジュールでベースライン プロファイル テストを作成します。サンプルアプリに固有のもので、ライブラリのすべての機能を使用する必要があります。
  4. ライブラリ モジュール内の build.gradle.ktss ファイル(:library など)の設定を更新します。
    1. プラグイン androidx.baselineprofile を適用します。
    2. baselineProfile 依存関係を :baseline-profile モジュールに追加します。
    3. 次の例に示すように、必要なコンシューマ プラグイン設定を適用します。

    Kotlin

    plugins {
        id("com.android.library")
        id("androidx.baselineprofile")
    }
    
    android { ... }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the `:baseline-profile` module.
        baselineProfile(project(":baseline-profile"))
    }
    
    // Baseline Profile Gradle plugin configuration.
    baselineProfile {
    
        // Filters the generated profile rules. 
        // This example keeps the classes in the `com.library` package all its subpackages.
        filter {
            include "com.mylibrary.**"
        }
    }
    

    Groovy

    plugins {
        id 'com.android.library'
        id 'androidx.baselineprofile'
    }
    
    android { ... }
    
    dependencies {
        ...
        // Add a baselineProfile dependency to the `:baseline-profile` module.
        baselineProfile ':baseline-profile'
    }
    
    // Baseline Profile Gradle plugin configuration.
    baselineProfile {
    
        // Filters the generated profile rules. 
        // This example keeps the classes in the `com.library` package all its subpackages.
        filter {
            include 'com.mylibrary.**'
        }
    }
    
  5. アプリ モジュール :sample-appbuild.gradle.kts ファイルに androidx.baselineprofile プラグインを追加します。

    Kotlin

    plugins {
        ...
        id("androidx.baselineprofile")
    }
    

    Groovy

    plugins {
        ...
        id 'androidx.baselineprofile'
    }
    
  6. ./gradlew :library:generateBaselineProfile のコードを実行してプロファイルを生成します。

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