top of page
Writer's pictureDon Peter

Detecting and fixing ANR in Android Apps.

Updated: Sep 18, 2022


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 {
        jcenter()
        maven {
            url "https://finotescoreandroid.s3.amazonaws.com/release"
        }
    }
}
    

2. Add dependencies to the app level build.gradle 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.


Fn.init(this);

Java Code: In the onCreate function of Application class.



Fn.init(this)

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.


Finotes reports ANR as and when they occur, unlike Firebase Crashlytics which report only those ANRs which resulted in the user Force Quitting the app and restarting it.


Identifying root cause of ANR

So, how do we identify the root cause of these issues. Head over to Finotes dashboard and look for all issues reported.

ANR reported in Finotes dashboard
ANR reported in Finotes dashboard

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 shorter 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.


One of our customers tells us on how the new improved ANR detection feature helped them identify the root cause. Read it here https://www.blog.finotes.com/post/identifying-root-cause-of-anr-in-android-apps.


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


Watch this space for more updates.

Comments


Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

Monitor & Improve Performance of your Mobile App

 

Detect memory leaks, abnormal memory usages, crashes, API / Network call issues, frame rate issues, ANR, App Hangs, Exceptions and Errors, and much more.

Explore Finotes

bottom of page