Improve Android app startup time using Baseline profile

Sagar Das
4 min readMay 31, 2023

--

Baseline Profile is a set of pre-compiled bytecode that can be used to improve the performance of Android apps. It is generated by the Android Gradle plugin and are used by ART (Android Runtime) to optimize the app’s startup time and runtime performance.

Photo by Iván Díaz on Unsplash

Baseline Profiles are created by running the app through the ART Optimizer. The Optimizer analyzes the app’s bytecode and identifies the most frequently executed code paths. These code paths are then compiled into pre-compiled bytecode and stored in a Baseline Profile file.

When an app is launched, ART uses the Baseline Profile file to optimize the app’s startup time. ART loads the Baseline Profile file into memory and then uses it to compile the app’s most frequently executed code paths ahead of time. This results in a significant improvement in the app’s startup time.

The use of Baseline Profiles enhances the performance of Android applications by pre-compiling code (AOT) rather than interpreting it or compiling it just-in-time (JIT). This optimization technique can result in a speed boost of up to 30% during app startup and runtime.

The adoption of Baseline Profiles significantly enhances user experience by making apps faster and more responsive. This, in turn, can increase the number of daily active users and improve the average return visit rate.

For developers aiming to optimize their app’s performance, Baseline Profiles are an invaluable tool. They are straightforward to generate and can be utilized to enhance app performance on all Android devices.

Here are some of the steps on how Baseline Profiles work:

  1. The app is run through the ART Optimizer.
  2. The ART Optimizer analyzes the app’s bytecode and identifies the most frequently executed code paths.
  3. The ART Optimizer compiles the most frequently executed code paths into pre-compiled bytecode and stores them in a Baseline Profile file.
  4. The Baseline Profile file is deployed with the app.
  5. When the app is launched, ART loads the Baseline Profile file into memory.
  6. ART uses the Baseline Profile file to compile the most frequently executed code paths ahead of time.
  7. This results in a significant improvement in the app’s startup time.
  8. ART also uses the Baseline Profile file to cache the results of frequently executed code paths.
  9. This results in a significant improvement in the app’s runtime performance.

Prerequisites : Your app’s min sdk support version needs to be 28 or higher.

Steps to generate a baseline profile for your app:

  1. Click on File > New Module
  2. Select the Baseline profile Generator template
  3. Select the target app in your project
  4. Provide a module name. (Tip: Prefix or append the word “baseline profile” for easier identification.)
  5. Provide a package name.
  6. The Use Gradle Managed Device checkbox sets the module to run the Baseline Profile generators on automatically managed Android emulators. If you uncheck this, the generators use any connected device.
  7. Click on Finish

You will now see a new baseline profile module created for inside your project. Inside the benchmark module you will find a BaselineProfileGenerator.kt and a StartupBenchmarks.kt

At the time of writing this blog, the contents of the BaselineProfileGenerator.kt are:

@RunWith(AndroidJUnit4::class)
@LargeTest
class BaselineProfileGenerator {

@get:Rule
val rule = BaselineProfileRule()

@Test
fun generate() {
rule.collectBaselineProfile("com.example.benchmarkapp") {
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll
// through your most important UI.
// Start default activity for your app
pressHome()
startActivityAndWait()
}
}
}

The BaselineProfileGenerator class is a test class that uses a BaselineProfileRule test rule to generate a baseline profile for an app. The collectBaselineProfile() method is the entry point for generating the profile. It takes two parameters:

  • packageName: The package name of the app.
  • profileBlock: A lambda expression that defines the interactions that cover the typical user journeys of the app.

The library runs the profileBlock several times, collects the called classes and functions, and generates a baseline profile on the device with the code to be optimized.

By default, the BaselineProfileGenerator class contains interactions to start the default Activity of the app and wait until the first frame of the app is rendered using the startActivityAndWait() method.

To generate Baseline Profiles, Google recommends either using an emulator such as a Gradle Managed Device or a device running Android 13 (API 33) or higher. After the device is ready, you can create the Baseline Profile. The Baseline Profile Gradle plugin creates Gradle tasks to automate the whole process of running the generator test class and applying the generated baseline profiles into your app.

To run it, locate the Generate Baseline Profile run configuration and click the Run button.

The task starts the emulator device, runs the interactions from the BaselineProfileGenerator test class several times, and afterwards stops the emulator and provides the output to the IDE.

Once the generator finishes with success, the Gradle plugin automatically puts the generated baseline-prof.txt into your target application (:app module) in the src/release/generated/baselineProfile/ folder.

After creating and integrating the Baseline Profile into your app’s source code, proceed with building the production version of your app as usual. There’s no need for additional steps to distribute the Baseline Profiles to your users. The Android Gradle Plugin automatically selects and includes them in your AAB or APK during the build process. Finally, upload the built version to Google Play.

When users install or update the app from a previous version, the Baseline Profile will also be installed. This leads to enhanced performance right from the initial launch of the app.

--

--

Sagar Das

Staff Android Engineer. Google Developer Expert in Android, Lives in Boston.