Running Your App in the Debugger

The SimpleBoom module in the Sampler and SamplerJ projects is very similar to the SimpleButton example we looked at in the chapter on widgets. There are three main differences:

<?xml version="1.0" encoding="utf-8"?>
<Button android:id="@+id/showElapsed"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/elapsed_caption"
  android:textSize="@dimen/hello_size" />
SimpleBoom Sample App, As Initially Launched
SimpleBoom Sample App, As Initially Launched

Sometimes, the stack trace alone will give you enough information to know how to fix the bug. If not, you can run your app in the debugger to see exactly how your code is executing and perhaps get a better sense for your mistake.

Setting Breakpoints

In our Java and Kotlin code editors in Android Studio, the vertical bar on the left is called “the gutter”:

Editor, with Gutter Area Highlighted
Editor, with Gutter Area Highlighted

The author of this book likes having the line numbers visible. To control whether those show up, go to Files > Settings > Editor > General > Apperance — the “Show line numbers” checkbox controls whether the gutter shows line numbers.

Clicking in the gutter on a line of source code will set a breakpoint, which appears as a red dot in the gutter and a pink highlight on the line:

Editor, with Breakpoint
Editor, with Breakpoint

When you run your app using the debugger, when code execution reaches lines with a breakpoint, execution stops and you can use the IDE to learn more about what is going on.

For lines containing lambda expressions, when you click in the gutter to set a breakpoint, you will get a drop-down menu of options for what the breakpoint should affect:

Editor, While Trying to Set a Breakpoint on Line 31
Editor, While Trying to Set a Breakpoint on Line 31

Your options are:

Launching the Debugger

To run the app and have the breakpoints take effect, you need to run it in the debugger. You can do this from the Run > Debug main menu option or the “green bug” toolbar icon:

Android Studio Toolbar, with Debug Icon on Right
Android Studio Toolbar, with Debug Icon on Right

This behaves a lot like when you normally run the app. However, the app will run more slowly, and when the breakpoint is reached, your IDE window will open a Debug tool that shows you what is going on:

Android Studio, Showing Debug Tool at a Breakpoint
Android Studio, Showing Debug Tool at a Breakpoint

Examining Objects

The middle of the Debug tool shows a “Variables” list. This will contain local variables, fields/properties of the current object, and other values that your code might be referencing:

Variables Pane of Debug Tool
Variables Pane of Debug Tool

Simple types, like the Long value nowMs, just show their value. More complex types will show you their type and can be explored in turn using the gray triangle to expand the object tree:

Variables Pane of Debug Tool, Showing Object Contents
Variables Pane of Debug Tool, Showing Object Contents

This can help you understand the data at this point in the code.

Stepping Through the Code

The toolbar towards the top of the Debug tool has a few buttons that let you step through the code to follow along as it gets executed:

Code Step Toolbar Buttons in Debug Tool
Code Step Toolbar Buttons in Debug Tool

From left to right, these are:

Also, there is a tool strip on the left with a few useful buttons:

Tool Strip in Debug Tool
Tool Strip in Debug Tool

Of particular note:


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.