Monday, June 29, 2015

Advanced exception debugging using Android Studio

Standard
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: ashwani.in.blogdemo, PID: 5324
    java.lang.ArithmeticException: divide by zero
            at ashwani.in.blogdemo.MainActivity$1.onClick(MainActivity.java:25)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

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.
Thanks,
Ashwani.

0 comments :

Post a Comment