Overriding Variable Contents in the Debugger
Debugging is always a very important feature of an IDE. One useful tip that I don’t hear much about while debugging is being able to change the contents of a variable in the middle of debugging session. If the debugger is halted on a line you simply need to execute the Evaluate Expression Command(⌥+F8). From there you simply run any valid java command it will update the contents of your variable. It’s not limited to assignment, you can evaluate any java expression.
One of my peeves with debugging in Android Studio is that the debugger sometimes jumps to seemingly strange lines. In the youtube video below, the condition is met for the if statement so the inner code block gets execute, but for some reason the debugger hits the final return as well.
Stack Overflow mentions this is a bug with dx, as only 14 some people have stared this issue I’m doubtful it will get addressed soon(Please star the issue). One insightful comment in the issue is that this bug does not exist in the new Android Tool Chain Jack and Jill. I tested it out and sure enough I could not repeat the same problem. Unfortunately Jack and Jill are far from being ready, as annotation processing doesn’t yet work. In the second part of the video we see that this problem doesn’t exist.
One of my favorite features in Microsoft Visual Studio was the ability to move your current code execution line backwards, simply by click and dragging to backwards. It was super useful to rerun previous lines. Coupled with the ability with being able to change variable values you could run a section of code multiple times under different circumstances in the same debug session. From what I’ve read this isn’t available on Android. It is sort of available in intellij. I say “sort of” because it appears that it isn’t as elegantly implemented and you don’t have fine grained control to go back one single line. You can however go back to the start of the method and then use one of the run to cursor methods to get back to the previous line. From the line that is being debugged you simply need to execute the Drop Frame command(From the Run Menu > Drop Frame) this will take you back to before the method call. Hopefully some day we will get this feature in Android.