Baseline Profiles for Android Performance
Baseline Profiles for Android Performance
Baseline Profiles are a performance optimization feature that reduces app startup time and improves runtime execution by providing pre compiled code information to the Android Runtime (ART). Instead of relying on interpretation and just in time (JIT) compilation during the first several launches, ART uses the profile to compile critical code paths ahead of time during installation. This can yield startup improvements of 20 to 30 percent from the very first launch. By the end of this lesson, you will be able to:
- Explain how Baseline Profiles interact with ART's compilation pipeline.
- Describe how profiles are generated using the Jetpack Macrobenchmark library.
- Identify where the generated profile is stored within the project structure.
- Explain the difference between Baseline Profiles and cloud profiles from Google Play.
How Baseline Profiles Work with ART
Android Runtime supports multiple compilation strategies. On a fresh install without any profile data, code runs through the interpreter. As methods are called repeatedly, the JIT compiler compiles them into native code. Over time, the system records which methods are frequently used and performs background Ahead of Time (AOT) compilation during idle periods. This means the first several launches of an app are measurably slower than later launches.
Baseline Profiles shortcut this warmup process. The profile file contains a list of classes and methods that should be AOT compiled during installation, before the user ever opens the app. ART reads this profile and compiles the specified code paths into native machine code immediately, skipping the interpreter and JIT warmup entirely. The result is that the first launch performs as well as a fully warmed launch.
// Example entries in a baseline-prof.txt
HSPLcom/example/app/MainActivity;->onCreate(Landroid/os/Bundle;)V
HSPLcom/example/app/data/Repository;->fetchData()Ljava/util/List;
PLcom/example/app/ui/HomeScreen;->render()V
Each line specifies a method or class with flags indicating whether it should be hot (H), startup (S), or post startup (P). ART uses these flags to prioritize compilation order.
Generating Baseline Profiles
Profiles are generated using the Jetpack Macrobenchmark library. A benchmark test drives the app through its critical user journeys, and the system records which classes and methods are accessed during those journeys.
@ExperimentalBaselineProfilesApi
class BaselineProfileGenerator {
@get:Rule
val baselineProfileRule = BaselineProfileRule()
@Test
fun startup() = baselineProfileRule.collect(
packageName = "com.example.app"
) {
pressHome()
startActivityAndWait()
}
}
This interview continues for subscribers
Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.
Become a Sponsor