To create Baseline Profiles for a library, use the Baseline Profile Gradle plugin.
There are three modules involved in creating Baseline Profiles for a library:
- Sample app module: contains the sample app that uses your library.
- Library module: the module you want to generate the profile for.
- Baseline Profile module: the test module that generates the Baseline Profiles.
To generate a Baseline Profile for a library, perform the following steps:
- Create a new com.android.testmodule—for example,:baseline-profile.
- Configure the build.gradle.ktsfile for the:baseline-profilemodule. The configuration is essentially the same as for an app, but make sure to set thetargetProjectPathto the sample app module.
- Create a Baseline Profile test in the :baseline-profiletest module. This needs to be specific to the sample app and must use all the functionalities of the library.
- Update the configuration in build.gradle.ktssfile in the library module, say:library.
- Apply the plugin androidx.baselineprofile.
- Add a baselineProfiledependency to the:baseline-profilemodule.
- Apply the consumer plugin configuration you want, as shown in the following example.
- Add the androidx.baselineprofileplugin to thebuild.gradle.ktsfile in the app module:sample-app.Kotlinplugins { ... id("androidx.baselineprofile") } Groovyplugins { ... id 'androidx.baselineprofile' } 
- Generate the profile by running the following code:
  ./gradlew :library:generateBaselineProfile.
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.**' } }
At the end of the generation task, the Baseline Profile is stored at
library/src/main/generated/baselineProfiles.
