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 🔴
- Set some breakpoints in the app code.
- In the toolbar, click Debug
- to display the Select Deployment Target window.
- Select a deployment target as your device and click OK.
- 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:
- Click Attach debugger to Android process
- In the Choose Process dialog, select the process you want to attach the debugger to.
- If you’re using an emulator or a rooted device, you can check Show all processes to see all processes.
- 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.
- Click OK.
- The Debug window appears.
Use the system log
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:
- Locate the line of code where you want to pause execution
- If your app is already running, you don’t need to update it to add the breakpoint — just click Attach debugger to Android proccess
- Otherwise, start debugging by clicking Debug
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:
- 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
- To evaluate an expression at the current execution point, click Evaluate Expression
- To advance to the next line in the code (without entering a method), click Step Over ⬇️
- To advance to the first line inside a method call, click Step Into ↘️
- To advance to the next line outside the current method, click Step Out ↗️
- To continue running the app normally, click Resume Program ▶️
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.
- In the Watches pane, click Add
- 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..