• Don Peter

Detecting and fixing ANR in Android Apps.

How to detect and fix ANR in Android Apps

Any developer who has ever written an Android app must have faced the age old issue of 'App Not Responding' popularly known by the acronym ANR. ANRs are UI/Main thread blocks that prevail for more than 5000 milliseconds. Fixing ANRs require three steps much like any other issue.

1. Get to know the existence of the issue.

2. Get accurate data points to identify the root cause of the issue.

3. Get the issue fixed and release a patch version fast.

Unlike crashes, the problem that we developers face here is in step 2. We at Finotes figured out a method that will help us get accurate data points to fix ANRs in Android apps.

Let us get started.

Integrate Finotes.

The first step is to add Finotes SDK to the android application. SDK is capable of automatically detecting and reporting UI thread blocks in Android apps out of the box. SDK comes as a Gradle library, and the integration will take only couple of minutes.

Steps to integrate

1. Add maven tag to your project level build.gradle file.

buildscript {
    repositories {
    dependencies {
        classpath 'com.android.tools.build:gradle:x.x.x'
allprojects {
    repositories {
        maven {
            url "https://finotescoreandroid.s3.amazonaws.com/release"

2. Add dependencies to the app level build.gralde file and sync the project with gradle files.

releaseImplementation('com.finotes:finotescore:x.x.x@aar') {
    transitive = true;

debugImplementation('com.finotes:finotesdebug:x.x.x@aar') {
    transitive = true;

3. Finally, initialize the SDK.


Java Code: In the onCreate function of Application class.


Kotlin Code: In the onCreate function of Application class.

Run the app using Exerciser Monkey.

Exerciser Monkey is an adb command. It executes random touches and gestures in the app, just as if a monkey is using the app. This will help in detecting abnormalities in the app that regular tests may miss.

It is easy to execute Monkey tool. Run this command from the terminal and wait for the execution to complete. Please feel free to customize these command options.

$ ./adb shell monkey -v-v --ignore-crashes --ignore-timeouts --ignore-security-exceptions --throttle 100 --pct-touch 35 --pct-motion 40 --pct-nav 0 --pct-majornav 0 --pct-appswitch 5 --pct-anyevent 5 --pct-trackball 5 --pct-syskeys 0 --pct-pinchzoom 5 --bugreport 11000

Before executing the command, it is a good practise to keep the app pinned to the screen. This is to make sure that the monkey command does not accidentally close your app.

Once the execution is complete you will get detailed report with the number of ANRs encountered. But as I said at the beginning, it is real hard to identify the exact reason for the ANRs purely from these bug reports.

Fixing ANR

So, how do we identify the root cause of these issues. Head over to Finotes dashboard and look for all issues reported. Stacktrace available with each ANR issue report will help us to identify the exact line that triggered the ANR and to fix them.

Custom ANR threshold to detect shorted UI thread blocks.

Now, let us take this to the next level. We can use the @Observe annotation provided by the SDK to set a custom threshold in detecting UI thread blocks. The value can be anywhere between 500 and 5000 milliseconds. Once set, SDK will report UI thread blocks detected above the set threshold. The default value is 3000 milliseconds.

@Observe(ANRThresholdInMilliseconds = 1000)
public class BlogApp extends Application {

Java Code - Setting custom ANR threshold

@Observe(ANRThresholdInMilliseconds = 1000)
class BlogApp: Application() {

Kotlin Code - Setting custom ANR threshold

It is important to note that, monkey tool is not mandatory for Finotes to detect ANRs. We run the app in any device or simulator and it will detect issues.

Visit https://docs.finotes.com for detailed documentation. To know more about Finotes, visit https://finotes.com.

Watch this space for more updates.