Use the debugger in Android Studio¶
Before you begin¶
This codelab teaches you how to use the debugger in Android Studio to inspect what happens in the Dice Roller app at runtime.
The debugger is an essential tool that lets you inspect the execution of the code that powers your Android app so that you can fix any bugs in it. It lets you specify points at which to suspend the execution of the code and manually interact with variables, methods, and other aspects of the code.
Prerequisites¶
Basic familiarity with Android Studio
Ability to create and run a basic Jetpack Compose app in Android Studio
Completion of the Create an interactive Dice Roller App codelab
What you’ll learn¶
How to attach the debugger to an Android app.
How to launch an app with the debugger attached.
How to use some fundamental features of the debugger.
What the debugger is typically used for.
What you’ll need¶
A computer with Android Studio installed
The solution code for the Dice Roller app in Compose
Eye Power¶
Starter code¶
https://github.com/google-developer-training/basic-android-kotlin-compose-training-dice-roller
Branch: main
Clone:
$ git clone https://github.com/google-developer-training/basic-android-kotlin-compose-training-dice-roller.git
Run the debugger¶
There are two ways to run the debugger alongside your app:
Attach the debugger to an existing app process that runs on a device or emulator.
Run the app with the debugger.
Both ways accomplish the same thing to a certain extent. Once you’re familiar with both ways, you can pick the one that you prefer or whichever one is required.
Attach the debugger to an app process¶
First, run your app.
Click Attach Debugger to Android Process.
A Choose Process dialog opens in which you can choose the process to which you want to attach the debugger.
Select com.example.diceroller, click OK.
A Debug pane appears at the bottom of Android Studio with a message that indicates that the debugger is attached to the target device or emulator.
You attached the debugger to your app! Deets to be covered later. Next, you learn how to launch an app with the debugger already attached.
Run the app with the debugger¶
If you know that you want to use the debugger from the start, you can save time when you run the app with the debugger. Furthermore, if you want to debug code that only runs when the app launches, you need to launch the app with the debugger already attached.
To run the app with the debugger, in the Debug pane, click Stop, which should close the app on the emulator.
Click Debug ‘app’.
The same Debug pane appears at the bottom of Android Studio with some console output.
Use the debugger¶
The Debug pane¶
You likely noticed that there are quite a few buttons across the top of the Debug pane, but these buttons don’t mean much right now, and most are grayed out and unclickable. This section covers the more-commonly used features found in the debugger. This codelab explains the other buttons as they become relevant.
When you first launch the debugger, you see a number of buttons in the Debug pane. At the top of the Debug pane, you see the Debugger and Console buttons. On some versions of Android Studio, instead of Debugger, you see Threads & Variables. They are the same.
The Console button displays the logcat output of the app. If you have any log statements in your code, the output displays as that piece of code executes.
The Debugger (or Threads & Variables) button displays three separate panes, which are empty right now because you aren’t using the debugger:
Frames display
Evaluation and watch expression entry
Variables pane
Use common debugger features¶
Set a breakpoint¶
One of the main features of the debugger is that it lets you stop execution on a specific line of code with a breakpoint.
To set a breakpoint in Android Studio, you need to navigate to a specific line of code and then click in the gutter next to the line number. To unset a breakpoint, you need to click an existing breakpoint in the gutter to make it disappear.
Example: here a breakpoint is set at where
DiceRollerApp()is called.
Try it yourself. Set a breakpoint where the
imageResourcevariable is set, i.e. this line of code:val imageResource = when(result) {
Inspect variables¶
Earlier in the codelab, there was a brief description of the Variables pane, which provides a more in-depth explanation of how to inspect the variables shown in the pane to help you debug issues in your app.
Click the breakpoint to remove it from where the
DiceRollerApp()function is called, but leave the breakpoint where theimageResourcevariable is set.Click Debug ‘app’. You should see that the
result$delegatevariable is aMutableStatewith a value of 1. That is because when the variable is defined, it is instantiated with amutableStateOf1.MutableStatemeans that the result variable holds a state that can be changed.Note
The
result$delegatevariable in the Variables pane refers to theresultvariable in the code. The$delegatenotation is there because the result variable is arememberdelegate.
Click Resume Program.
In the app, click Roll. Your code suspends at the breakpoint again and you may see a different value for the
result$delegatevariable.In this image, the mutable state of the
result$delegatevariable holds a value of 2. This demonstrates how you can inspect variables at runtime with the debugger. In a more full-featured app, the value of a variable could potentially cause a crash. When you use the debugger to inspect variables, you can gain more insight into the details of the crash so that you can fix the bug.
Conclusion¶
Attach the debugger to an app.
Launch an app with the debugger already attached.
Gain familiarity with the debugger pane.
Set a breakpoint.
Resume the program from the debugger.
Use the Step Into button.
Use the Step Over button.
Use the Step Out button.
Inspect variables with the debugger.