Debugging log · Performance
ANR — "Application Not Responding"
Users started seeing the dreaded Application Not Responding dialog — the one that offers to close the app — and Play Console's Android vitals lit up with an ANR rate. There was no crash, no exception, no stack trace in the usual sense. The app wasn't broken; it was busy. The main thread sat blocked long enough that the system decided the app had hung.
The short version: an ANR fires when the main (UI) thread is blocked too long — roughly 5 seconds for an unhandled input event. Something slow is running where it shouldn't: disk, network, a big loop, or a lock. Read the ANR trace to find what the main thread was doing, and move that work off it.
What triggers an ANR
Android watches the main thread because that thread draws the UI and handles taps. If it can't respond to an input event within about 5 seconds — or finish a BroadcastReceiver or Service lifecycle step within its own limit — the system raises an ANR. The cause is always the same shape: something that should have run in the background ran on the main thread and held it. Unlike a crash, there's no exception thrown; the symptom is a freeze, so you have to look at what the main thread was stuck on.
Read the ANR trace
An ANR produces a trace (in Play Console's Android vitals, or on-device under /data/anr/, or via adb bugreport). Find the "main" thread in it and read its stack — the top frames show exactly what it was doing when it stalled. You're looking for one of a few tell-tales:
- I/O on main — file reads, database queries, DataStore/SharedPreferences, network.
- A lock — the main thread waiting (
BLOCKED/waiting to lock) on a monitor another thread holds. - Heavy computation — a large loop, JSON parsing, bitmap work, sorting a big list.
Fix: move the slow work off the main thread
Whatever the trace names, the fix is to run it in the background and only touch the UI when done. With coroutines:
lifecycleScope.launch {
val result = withContext(Dispatchers.IO) {
loadFromDatabase() // the slow thing, off main
}
render(result) // back on main
}
For CPU-heavy work use Dispatchers.Default; for disk/network use Dispatchers.IO. The principle is identical to avoiding the main-thread network and wrong-thread view crashes — keep the UI thread free to respond.
Specific offenders worth hunting
- Database/DataStore on main. Room will warn; a raw query won't. Make them suspend or background them.
- SharedPreferences
commit()on main — useapply()(async) or move it off. - Large JSON parsing in
onCreate— defer and background it. - A lock held by a slow background task that the main thread then waits on — rethink the locking.
- Bitmap decoding / image work on main — decode off-thread.
- Too much in
onCreate/onResume— these run on main; keep them light so the first frame is fast.
An ANR isn't a bug with a line number — it's a budget you blew. Treat the 5-second window as a hard ceiling you should never come near: anything that might be slow (I/O, parsing, big loops) belongs off the main thread by default, not just when it happens to cross the line on a fast device.
Catching it before users do
- Enable StrictMode in debug to flag disk/network on main while you develop.
- Watch Android vitals in Play Console — it aggregates real-world ANRs with traces.
- Test on a slow device, not just a flagship — a stall that's invisible on fast hardware becomes an ANR on budget phones.
Method names above are illustrative — use your own, and act on what your ANR trace's main thread is actually doing.