Tech Blog

Android

Different ways of Analyzing ANR in Android

By Raaziya Sultana3 min read
← Back to blog

Using Bug Report:

Reproduce the ANR and Capture the Bug report via Take bug report option from Developer options or via adb bugreport command. Search for Reason for ANR or 

am_anr in the Bug report to find which component it’s pointing to.Analyse the logs  just 5 seconds or 10 seconds before this based on the component it’s referring to , to find the root cause.

Using traces file:

After ANR is recreated pull the traces file via adb pull /data/anr/traces.txt command.

Traces file consists of the states of threads.There are different states of threads such as Waiting,Sleeping, Native, Suspended etc. Mainly focus on Suspended thread and find the reason for suspension. Also try to find the Deadlocks by searching for waiting to lock

An example of Deadlock is mentioned below 

DALVIK THREADS (16):

“main” prio=5 tid=1 Blocked

  ….

  – waiting to lock <0x3a357895> (a android.os.MessageQueue) held by thread 12

  at android.os.Looper.loop(Looper.java:122)

  ….

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

  “SampleHandlerThread” prio=5 tid=12 Blocked

   …..

  at android.os.MessageQueue.next(MessageQueue.java:145)

  – waiting to lock <0x32085e66> (a android.os.MessageQueue) held by thread 1

  at android.os.Looper.loop(Looper.java:122)

In the above example the main thread is held by SampleHandlerThread and SampleHandlerThread  is held by the main thread causing a Deadlock. In case of a Deadlock, something is severely wrong in the code which needs to be worked on.One of the ways of finding issues in the code is by inspecting the code. We can inspect the code in android studio by clicking on Analyze -> Inspect Code and this will prompt us to specify the Inspection scope.We can specify the inspection scope to be a file, module or the whole project and perform the inspection. The inspection results will provide a list of various issues such performance issues, threading issues and other severe issues which can be worked on and resolved.

If Bugreports and trace files are not helpful to find the root cause of the ANR then we can use StrictMode to find out what is done wrong on the main thread of the application.

StrictMode is used mainly to detect the network and disk access operations done on the main thread.

StrictMode code example is mentioned below

public class SampleApplication extends Application
{
   @Override
    public void onCreate ()
    {
        super.onCreate();
        .....

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
             .detectAll()
             .penaltyLog()
            .build());     
     }
}

We can call the StrictMode code in the Application’s onCreate. detectAll() will detect all the possible problems and penaltyLog() will print all the violations done in the logcat.Therefore the logcat has to be monitored for the violations when the application is being used and resolve the violations if they are problematic.

Instead of detectAll() we can specifically use detectDiskReads() and detectDiskWrites()  

to detect disk operations and use detectNetwork() to detect network operations on the Main thread.

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →