Monday, June 29, 2015

Advanced exception debugging using Android Studio

Hello All:

During application development we often encounter situations where we need to debug our application by attaching the running application as a process to Android Studio.
In this post I will some settings/ features offered by Android Studio which can help us in debugging applications.

I am using following version of Android Studio:

Tip #1: Configuring Android Studio Debugger to break on a uncaught exception

By default Android Studio debugger does not break on uncaught exceptions. To enable this feature we need to do some tweaks in settings.
Go to Run-> View BreakPoints 

Then replicate the following settings

Try running the application in debug mode, you will see the debugger breaks when ever we have uncaught exceptions.

Tip #2: Analyzing stack trace using Android Studio

In our applications we ofter use external libraries to report caught and uncaught exceptions. We can receive the stack trace using the web interface which is often provided by these libraries. Android studio has a very good feature of analyzing these stack trace and point to the culprit line of code.

Suppose following is the stack trace which we received externally.
06-29 10:24:05.479    5324-5324/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process:, PID: 5324
    java.lang.ArithmeticException: divide by zero
            at android.view.View.performClick(
            at android.view.View$
            at android.os.Handler.handleCallback(
            at android.os.Handler.dispatchMessage(
            at android.os.Looper.loop(
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(

Open the project of which you have the stack trace.

Now if we want to pin point the culprit code we can either go through the entire stack track (which is easy in current case but can be messy) or we can use Android Studio's Analyze

Click on this and it will pop-up following window

Hit OK, and you will the stack trace in red with clickable link to Java files.

Hope this helps. Happy coding.

Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.


Post a Comment