ANR (Application Not Responding) in Android: Causes, Detection, and Debugging

This article explains what Android ANR (Application Not Responding) is, outlines typical scenarios that cause it, describes how to locate ANR issues via logs and traces, and details the integration and usage of an ANR detection tool to automatically capture diagnostic data for debugging.

Baidu Intelligent Testing
Baidu Intelligent Testing
Baidu Intelligent Testing
ANR (Application Not Responding) in Android: Causes, Detection, and Debugging

In Baidu's Quality Assurance team, QA engineers focus on discovering, locating, and ensuring proper fixing of bugs. This article shares several ANR (Application Not Responding) case studies and analysis methods.

1. What is ANR

Simply put, ANR occurs when an app becomes completely unresponsive for a period of time, causing Android to consider it an "Application Not Responding" event.

ANR is triggered when any of the following conditions happen (the exact timeout may vary across ROMs):

User input is not responded to within 5 seconds.

BroadcastReceiver's onReceive() runs longer than 10 seconds.

Lifecycle methods of a Service run longer than 20 seconds.

2. Why ANR Must Be Eliminated

Most user interactions require immediate feedback (button state change, scrolling, loading progress). A delay of 0.1 s is already noticeable; an ANR forces users to wait at least 5 seconds, often leading them to close the app.

From a user‑experience perspective, an ANR feels as frustrating as a crash, and it typically indicates that the UI thread is blocked by heavy work.

3. Typical ANR Scenarios

UI thread waiting for a lock held by another thread.

Per‑frame heavy calculations in a game causing CPU overload.

Web view waiting for unstable network data.

IO operations (database, SD‑card) performed on the UI thread, sometimes blocked by hardware issues.

CPU time being taken by other apps, leaving insufficient time slices for the current app.

4. Locating ANR via Logs

When ANR occurs, developers examine Logcat and the trace files located in /data/anr/. The information includes:

Basic info: process name, PID, package name, system build, ANR type.

CPU usage: average CPU load, IO status.

Thread stack traces for the ANR process and related processes.

5. Testing Findings

In regular testing ANR is rarely reproduced because it often appears on low‑end devices, weak networks, or with intensive user actions. When it does appear, locating the cause is cumbersome because it requires rooting the device to access /data/anr.txt and correlating logs manually.

6. Introducing an ANR Detection Tool

Because ANR problems are intermittent, an automatic detection tool was integrated to dump trace files and stack information as soon as the UI thread is blocked beyond a configurable threshold (e.g., 4 seconds).

6.1 Basic Principle

When the main thread exceeds the threshold, the tool dumps the trace file, captures stack information, throws an exception, and stores the data for later analysis.

6.2 Application in Baidu Browser

Step 1: Add anr.jar to the project's libs directory.

Step 2: Initialize the SDK in Application.onCreate():

initSDK(Context context, String appKey, boolean watchdog, int time)

Here time is the ANR detection threshold in milliseconds.

Step 3: Build the APK normally.

Step 4: Test the built APK (e.g., with monkey or custom cases). When ANR occurs the process is killed and logs are saved under /sdcard/lynq_anr.

Example: Baidu Browser startup on a low‑end device shows a long main‑thread block, leading to ANR. The generated trace file can be inspected via DDMS.

Analyzing the trace reveals the most time‑consuming method, which in this case is the initialization of the bookmark database on the UI thread.

Further inspection of the source code confirms that database initialization runs on the main thread, causing the ANR on low‑end devices.

Note: The ANR detection tool described here originates from Baidu's QA team; readers can refer to the principle diagram and examples to implement a similar solution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentAndroidANR
Baidu Intelligent Testing
Written by

Baidu Intelligent Testing

Welcome to follow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.