为库创建基准配置文件

如需为库创建基准配置文件,请使用基准配置文件 Gradle 插件

为库创建基准配置文件时涉及以下三个模块:

  • 示例应用模块:包含使用库的示例应用。
  • 库模块:要为库生成配置文件的模块。
  • 基准配置文件模块:生成基准配置文件的测试模块。

如需为库生成基准配置文件,请执行以下步骤:

  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. androidx.baselineprofile 插件添加到应用模块 :sample-app 中的 build.gradle.kts 文件。

    Kotlin

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

    Groovy

    plugins {
        ...
        id 'androidx.baselineprofile'
    }
    
  6. 运行以下代码,生成配置文件:./gradlew :library:generateBaselineProfile

在生成任务结束时,基准配置文件会存储在 library/src/main/generated/baselineProfiles 中。