Home Android & Kotlin Books Android Debugging by Tutorials

Watches & Evaluating the Code Written by Vincenzo Guzzi

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

A major part of debugging is interacting with and changing variables when your code is in a suspended state. This allows you to manipulate responses to simulate different scenarios and see how your app will react. The Variables pane inside the Debug window allows you to perform this manipulation of variables by providing useful tools, which you’ll be exploring in this chapter.

You’ll learn how to:

  • Read and use the Variables pane.
  • Make important variables more visible by using flagged and the Watched pane.
  • Evaluate code at runtime.
  • Change the values of variables to find bugs.
  • Augment code with Force Return and Throw Exception.

Exploring The Variables Pane

The Variables pane, like everything else in the Debug window, is only viewable when your code is suspended. This pane contains all of the information about the variables that your current context can view. Within this pane, you can also manipulate the data by setting values, finding usages, or pinning important information.

To start, open the Podplay starter project in Android Studio. Open EpisodeListAdapter.kt, and add a breakpoint on the first line inside onBindViewHolder() so you can suspend your code inside this method.

Run the app in debug mode. Once PodPlay has loaded, tap the search icon, enter the term “All about android” and open the All About Android (Audio) podcast.

Your program is now suspended inside onBindViewHolder() where you placed your breakpoint. The Debug window automatically opens when your code suspends. If it doesn’t, open it manually by clicking View ▸ Tool Windows ▸ Debug.

You’ll locate the Variables pane to the right of the Debug window:

Each line inside the Variables pane is a variable; these can be primitives or objects. The first variable you see is this.

In Android, this refers to the scoped context. In this case, it’s the context of EpisodeListAdapter. The Variables pane will always show the scoped context as the first variable in the list. That way you can find out what context is accessible from wherever your code is suspended.

There’s a chevron located to the left of this, which means that it’s an object containing additional values. Click the chevron to expand this and see its values.

Each variable in the pane provides the variable’s name and its value. If it’s an object, it will display metadata to help you understand what type of object it is.

In the second row of this is a variable called episodeViewList. This is what the row shows:

  1. A chevron that you can click to expand the object and see the variables inside.
  2. The variable identifier — this one indicates that episodeViewList is a final field.
  3. The variable name.
  4. The variable type.
  5. The object ID.
  6. The variable value.

In this case, the local scoped context (this) will always appear in the first row of the Variables pane. The following variables in the list will be the ones that are either method parameters or ones created within the current scope.

The following two variables in the list are holder and position. Both of them have been passed into onBindViewHolder() as parameters.

Click Step over twice to execute the next two lines of code.

You’ll now see that two new variables have appeared in the Variables pane; episodeViewList and episodeView. These local variables have just been created in the two lines you just executed.

Drag and drop your breakpoint to your new line, so future suspends will occur on this line:

Pinning and Watching Variables

The Variables pane is often full of information. The pane itself doesn’t know what variables are most important to you. However, it does provide a way for you to let it know.

Evaluating Code

Whenever your code is in a suspended state, you have access to retrieve and manipulate everything available to your current scope. The most instantaneous way to manipulate your code is with Evaluate Expression.

Augmenting Code

Code augmentation is the act of changing the value of a variable to something different from its real value. You can use this to simulate different data responses and evaluate how your app UI responds.

position == 1

if (podcastViewModel.activeEpisodeViewData?.mediaUrl?.isEmpty() == true) {
  Log.d("test", "MediaURL is empty, unable to play podcast episode.")
  Toast.makeText(context, "Unable to play podcast episode, media URL missing.", Toast.LENGTH_SHORT)
import android.util.Log
import android.widget.Toast

Force Return

Changing the values of variables during code suspension is a powerful debugging tool, but what if you wanted to augment the returning value of a method? You can do this with Force Return.

PodcastViewData(podcast.id != null, "", podcast.feedUrl, "", podcast.imageUrl, episodesToEpisodesView(podcast.episodes))

Throwing Exceptions

You can also augment code by throwing exceptions. Throwing rogue exceptions at your program is a great way to see how resilient your code is.

mediaPlayer.playbackParams = mediaPlayer.playbackParams.setSpeed(speed)


Key Points

  • Use the Variables pane to observe your local and context-accessed variables.
  • Use Variable pinning and the Watches pane to keep important variables in view.
  • You can use Evaluate Expression to debug your code logic without re-running your app.
  • Use Force Return and Throw Exception to debug how your app behaves in unusual scenarios.

Where to Go From Here?

You’ve learned how to observe and change variables within Android Studio to create better, more resilient code, and you’ve learned how to use the Variables pane to help you find and fix problems, but there’s still more to learn!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

© 2022 Razeware LLC

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.