After a dependency upgrade slowed cold start, how do you generate a Baseline Profile with BaselineProfileRule and the androidx.baselineprofile plugin, then verify it helped?
answer
- record a journey, ship the list
- producer module plus consumer app
- collect until stable
- generateBaselineProfile task
- None vs Partial(Require)
basics
~10 sRecord startup with BaselineProfileRule.collect in a com.android.test module, let the androidx.baselineprofile plugin copy the rules into the app's source set, ship ProfileInstaller, then compare a cold-start Macrobenchmark under CompilationMode.None() and Partial(BaselineProfileMode.Require).
solid answer
~30 sI'd add a `com.android.test` producer module with the `androidx.baselineprofile` plugin targeting `:app`, and declare `baselineProfile(project(":baselineprofile"))` in the app. The generator calls `BaselineProfileRule().collect(packageName, includeInStartupProfile = true) { pressHome(); startActivityAndWait() }`, waiting for the feed so the loading code is captured; it repeats until the profile is stable. It needs API 33+ or a rooted API 28+ device. `./gradlew :app:generateBaselineProfile` writes `baseline-prof.txt` under `src/<variant>/generated/baselineProfiles`. The app includes ProfileInstaller. To verify, run a cold-start Macrobenchmark under `CompilationMode.None()` and `Partial(BaselineProfileMode.Require)`, which throws if the profile is missing, and compare medians.
code
kotlin · 16 lines// :baselineprofile module (com.android.test)
@RunWith(AndroidJUnit4::class)
class StartupProfileGenerator {
@get:Rule
val baselineProfileRule = BaselineProfileRule()
@Test
fun startup() = baselineProfileRule.collect(
packageName = "com.example.feed",
includeInStartupProfile = true,
) {
pressHome()
startActivityAndWait()
device.wait(Until.hasObject(By.res("feed")), 5_000)
}
}go deeper
Recall that a Baseline Profile lists the classes and methods a journey uses so they are compiled ahead of time after install.
Explain the producer and consumer modules, what collect does until the profile is stable, and where the generated file lands.
Show the whole loop: a generator that waits for real content, the device requirement, ProfileInstaller, and a None versus Partial(Require) cold start on hardware.
Weigh regenerating on every release build against committing a reviewed profile on a cadence, and decide which journeys the team keeps covered.
## What a Baseline Profile is and why it helps A **Baseline Profile** is a list of classes and methods that your app uses on its critical journeys, such as startup and opening the main feed. It ships inside the APK or AAB. When the app is installed, the runtime compiles those methods ahead of time instead of interpreting and JIT-compiling them on first use, so the first launches after an install or update run faster. A dependency upgrade can easily undo this: new library code on the startup path is not in an old profile, so it runs unoptimized. Jetpack Benchmark gives you two tools here: **`BaselineProfileRule`**, which records a profile by running a journey, and the **Baseline Profile Gradle plugin** (`androidx.baselineprofile`), which builds the right variants, runs the generator and copies the result into your app. ## Generating the profile The usual setup has two modules: - **A producer module**: a `com.android.test` module that applies `androidx.baselineprofile` and points `targetProjectPath` at `:app`. It holds the generator test. The Android Studio *Baseline Profile Generator* template creates this module for you. - **The app module** applies the same plugin and declares `baselineProfile(project(":baselineprofile"))` in `dependencies`. The generator test calls `BaselineProfileRule().collect(packageName = ..., includeInStartupProfile = true) { ... }`. Inside the block you run the journey: `pressHome()`, `startActivityAndWait()`, then scroll the feed or open a screen. `collect` repeats the journey until the captured profile is stable, by default for 3 iterations (`stableIterations`) within at most 15 (`maxIterations`). Things that trip people up: 1. **Device.** `BaselineProfileRule` works on Android 13 (API 33) or later, or on a rooted device from API 28. A Gradle Managed Device used for generation on an older API needs an `aosp` system image so it can be rooted. 2. **Startup profile.** Set `includeInStartupProfile = true` only for startup journeys. Those rules also become a *startup profile*, which lets R8 place startup classes in the primary DEX file when the release build is minified and optimized. 3. **Fully drawn.** The profile captures the code that runs during the journey. If the feed loads after the first frame, wait for it in the generator and have the app report fully drawn, through `reportFullyDrawn()` or Compose's `ReportDrawnWhen`, once content is on screen, so the loading code is captured too. 4. **Variants.** The plugin adds `nonMinifiedRelease` and `benchmarkRelease` build types. Generation runs against the non-minified one so the rules name real classes; R8 rewrites them for the obfuscated release build. Run `./gradlew :app:generateBaselineProfile` (or the variant task, such as `generateReleaseBaselineProfile`). With Benchmark 1.5.0 the plugin also works on AGP 9.0 without opting out of AGP's new DSL, which earlier versions required. The rules land in `src/<variant>/generated/baselineProfiles/baseline-prof.txt`, ready to commit. To have every release build regenerate it, set `automaticGenerationDuringBuild = true` in the `baselineProfile { }` block, at the cost of longer builds. ## Shipping it: ProfileInstaller Google Play applies the profile at install time. For other installs, including the APK your benchmark installs, the app needs the **ProfileInstaller** library (`androidx.profileinstaller`), which installs the profile on first run. Macrobenchmark also relies on it to install and reset profiles between modes. Profiles are only installed for **release-like** builds; a debug build never shows the benefit. ## Verifying it helped Generation proves nothing by itself. Measure with a **Macrobenchmark** cold start, run twice: - `CompilationMode.None()`: no profile at all, the worst case; - `CompilationMode.Partial(BaselineProfileMode.Require)`: the shipped profile, and the test **throws** if the profile or ProfileInstaller is missing, so a misconfigured setup cannot pass quietly. Compare the median `timeToInitialDisplayMs` and `timeToFullDisplayMs` from `StartupTimingMetric` on a physical device. Add a frame benchmark for the feed if the profile covers scrolling. | Step | Tool | Proof it worked | |---|---|---| | Record | `BaselineProfileRule.collect` | profile stable over `stableIterations` runs | | Package | `androidx.baselineprofile` plugin | `baseline-prof.txt` under `src/<variant>/generated/` | | Install | ProfileInstaller, or the store | `Partial(Require)` does not throw | | Measure | `MacrobenchmarkRule` + `StartupTimingMetric` | `Partial(Require)` median below `None()` | If the gap is small, read the traces: library code that still JIT-compiles during startup means the journey did not exercise it, or it starts in a process the generator did not cover. How the runtime compiles code internally is beyond this library; what you own is that the journey is captured and the improvement is measured.
- Partial(BaselineProfileMode.Require) throws in your benchmark although baseline-prof.txt exists; what do you check?That the target app includes ProfileInstaller, which the library uses to install the profile, and that the benchmark builds a release-like variant such as `benchmarkRelease`, since profiles are only packaged and installed for release builds. Also confirm the profile file sits in a source set that variant actually compiles.
- What does includeInStartupProfile = true change, and why not set it on every generator?It also emits the captured rules as a startup profile, which a minified, optimized release build uses to place startup classes in the primary DEX file. Rules from non-startup journeys such as a settings screen would pull unrelated classes into that file and weaken the startup layout, so only startup flows should set it.
- A Baseline Profile can be generated on any unrooted API 28 device.
- Installing the debug build shows the profile's speed-up.
- Benchmarking only the Partial run proves the profile helped.
- ProfileInstaller is only needed for Play Store installs.
- Every generator should set includeInStartupProfile to true.
Assumes Benchmark 1.5 and its Baseline Profile Gradle plugin; since 1.5.0 the plugin no longer needs AGP 9.0's new-DSL opt-out.