Debugging
in Android Studio
Last
night I was struggling with my code, which stopped running suddenly(that too without prior notice....not fair. right ??) I was like clueless at that moment. I had no options left but examining the code thoroughly for the third time. I did the same, and the result remained same as well. Yeah...I couldn’t find the bug even after giving it a third try. That's why I love handling compile time errors rather than these runtime or logical ones. At least with compile time errors there is a learning, while in later case it's all your carelessness(in most of the cases). Well, when I had no alternatives left, I thought
let’s try debugging this project. Yes....you heard it right...I had never used debugger
before. Trust me guys....It's an awesome thing to be used. At least in my case, It helped me
a lot. If you are also a beginner like me, then don’t waste your
time in debugging projects by yourself(like I did). Learn how to use “Debugger” instead. It will save both your time and
energy.
Of
course use of log messages is an another way of debugging
your project. But once you will use the debugger, you will get the
difference. I personally used to love using log messages but it
requires some extra efforts which debugger does that for us with just
a click only.
To start debugging,
click Debug
in the tool bar. Android Studio builds an APK, signs it with a debug
key, installs it on your selected device, then runs it and opens the
Debug window.
If your app is already running on a
connected device or emulator, you can start debugging as follows:
-
Click Attach debugger to Android process
.
-
In the Choose Process dialog, select the process you want to attach the debugger to.By default, the debugger shows the device and app process for the current project, as well as any connected hardware devices or virtual devices on your computer. Select Show all processes to show all processes on all devices; the display includes any services that your app created as well as system processes, for example.From the Debugger menu, you can select a different debug type. By default, Android Studio uses the Auto debug type to select the best debugger option for you, based on whether your project includes Java or C/C++ code.
-
Click OK.The Debug window appears. In this case, notice the two tabs to the right of the Debug window title: one tab is for debugging native code and the other for Java code, as indicated by -java.
Separate debugging sessions have
separate tabs and different port numbers, which are displayed in
parentheses in the tab.
4. To end a debugging session, click
the tab for the session, and then click Terminate
.
Note: The Android
Studio debugger and garbage collector are loosely integrated. The
Android virtual machine guarantees that any object the debugger is
aware of is not garbage collected until after the debugger
disconnects. This can result in a buildup of objects over time while
the debugger is connected.
You can find
complete Documentation here