Skip to content
Share
Explore

Android Debugging Lab: Mastering the Debugger in Android Studio

Skill Level: Beginner to Intermediate ​Objective: Learn to use the Android Studio Debugger to track code execution, inspect variables, set breakpoints, and step through code efficiently.

📌 Part 1: Setting Up Debugging

🛠 Step 1: Enable Debugging Mode

Before you can debug, ensure Developer Options are enabled:

On Your Device/Emulator:

Open Settings → About Phone.
Tap "Build Number" 7 times until Developer Mode is enabled.
Go to Developer Options and enable USB Debugging.

In Android Studio:

Open Run → Edit Configurations.
Select Debugger Tab and ensure USB Debugging is enabled.

📌 Part 2: Setting Breakpoints

Breakpoints pause execution at a specific line so you can inspect what’s happening.

🛠 Step 2: Add Breakpoints in Your Code

Open MainActivity.kt in any test app.
Find the onCreate() method.
Click on the left margin (gutter) next to the line below:
Log.d(TAG, "onCreate: Activity initialized")

A red dot will appear—this is your breakpoint.
Run the app in Debug Mode:
Click the 🐞 Debug Button (beside Run button) or press Shift + F9.
The app will pause at your breakpoint.

📌 Part 3: Using the Debugger Panel

When the app pauses at a breakpoint, the Debugger Panel appears at the bottom.

Debugger Tabs Overview

Table 1
There are no rows in this table

📌 Part 4: Stepping Through Code

You can control execution line by line using these step buttons in the Debugger Panel:
Table 2
There are no rows in this table

🛠 Step 3: Try Stepping Through Code

Copy-Paste this function inside MainActivity.kt:
private fun debugExample() {
val numbers = listOf(1, 2, 3, 4)
val sum = numbers.sum()
Log.d(TAG, "Sum: $sum")
}

Set a breakpoint inside debugExample().
Call debugExample() from onCreate():
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate: Activity initialized")
debugExample()
}

Run the app in Debug Mode (Shift + F9).
Use Step Over (F8) and Step Into (F7) to walk through execution.

📌 Part 5: Inspecting Variables and Watches

🛠 Step 4: Modify Variables in Debug Mode

Set a breakpoint inside debugExample().
Run the app in Debug Mode (Shift + F9).
In the Variables Tab, find sum.
Right-click sum → Modify Value, set it to 99, and press Enter.
Resume execution (F9), then check Logcat:
D/MainActivity: Sum: 99

🔥 Lesson: The debugger allows you to change values on the fly!

📌 Part 6: Handling Crashes with the Debugger

🛠 Step 5: Debugging a NullPointerException

Modify debugExample() to cause a crash:
private fun debugExample() {
val str: String? = null
val length = str!!.length // This will cause a NullPointerException!
}

Set a breakpoint on the length line.
Run Debug Mode and trigger the crash.
Check Logcat for the error message:
java.lang.NullPointerException: Attempt to invoke length() on a null object reference

Fix it using safe calls (?.) or null checks:
val length = str?.length ?: 0

🔥 Lesson: Use breakpoints to catch and fix crashes before they reach users.

🔹 Debugging Exercises

Exercise 1: Using Step Over & Step Into
Modify debugExample():
private fun debugExample() {
val numbers = listOf(1, 2, 3, 4)
val sum = numbers.sum()
Log.d(TAG, "Sum: $sum")
}

Set a breakpoint inside the function.
Step Over (F8) and Step Into (F7) to observe the sum calculation.
Exercise 2: Watching Variables
Run the app in Debug Mode.
Right-click sum in the Variables Tab"Add to Watches".
Change the list values (listOf(5, 10, 15)).
Run again and observe how sum updates in real-time.
Exercise 3: Fixing a Bug
Add this buggy function:
private fun buggyFunction() {
val list = listOf(1, 2, 3)
val value = list[5] // Crash: IndexOutOfBoundsException!
}

Set a breakpoint inside it.
Run Debug Mode and fix the issue using:
val value = list.getOrNull(5) ?: -1

🚀 Summary: Debugging in the Android MOJO Methodology

Debugging is not just about fixing errors—it’s about understanding how your code executes.

Key Debugging Lessons:

Breakpoints stop execution to inspect app state. ✅ Step Over/Into/Out lets you control execution. ✅ Watches & Variable Modification helps tweak values without restarting. ✅ Logcat & Exception Handling help identify and fix crashes.
💡 Best Practice: Debug early, debug often!
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.