5 Debugging Tips with Android Studio

Travis P
Black Lens
Published in
4 min readDec 4, 2017

--

One of the most essential skill for us developers is debugging. Sure, there are times when we get lost in debugging, maybe we can’t figure out who calls a function or maybe the stack trace is misleading. Well, these tips should help.

Attach Debugger

I had been doing a full debug run for so long, longer than I care to admit. Until one day I decided to click this little button called Attach Debugger. It allows me to debug a currently running app — no wasting time performing a debug run anymore. I’m sure most of you know of it, but for those who don’t, this is the button.

Conditional Breakpoints

When you debug a loop and somehow you want to inspect the 6th iteration. Typically you will place a breakpoint inside a loop then you manually hit resume 5 times to get where you want.

With conditional breakpoints you can specify a condition to a breakpoint and the IDE will suspend only when the condition is true. This can be done in 3 simple steps.

  1. Place a breakpoint.
  2. Right click on the breakpoint and fill in a condition.
  3. Debug like you normally would and find it amazingly stop only when the condition is met.
Simply place a breakpoint
Right click on a breakpoint and specify a condition
Only suspend on a condition

Exception Breakpoints

This is super handy when a stack trace betrays you and you have no idea where the exception is thrown from. Instead of try placing breakpoints at the lines you think it is. You can tell the IDE to suspend whenever an exception is thrown. Here’s how.

  1. Run > View Breakpoints or cmd + shift + F8 to bring up Breakpoints window.
  2. Hit + then select Java Exception Breakpoint.
  3. Choose/search an exception of interest.
  4. Specify any other options if you want and make sure the breakpoint is enabled.
  5. Debug like you normally would and find it break on an exception like magic!
cmd + shift +F8
Java Exception Breakpoints (even if you write Kotlin)
Find the exception that has been troubling you
Lots of options!
There it is

Call Stack

Have you ever wondered “How the hell did this function get called? I don’t remember call it anywhere around here.” No worries, you’ll find out — easier than ever.

Whenever you suspend at a breakpoint, there is a small tab called Frames. It lets you see where the function call is originated from.

For example, the current breakpoint is at setUpRecyclerView() which was called in setUpWidgets() which was called in onCreate(), and so on.

You can travel through call stack and can see variables in its scope.

You can even travel back to the main function itself!

Evaluate Expression

Inspecting variables while debugging is very useful, you get to see everything in the scope at that moment! But what if you want more? What do you do?

Well, you could print something out, right? This is classic. Just add a few lines of code and rerun. If that’s not enough, just add a few more lines — easy peasy.

Or… you could run arbitrary piece of code while suspending at a breakpoint, in the middle of an execution! Yes, that is what Evaluate Expression does.

  1. When you hit a breakpoint, click evaluate expression button in debug window or press opt + F8.
  2. Type any expression you want, hit Evaluate and see the result. Remember that an expression has to return something.
  3. If only one expression doesn’t satisfy your need, click Code Fragment Mode. Now you can run a snippet!
The button is there all along
Result from an expression
Code fragment mode
println() is shown in console tab
You can do a lot of things on a break(point)

That’s about it, guys. Have fun debugging!

--

--

Travis P
Black Lens

Android Developer, Kotlin & Flutter Enthusiast and Gamer