Skip to main content

Fix ANRs and slow screens

An ANR (Application Not Responding) happens when the main thread is blocked long enough that the app stops responding to input. Your crash reporter usually records that it happened, with a main thread stack parked in Looper.loop, and often labels the root cause as unknown.

Kotzilla records which screen froze and which components held the main thread, with their timings, from real user sessions. Your AI agent uses that context to fix the code and verify the fix.

What your crash reporter shows          What Kotzilla shows

ANR: Input dispatching timed out MainActivity Screen Is Not Responding · 660 sessions
main (WAITING) SettingsViewModel CREATE 3.2 s main thread
at android.os.MessageQueue HomeViewModel CREATE 1.8 s main thread
.nativePollOnce MediaPlayerViewModel CREATE 1.7 s main thread
at android.os.Looper.loop └ their dependencies: all under 5 ms
Root cause unknown 6.7 s on the main thread, past the 4.5 s ANR bar

How Kotzilla detects ANRs and slow screens​

The Kotzilla SDK measures how long each screen takes to render and how long each component takes to be created, and on which thread. Three thresholds apply to the main thread:

What is detectedThresholdWhat it means
Main thread performance issue100 msA component blocks the main thread. One alone rarely freezes the app, but they add up on the same screen.
Slow screen500 msA screen takes too long to render. Users notice, and the screen is on its way to an ANR.
ANR4.5 sThe main thread is blocked long enough to make the app unresponsive. The issue is raised as Screen Is Not Responding on the screen that froze.

The Console tracks the ANR-free rate of each version, the share of sessions without an ANR. It is critical below 99%, in line with Google Play's target. Above Google's 0.47% user-perceived ANR rate in Android Vitals, Play may show your app to fewer people.

Slow screens matter even when they stay under the ANR bar: the same code that takes 1 second on a flagship phone can cross 4.5 seconds on a low-end device. Fixing slow screens early keeps them from becoming ANRs.

Find the frozen screen and what blocked it​

With your AI agent​

With the Kotzilla MCP Server connected, ask in your own words:

Why does MainActivity freeze, and how can I fix it?

Your agent gets the screens with the most ANRs, the components created on the main thread on that screen with their timings, the dependency tree of each one, and the devices and OS versions hit. From there it retrieves the fix patterns for the issue type and applies the fix in your code, under your review (see Fix it below).

In the Console​

The screenshots below come from the codelab app.

  1. On the Dashboard, check the ANR free rate of the version. In the Screen Rendering table, the ANRs column shows which screens froze. Here, MainActivity renders in 5.15 s at P50, with 3 ANRs.

  2. Click the screen to open its Screen Details. Blocking components lists what is resolved while the screen renders, with P50 and P95 times. MainActivityViewModel takes 5,019 ms, while the repository and data store it depends on take less than 10 ms each.

  3. In the Issues view, open MainActivity Screen Is Not Responding. The issue gives the timings, the impacted sessions, and a prompt to paste in your AI agent connected to the MCP Server, or a contextual prompt for any assistant.

  4. Click an impacted session to open its timeline. The events show MainActivityViewModel resolved in 5 s on the main thread, right after MainActivity was created, and the thread graph shows the main thread busy for the whole time.

Compare the component with its dependencies. If the component takes seconds and its dependencies take a few milliseconds, the time goes into its own constructor or init block. ViewModels most often look like this, as in the screenshots above.

Common root causes​

Across the Android and Kotlin Multiplatform apps we monitor, the cause is usually a ViewModel, a repository or an SDK doing too much on the main thread when the first screen needs it.

  • Heavy ViewModel init: the ViewModel loads settings or data in its constructor, so the screen waits before it can draw.
  • Storage opened at launch: a session or auth repository opens the local database on the main thread, right when the first screen needs it.
  • SDKs set up on the main thread: analytics, consent or crash reporting SDKs are initialized when the first screen asks for them, and the screen waits for them.
  • Session and login classes: a user, session or auth class is built, or reads its state, on the main thread.
  • Locks between threads: a background thread is building an object at launch, the screen asks for the same one, and the main thread waits until it is ready.

These delays cross the ANR bar first on low-memory phones, TVs and set-top boxes, which is why an ANR is hard to reproduce on a development device.

Fix it from your AI agent​

The fix depends on the root cause, and your agent gets the pattern that matches the issue type through the MCP Server. For the most common one, a ViewModel doing work in its constructor, the pattern is: keep construction instant, expose state as a StateFlow that starts in a loading state, and load in viewModelScope on a background dispatcher.

// Before: the screen waits while the constructor reads from disk on the main thread
class SettingsViewModel(private val repository: SettingsRepository) : ViewModel() {
val settings: Settings = repository.loadSettings()
}

// After: construction is instant, the data arrives when it is ready
class SettingsViewModel(private val repository: SettingsRepository) : ViewModel() {
private val _state = MutableStateFlow<SettingsUiState>(SettingsUiState.Loading)
val state: StateFlow<SettingsUiState> = _state.asStateFlow()

init {
viewModelScope.launch {
val settings = withContext(Dispatchers.IO) { repository.loadSettings() }
_state.value = SettingsUiState.Ready(settings)
}
}
}

The other root causes follow the same idea: take the work off the main thread, or out of the path of the first screen. Open storage and set up SDKs lazily or on a background dispatcher, and avoid forcing early creation with createdAtStart() or a premature get().

Verify the fix​

Your agent then rebuilds the app with a new version, runs it to the screen that froze, and checks two things in the fresh session: the component is no longer created on the main thread, and the screen's timing dropped compared with the same screen in the version before, on the same device. Once you agree, it closes the issue with the version that fixed it.

To ask for the whole loop in one prompt:

Fix the ANR on MainActivity and close the issue once the fix is verified

See Verify the fix locally for details.

Try it on the codelab​

The Kotzilla codelab is a version of Google's Now in Android with bugs added on purpose. One of them is an ANR: MainActivityViewModel takes about 5 seconds to be created on the main thread, so MainActivity is reported as Screen Is Not Responding. Its dependency tree costs a few milliseconds in total, which points to the constructor.

Follow the codelab setup to register the app and capture a session, then ask your agent:

MainActivityViewModel is blocking the main thread. Diagnose it with Kotzilla and fix it.

Keep ANRs out of the next release​

New features bring ANRs back, so check each release before and after it ships:

  • The Gradle build prints the Kotzilla report for the app version, with its ANR count. With skipBuildReportFailure = false, a FAIL status fails the build, locally or in CI. See Build Report.
  • Email alerts report new critical issues on a schedule, from every 15 minutes to once a month. See Critical Issues Notifications.
  • A Jira ticket per issue carries the root cause, the impacted screens and versions, and a ready-to-run prompt for the MCP Server. See Jira ticket integration.
  • To compare ANR counts and screen rendering times between two versions, ask your agent Is version 1.2.1 performing better than 1.1.5?