Debug your app in Android (Part 2)

In previous article i have explained about enabling debugging option in Android phone. Today we will see using that device How to set debug pointer in android studio and actually debug your app.

Start debugging 🔴

  1. Set some breakpoints in the app code.
  2. In the toolbar, click Debug
  3. to display the Select Deployment Target window.
  4. Select a deployment target as your device and click OK.
  5. 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,

Attach the debugger to a running app

If your app is already running on your device, you can start debugging without restarting your app as follows:

  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.
  4. 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.
  5. Click OK.
  6. The Debug window appears.

Use the system log

system logs

The system log shows system messages while you debug your app. These messages include information from apps running on the device. If you want to use the system log to debug your app, make sure your code writes log messages and prints the stack trace for exceptions while your app is in the development phase.

Write log messages in your code

To write log messages in your code, use the Log class. Log messages help you understand the execution flow by collecting the system debug output while you interact with your app. Log messages can tell you what part of your application failed.

Work with breakpoints

Android Studio supports several types of breakpoints that trigger different debugging actions. The most common type is a line breakpoint that pauses the execution of your app at a specified line of code. While paused, you can examine variables, evaluate expressions, then continue execution line by line to determine the causes of runtime errors.

To add a line breakpoint, proceed as follows:

  1. Locate the line of code where you want to pause execution
  2. If your app is already running, you don’t need to update it to add the breakpoint — just click Attach debugger to Android proccess
  3. Otherwise, start debugging by clicking Debug
A red dot appears next to the line when you set a breakpoint

When your code execution reaches the breakpoint, Android Studio pauses execution of your app. You can then use the tools in the Debugger tab to identify the state of the app:

  1. 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
  2. To evaluate an expression at the current execution point, click Evaluate Expression
  3. To advance to the next line in the code (without entering a method), click Step Over ⬇️
  4. To advance to the first line inside a method call, click Step Into ↘️
  5. To advance to the next line outside the current method, click Step Out ↗️
  6. To continue running the app normally, click Resume Program ▶️

Inspect variables

The Variables and Watches panes in the Debugger window

In the Debugger window, the Variables pane lets you inspect variables when the system stops your app on a breakpoint and you select a frame from the Frames pane.Begin debugging.

  1. In the Watches pane, click Add
  2. In the text box that appears, type the name of the variable or expression you want to watch and then press Enter.

To remove an item from the Watches list, select the item and then click Remove

You can reorder the elements in the Watches list by selecting an item and then clicking Up or Down.

If you have found this useful Click the 💚 below to show your support and share it with other fellow Medium users. stay tuned..

I’m a Product Engineer at Gojek. I love to share my experiments, learnings via Blogs. https://twitter.com/vrushaliSRaut

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Accident Lawyer Sandy Utah

accident lawyer sandy utah

5 Tips On Avoiding Burnout While Self Learning to Code

C# Events for beginners

Multi Node Spark Setup on Hadoop with YARN

Google Cloud Shell — Resources to get started

Automation using available APIs — Azure DevOps

Android fundamentals 07.1: AsyncTask

Anonymous Puzzle Board — App Update and What I learned by going back to my code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vrushali Raut

Vrushali Raut

I’m a Product Engineer at Gojek. I love to share my experiments, learnings via Blogs. https://twitter.com/vrushaliSRaut

More from Medium

Reactive programming in action — part 1

Global Usings in C#10

What is Server-Sent Events (SSE) and how to implement it?

Setting up a NodeJS API with TypeScript: Part 1