Understanding Android ANR: Principles, Detection, and Debugging (WeChat Bug Case Study)
This article explains the Android ANR mechanism, common scenarios that trigger it, how the system detects and logs ANR events, and provides step‑by‑step methods for locating and fixing ANR issues, illustrated with a recent WeChat bug example.
Recently a WeChat Android bug caused an ANR (Application Not Responding) when a message containing two digits followed by fifteen Chinese characters and a period was sent, bringing ANR issues back into focus.
The article introduces the ANR principle, explains what ANR is, and lists typical scenarios that can cause it, such as Service timeout, BroadcastReceiver timeout, ContentProvider timeout, and InputDispatching timeout.
It describes how Android detects ANR using a message‑driven timeout mechanism, using Service as an example: a timeout message is posted, and if the service does not finish within the allotted time, the message triggers an ANR.
Code snippets (shown as images) illustrate where the timeout message is posted and how it is removed when the service completes, preventing the ANR.
The system_server process contains a Handler named "ActivityManager" that receives the timeout message and executes the ANR handling logic, which includes updating CPU stats, writing event logs, collecting thread traces, saving data to the dropbox, and deciding whether to kill the process or show a dialog.
The article enumerates the logs generated during an ANR (event log, main log, dropbox log, trace log) and explains how to locate and analyze them using logcat, trace files, and dropbox files.
For debugging, it suggests analyzing trace files, pulling them from the device, and examining stack traces. When source code is unavailable (as with WeChat), it recommends reverse‑engineering techniques such as decompiling the APK with baksmali or dex2jar and using tools like SmaliIDEA or JD‑GUI to locate problematic code.
To avoid ANR, the article advises eliminating long‑running operations on the main thread and using asynchronous mechanisms like AsyncTask, Thread, or Handler to offload work.
Finally, the author, Hong Zhiyuan, a Baidu MTC R&D and testing engineer, is introduced with a brief bio.
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.
