Understanding ANR (Application Not Responding) and Implementing Detection Tools in Android Apps
This article explains what ANR is, why it harms user experience, typical scenarios that cause it, how to locate ANR issues via logs, and provides step‑by‑step guidance for integrating an ANR detection tool into Android applications such as Baidu Browser.
In Baidu's Quality Assurance team, a core skill is discovering, locating, and ensuring proper fixes for bugs; this article compiles several ANR (Application Not Responding) case analyses for discussion.
1. What is ANR – An ANR occurs when an Android app becomes unresponsive for a period (typically 5 seconds), causing the system to present an ANR dialog. It happens when user input is not responded to within 5 s, a BroadcastReceiver's onReceive() exceeds 10 s, or a Service lifecycle method runs longer than 20 s.
2. Why ANR must be eliminated – Users expect immediate feedback (e.g., button state change, scrolling, loading progress). Delays over 0.1 s feel sluggish, and ANR dialogs force users to wait >5 s or close the app, making ANR as damaging as crashes.
Typical ANR scenarios include:
UI thread waiting for a lock held by another thread.
Per‑frame heavy calculations in games exhausting CPU.
Web views waiting for unstable network data.
UI thread performing disk I/O (database, SD card) that blocks on some devices.
Other apps monopolizing CPU, leaving insufficient time slices for the target app.
3. Locating ANR via logs – When ANR occurs, Logcat and files in /data/anr/ provide:
Basic info: process name, PID, package, build number, ANR type.
CPU usage: average CPU load, I/O status.
Thread stack traces for the ANR process and related processes.
4. Testing observations – ANR is rarely reproduced in normal tests because it often appears on low‑end devices, weak networks, or intensive user actions. When it does appear, locating it is hard because it requires root access to read /data/anr.txt and manual correlation.
5. Introducing an ANR detection tool – To automate detection, a tool can dump trace files and stack information when the UI thread stalls beyond a threshold (e.g., 4 s) and then throw an exception for analysis.
5.2 Application in Baidu Browser Step 1: Add anr.jar to the project's libs folder. Step 2: Initialize the SDK in Application.onCreate() : initSDK(Context context, String appKey, boolean watchdog, int time) where time is the ANR detection threshold in milliseconds. Step 3: Build and package the APK normally. 5.2.2 Testing and locating ANR – Install the instrumented APK, perform arbitrary actions (e.g., monkey testing). When ANR occurs, the tool kills the process and writes logs to /sdcard/lynq_anr . Using Baidu Browser as an example, the long UI thread startup on low‑end devices triggers ANR; the generated trace and crash files can be examined via DDMS. Analyzing the trace file reveals the method consuming the most CPU time (e.g., bookmark database initialization). Sorting by real time shows the longest‑running method, and the corresponding source code snippet is displayed: The root cause is database initialization performed on the main thread, which can cause timeout on low‑end devices. Note – The ANR detection tool described originates from Baidu's QA team; readers can use the principle diagram and examples to develop their own implementations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
