Debugging is the process of finding and resolving the defects or the problem in a computer program or a software. Basically, programs needs to be debug when codes are not working as desired. Those defect call be called as errors in Computer Programming World.
In Android Studio:
Android studio provides debugging Tools which can be really handy in debugging process.We need some understanding before getting hands on it.
So let’s get started..Android Studio provides a debugger that allows you to do the following and more
1. Select a device to debug your app on.
2. Set breakpoints in your Java, Kotlin, and C/C++ code.
3. Examine variables and evaluate expressions at runtime.
Breakpoint : Breakpoint is a line of code where you want to pause your code execution.
Enable Debugging
Before you can begin debugging, you need to prepare as follows:
Install LLDB
If your project includes C/C++ code, you need to install LLDB from the SDK Manager.
Enable debugging on your device
If you're using the emulator, this is enabled by default. But for a connected device, you need to enable debugging in the device developer options.
Run a debuggable build variant:
Generally when we create android project we have two build variant and default is debug and other is release build variant. To check your build variant there is build variant panel at lower left of Android Studio
This is how a build variant dialog looks like after you click on Build Variants in lower left corner of the Android Studio.
If you have your own build variants:
You must use a build variant that includes debuggable true in the build configuration which can be accessed from build.gradle. Usually, you can just select the default "debug" variant that's included in every Android Studio project (even though it's not visible in the build.gradle file). But if you define new build types that should be debuggable, you must add `debuggable true` to the build type.
Start debugger
Now we are ready to start our debugger if this is the first time you can follow below steps else if the app is already running follow app is already running section.
Running app and debugger for first time
1.Set breakpoints:
Set some breakpoint in your code. To set breakpoints just click on gutter space on bar with line numbers.The red circles are the breakpoints. It should look something like this
2.Start debuggin tool:
In the toolbar, click Debug to display the Select Deployment Target window
If no devices appear in the Select Deployment Target window after you click Debug, then you need to either connect a device via USB or click Create new virtual device to use the Android Emulator.
Select Deployment Targetwindow, you see a dialog asking if you want to "switch from Run to Debug," that means your app is already running on the device and it will restart in order to begin debugging. If you'd rather keep the same instance of the app running, clickCancel Debug attach the debugger to a running app. and instead If, instead of the
3.Select a deployment target and Click OK
Android Studio builds an APK, signs it with a debug key, installs it on your selected device, and runs it.
4.Debug Window appears
After the app is installed successfully in debug mode, you get a automated pop up panel of debugger at the bottom of the android studio. It looks something like this:
If the Debug window is not open, select View > Tool Windows > Debug (or click Debug in the tool window bar), and then click the Debugger tab.
Run/Attach debugger if app is already running
1. Click Attach debugger to Android process .
2. In the Choose Process dialog, select the process you want to attach the debugger to.
3. If you're using an emulator or a rooted device, you can check Show all processes to see all processes.From the Debugger drop-down 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.
4. Click OK.
5. The Debug window appears.
Know your Debugger Window
To examine the object tree for a variable, expand it in the Variables view. If the Variables view is not visible, click Restore Variables View .
1. To evaluate an expression at the current execution point, click Evaluate Expression .
2. To advance to the next line in the code (without entering a method), click Step Over .
3. To advance to the first line inside a method call, click Step Into .
4. To advance to the next line outside the current method, click Step Out .
5. To continue running the app normally, click Resume Program .
Debugging in Android Studio
Senior Mobile Developer