Where Android startup time goes, and how to get it back
Cold start is the first thing every user experiences, on every launch. During the app modernization I led, we cut it by 31%. No single trick did it. It took measuring honestly, finding where the time actually went, moving everything the first frame didn't need to after the first frame, and a check in CI so the number couldn't quietly creep back. This post goes through each of those in the order I'd do them again.
What happens between the tap and the first frame
It helps to know who owns each part of a cold start, because you can only fix the parts that are yours.
Android also distinguishes three kinds of start. A cold start is all of the above, from a process that doesn't exist. A warm start finds the process alive but has to recreate the Activity. A hot start just brings an existing Activity back to the front. Cold start is the worst case and the one users notice, so that's what I measure and what this post is about. Play Console's Android vitals flags a cold start of five seconds or more as excessive. You want to be nowhere near that.
Measure the right number, on the right build
There are two numbers. Time to Initial Display is when the system draws your first frame. Time to Full Display is when that screen is actually useful, with its data in place. Users feel the second one. A fast first frame that shows a spinner for two seconds isn't a fast start.
The system can't know when your content is ready, so you tell it. In Compose:
@Composablefun HomeRoute(viewModel: HomeViewModel = hiltViewModel()) { val state by viewModel.state.collectAsStateWithLifecycle() ReportDrawnWhen { state.items.isNotEmpty() } HomeScreen(state)}With Views, call reportFullyDrawn() on the Activity at the same moment. From then on, both Macrobenchmark and Android vitals report a real Time to Full Display.
Then measure it properly:
- A release build. Debug builds skip R8 and run more code through the interpreter. They're slower, and slower in different places, so they send you after the wrong problems.
- A mid-range physical device. A flagship hides problems and an emulator invents new ones.
- Many runs. One cold start tells you almost nothing. Look at the median and the slow tail across ten or more iterations.
// Macrobenchmark cold-start test@RunWith(AndroidJUnit4::class)class StartupBenchmark { @get:Rule val benchmarkRule = MacrobenchmarkRule() @Test fun coldStartup() = benchmarkRule.measureRepeated( packageName = "com.example.app", metrics = listOf(StartupTimingMetric()), iterations = 10, startupMode = StartupMode.COLD, compilationMode = CompilationMode.Partial() ) { pressHome() startActivityAndWait() }}For a quick check without a benchmark module, adb shell am start -W -n com.example.app/.MainActivity prints the total launch time. It's rough, but it's enough to tell whether a change helped.
Find where the time goes
Guessing is how startup work gets wasted. Before changing anything, I want a trace that shows exactly what ran on the main thread and for how long.
Name your own work so it shows up in the trace. The androidx.tracing library makes this one line per section:
class App : Application() { override fun onCreate() { super.onCreate() trace("AppInit.crashReporting") { crashReporting.init(this) } trace("AppInit.analytics") { analytics.init(this) } trace("AppInit.featureFlags") { featureFlags.loadCached() } }}Record a system trace of a cold start and open it in Perfetto. The main thread's timeline shows your named sections next to the system's own: bindApplication, activityStart, Choreographer#doFrame, class loading and lock contention. On a large app, the surprise is rarely one slow thing. It's usually a long tail of small ones, each costing "only" a few tens of milliseconds, that nobody has ever added up.
Once a section is named, Macrobenchmark can track it on its own with TraceSectionMetric("AppInit.analytics"), so you can see a single initializer get slower over time instead of only the total.
Where the time usually hides
Library auto-initializers
Many libraries initialize themselves through a ContentProvider or through Jetpack App Startup. Providers run before Application.onCreate(), so this cost doesn't even appear in the code you think of as startup. Read your merged manifest. For anything that can wait, switch off its initializer and start it yourself later:
<provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <meta-data android:name="com.example.analytics.AnalyticsInitializer" tools:node="remove" /></provider>SDKs in Application.onCreate()
Nearly every third-party SDK's setup guide tells you to initialize it here, synchronously. For most of them that's convenient for the vendor and unnecessary for you. I sort each one into three buckets:
- Needed before the first frame. Crash reporting, so a crash during startup still gets reported, and whatever the first screen reads. This list should be short.
- Needed soon, not first. Analytics, remote config refresh, push token registration. These run after the first frame.
- Needed only when a feature is used. A payments SDK, a map, a chat widget. These initialize when that feature opens, not at launch.
An eager dependency graph
If the first Activity injects a repository that needs a database, an HTTP client and three other services, Hilt builds all of them before anything is drawn. For what the first frame doesn't need, inject a dagger.Lazy<T> or a Provider<T> so construction waits until first use:
@AndroidEntryPointclass MainActivity : ComponentActivity() { @Inject lateinit var analytics: dagger.Lazy<Analytics> @Inject lateinit var syncManager: dagger.Lazy<BackgroundSyncManager> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { AppTheme { HomeRoute() } } // Defer non-critical work until after the first frame is scheduled window.decorView.post { analytics.get().initialize() syncManager.get().schedulePeriodicSync() } }}Disk reads on the main thread
The first time you read a SharedPreferences file, Android loads the whole file, and a read on the main thread waits for that. Opening a database or reading a large JSON asset at launch does the same. Turn on StrictMode in debug builds so these show up the moment someone adds one:
if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .penaltyLog() .build() )}Then move the reads off the main thread. DataStore and Room are asynchronous by design, which is one more reason to migrate off SharedPreferences.
Waiting on the network
The most expensive startup mistake I see is a first screen that waits for a network call: fresh remote config, the latest feed, a token check against the server. On a slow connection that's seconds. Show what's cached immediately and refresh in the background. The same goes for the splash screen. If you use setKeepOnScreenCondition, make the condition depend on local data only, never on a request.
Code that hasn't been compiled yet
On a fresh install, much of your app's code runs interpreted or just-in-time compiled until the runtime has seen it enough times. Startup code runs once per launch, so it's exactly the code that stays slow. A Baseline Profile fixes that by listing the classes and methods on your critical path so they're compiled ahead of time at install:
@RunWith(AndroidJUnit4::class)class BaselineProfileGenerator { @get:Rule val rule = BaselineProfileRule() @Test fun generate() = rule.collect( packageName = "com.example.app", includeInStartupProfile = true, ) { pressHome() startActivityAndWait() device.wait(Until.hasObject(By.res("home_list")), 5_000) }}Of everything in this post, this is usually the biggest win for the least effort. Setting includeInStartupProfile also lets R8 lay out the startup code together in the DEX files, so less of it has to be read from disk.
A first screen that does too much
Even with everything else fixed, the first screen can be the bottleneck: a deep layout, full-size images decoded on the main thread, a custom font loaded synchronously, or a list that builds every row up front. Keep the first screen lean, size images to the views that show them, and let anything below the fold load after it's drawn.
Putting it together
If I were starting on a new app tomorrow, this is the order I'd go in:
- Add
ReportDrawnWhenand a Macrobenchmark test, and record the median on a mid-range device. - Take a trace and name every initializer.
- Add a Baseline Profile.
- Switch off auto-initializers and sort every SDK into now, soon or on first use.
- Make the dependency graph lazy for anything the first frame doesn't need.
- Move disk reads off the main thread and show cached content first.
- Trim the first screen.
- Put the benchmark in CI with a threshold.
Why modularization made this possible
In a tightly coupled codebase, deferring anything is risky. Some singleton somewhere expects global state to be ready at launch, and you find out in production. Once features sat behind module boundaries with injected interfaces, I could see exactly which dependencies the first screen needed and which could wait, and moving one didn't break three others.
Keep it from creeping back
Startup gets slower one reasonable change at a time. Someone adds an SDK the way its guide says, someone reads a preference at launch, and in six months the gain is gone. Run the startup benchmark in CI and fail the pull request when the median crosses a threshold, regenerate the Baseline Profile when the critical path changes, and keep an eye on Android vitals for the devices your lab doesn't have. A number that's checked on every change stays good.