MVP Factory
EN
ai startup development

Android app startup surgery: Baseline Profiles & dex layout

KW
Krystian Wiewiór · · 4 min read

Meta description: Master Android startup optimization: Macrobenchmark, Baseline Profiles, and dex layout strategies with the cold/warm/hot start budgets tied to Day-1 retention.


TL;DR

Cold start under 500ms and warm start under 300ms aren’t arbitrary targets. They correlate directly with Day-1 retention. The tools to hit those numbers exist: Macrobenchmark for instrumentation, Baseline Profiles for AOT compilation of critical paths, and dex layout optimization to reduce class-loading overhead. Most teams skip at least two of these. Don’t.


Why startup time is a retention problem, not a performance problem

Google’s own data shows startup latency above 500ms measurably increases Day-1 abandonment on cold launches. Users form a mental model of your app’s quality in the first three seconds. And it sticks.

If you’re shipping a productivity app — something like HealthyDesk, which surfaces break reminders and desk exercises for developers — a sluggish cold start defeats the core value proposition before the first screen renders.

Startup isn’t a vanity metric. It’s a retention lever.


Step 1: Instrument first

Most teams optimize blind. They add StrictMode, eyeball Logcat, and call it done. That’s guesswork.

The right tool is androidx.benchmark:benchmark-macrobenchmark. It runs in a real process, on a real device, isolated from your test APK.

@RunWith(AndroidJUnit4::class)
class StartupBenchmark {
    @get:Rule
    val benchmarkRule = MacrobenchmarkRule()

    @Test
    fun coldStartup() = benchmarkRule.measureRepeated(
        packageName = "com.example.myapp",
        metrics = listOf(StartupTimingMetric()),
        iterations = 10,
        startupMode = StartupMode.COLD
    ) {
        pressHome()
        startActivityAndWait()
    }
}

Run this against your release build (not debug — JIT behavior differs dramatically). The output gives you timeToInitialDisplayMs and timeToFullDisplayMs as percentile distributions. P50 matters. P95 is what your worst users experience.


Step 2: Baseline Profiles — AOT for your critical path

Here’s what most teams get wrong: Baseline Profiles are not a magic switch. They’re a compilation hint file that tells the ART runtime which classes and methods to compile ahead-of-time on first install or update.

Without a Baseline Profile, the class loader verifies and JIT-compiles your startup path on every cold launch until the device runs its background dexopt job (typically overnight, idle, charging). That means your Day-1 users are running interpreted bytecode.

Generating a profile:

@RunWith(AndroidJUnit4::class)
class BaselineProfileGenerator {
    @get:Rule
    val rule = BaselineProfileRule()

    @Test
    fun generate() = rule.collect(
        packageName = "com.example.myapp"
    ) {
        pressHome()
        startActivityAndWait()
        // Drive your critical user journey here
    }
}

The output is a baseline-prof.txt placed in src/main/. The Android Gradle Plugin bundles it into the APK. Play Store triggers compilation on install via speed-profile mode.


Step 3: Dex layout and the class loader tax

Even with a Baseline Profile, class loading order matters. The dex file format stores classes linearly. If your startup path loads MainActivity but MainActivity sits at offset 8,000 in the dex, the class loader pays a seek cost.

The dexlayout tool (part of the Android platform tools) reorders classes based on a profile, minimizing disk reads. The Gradle plugin handles this automatically when you provide a Baseline Profile, but only for release builds with minifyEnabled = true.

Optimization layerCold start reductionComplexity
No optimization (baseline)0%—
Baseline Profile only10–30%Low
Dex layout reordering5–15% additionalLow (automatic)
App Startup library (lazy init)5–20%Medium
Combined approach25–50%Medium

The combined approach routinely gets apps from 800ms cold starts down into the 400–450ms range in production.


The three start types

Start typeDefinitionTarget budget
ColdApp process not in memory< 500ms
WarmActivity recreated, process alive< 300ms
HotActivity brought to foreground< 100ms

Warm starts are where most teams leave performance on the table. If your Activity.onCreate() is re-running expensive initialization that should live in Application — or worse, hitting the network — your warm start collapses toward cold start territory.


Three takeaways

  1. Instrument before you optimize. Add Macrobenchmark to your CI pipeline and track P50 and P95 startup times across releases. You can’t improve what you don’t measure.

  2. Ship a Baseline Profile with every release. Generate it from real user journeys, commit it to source, and verify via adb shell dumpsys package that the compilation filter shows speed-profile post-install.

  3. Treat warm start as a first-class metric. Audit your Activity.onCreate() for work that should be deferred or moved upstream. The 300ms budget is tight.


#android #kotlin #mobile #architecture #jetpackcompose


Share: Twitter LinkedIn