
Debugging is an essential part of the development process in Android Studio. To effectively debug your app, you need to understand the different types of bugs that can occur.
A common issue is null pointer exceptions, which can be caused by trying to access a null object reference. This can be fixed by adding null checks to your code.
To debug your app, you should use the Android Studio debugger, which allows you to step through your code line by line and examine the values of variables. This can be done by setting breakpoints and using the debugger's features.
The debugger also allows you to inspect the state of your app at any given time, including the call stack and local variables. This can be useful for identifying the source of a bug.
By following these debugging essentials, you can quickly and efficiently identify and fix bugs in your Android app.
You might like: How to Debug Next Js in Vscode
Running the Debugger
To run your app in debug mode, you build an APK signed with a debug key and install it on a physical Android device or on the Android emulator. To set up an Android device for development, see Using Hardware Devices.
You can debug your app in Android Studio by clicking Debug in the toolbar. On the Choose Device window, select a hardware device from the list or choose a virtual device. Click OK, and your app starts on the selected device.
The Debug tool window opens when you debug your app, showing threads and variables in the Debugger tab, the device status in the Console tab, and the system log in the Logcat tab.
To attach the debugger to a running process, click Attach debugger to Android process. In the Choose Process window, select the device and app you want to attach the debugger to.
You can also use the Android debugger to connect to the running process of the developed application or start it so that it is connected right from the start.
Take a look at this: Debug Azure Function Locally
To use the debugger and debugging features of Android Studio, follow these steps:
- Set Breakpoints: Open your project in Android Studio and navigate to the line of code where you want to start debugging. Click in the left gutter of the code editor next to the line number to set a breakpoint.
- Start Debugging: Click on the "Debug" button in the toolbar or press Shift + F9 to launch your app in debug mode.
- Use the Debugger Controls: Once the app is running and hits the breakpoint, the debugger will pause execution at that line. You can now inspect variables, step through the code, and evaluate expressions.
Remember to connect your device with a USB cable to your computer before starting the debugger, and choose "allow" when prompted to read data through USB.
Debugging Tools
Debugging Tools are a crucial part of the Android Studio experience. They help you identify and fix issues in your code, making it easier to create a stable and efficient app.
To start debugging, you need to set breakpoints in your code. This can be done by clicking in the left gutter of the code editor next to the line number where you want to start debugging. A red dot will appear, indicating a breakpoint has been set.
The Debug tool window is where you can examine variables and control the execution step by step. 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.
You can also evaluate an expression at the current execution point by clicking Evaluate Expression. This is useful for quickly checking the value of a variable or expression.
Here are some common debugger controls:
- Step Over: Click to advance to the next line in the code (without entering a method)
- Step Into: Click to advance to the first line inside a method call
- Step Out: Click to advance to the next line outside the current method
- Resume Program: Click to continue running the app normally
If you started an app by running it, you can attach a debugger to the running app without needing to restart it. To do this, click Attach Debugger to Android Process and select the process you want to attach the debugger to.
The Android debugger is a tool in Android Studio that allows you to connect to the running process of your app or start it so that it is connected right from the start. If you're using a physical device, make sure to enable debugging and install the correct application variant through the studio.
To use the debugger and debugging features of Android Studio, follow these steps:
For more insights, see: Looker Studio Running Count
1. Set Breakpoints: Open your project in Android Studio and navigate to the line of code where you want to start debugging.
2. Start Debugging: Click on the "Debug" button or press Shift + F9 to launch your app in debug mode.
3. Use the Debugger Controls: Once the app is running and hits the breakpoint, the debugger will pause execution at that line.
4. Inspect Variables: While paused, you can hover over variables to see their current values or open the "Variables" window to get a detailed view of all variables in scope.
5. Evaluate Expressions: Use the "Evaluate Expression" feature to evaluate and modify expressions on the fly.
You can also use conditional breakpoints to set conditions under which the breakpoint should trigger. This is useful for debugging specific scenarios.
To view and configure breakpoints, click View Breakpoints on the left side of the Debug tool window. The Breakpoints window lists all the current breakpoints and includes behavior settings for each. You can enable or disable each breakpoint from the list on the left.
Additional reading: How to Use Google Drive for Android
Debugging Features
You can set breakpoints in your code to pause the app at a specific line. Click Rerun to start the app again, and when a breakpoint is hit, Android Studio pauses the app and highlights the breakpoint in the source code.
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, 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.
Here are some key debugger controls:
- To examine the object tree for a variable, expand it in the Variables view.
- To evaluate an expression at the current execution point, click Evaluate Expression.
- To advance to the next line in the code, 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.
You can also use the System Log to view system messages while debugging your app. To write log messages in your code, use the Log class.
Here's an interesting read: Emoji in Text Messages Android
Log code messages
To log code messages, you'll want to use the Log class. This allows you to collect system debug output while interacting with your app, which can help you understand the execution flow and identify where your app failed.
You can write log messages in your code by adding them while your app is in the development phase. This is especially useful for catching exceptions and writing stack traces to the system log.
For example, you might add log messages to determine if previous state information is available when your activity starts.
Note that you should remove debug log messages and stack trace print calls from your code when you're ready to publish your app. You can do this by setting a DEBUG flag and placing debug log messages inside conditional statements.
Here's a quick rundown of how to write log messages:
- Use the Log class
- Add log messages while in development
- Remove debug log messages and stack trace print calls before publishing
Advance
Debugging can be a fun and rewarding experience, especially when you know the right tricks. Inspect an object by hovering over it while paused in the debugger.
You can evaluate expressions on the fly using the "Evaluate Expression" feature, usually accessible via a button or shortcut like Alt + F8. This feature is super useful for getting quick answers or testing out code snippets.
To take debugging to the next level, you can change the value of a variable while paused in the debugger. This can help you test out different scenarios or see how your code reacts to different inputs.
Throwing an exception forcefully from a method is also an advanced technique. From the Frames pane, you can force a method to throw an exception, which can be really helpful for testing error handling code.
Analyze Stacktrace
Debugging your app can be a frustrating experience, but there are tools available to help you analyze and understand the issues. Analyzing stacktraces is a crucial part of this process.
You can turn a text copy of the exception's stack into meaningful information in Android Studio using the Analyze Stack Trace tool. This tool is available in the Analyze menu.
If the reporter has included a text copy of the stack trace in the bug report, you can simply copy and paste it into the tool. The tool will auto-populate with the text and provide a fully annotated version of the stack trace in the console.
A stack trace shows what's going on in your code, highlighting what's from your code base (in blue) and what's not (in gray). You can click on the links to quickly jump through your code base and identify the source of the issue.
Here are the steps to follow:
By following these steps, you can use the Analyze Stack Trace tool to turn a confusing stack trace into a clear understanding of what's going on in your code.
How to Use Debugger Features?
To use debugger features, you need to set breakpoints in your code. Open your project in Android Studio, navigate to the line of code where you want to start debugging, and click in the left gutter of the code editor next to the line number. A red dot will appear, indicating a breakpoint has been set.
You can also set conditional breakpoints by right-clicking on a breakpoint and selecting "More" or "Edit Breakpoint" to set conditions under which the breakpoint should trigger. This is useful for debugging specific scenarios.
To start debugging, click on the "Debug" button in the toolbar or press Shift + F9. This will launch your app in debug mode.
The debugger will pause execution at the breakpoint, and you can inspect variables by hovering over them or opening the "Variables" window. You can also evaluate expressions using the "Evaluate Expression" feature.
If you want to quickly verify that your breakpoint was triggered, you can use the "Breakpoint hit" message to log that the breakpoint has been hit. There's even a quick way to create this sort of breakpoint: just press the Shift key and click in the gutter.
Here's a step-by-step guide on how to use the debugger:
1. Set breakpoints in your code by clicking in the left gutter of the code editor.
2. Start debugging by clicking on the "Debug" button or pressing Shift + F9.
3. Inspect variables by hovering over them or opening the "Variables" window.
4. Evaluate expressions using the "Evaluate Expression" feature.
5. Use conditional breakpoints to debug specific scenarios.
By following these steps, you can effectively use the debugger features in Android Studio to identify and fix issues in your code.
Check this out: Inspect Element on Android
Breakpoints and Stepping
Using breakpoint groups can save you a lot of time and frustration when debugging. You can create a group for each bug you're working on and toggle all the breakpoints on and off with a single click.
To create a breakpoint group, simply select all the breakpoints associated with a bug, right-click, and choose Move to group, then Create new. You can name the group after the bug and easily manage all the breakpoints in one place.
Hitting unrelated breakpoints can be annoying and disrupt your debugging flow, but with breakpoint groups, you can keep your focus on the task at hand.
Breakpoint Groups
Breakpoint groups are a lifesaver when you're debugging multiple bugs at once. You can create a group for each bug and easily toggle all the breakpoints on and off with a click.
Hitting unrelated breakpoints can be frustrating and take you out of your debugging flow. By grouping them, you can quickly turn them off and focus on the issue at hand.
To create a breakpoint group, simply right-click on the unwanted breakpoint, select More, and then move all the breakpoints associated with the bug to a new group. Name the group after the bug you're working on.
This makes it easy to delete all the breakpoints when you're done with the bug. No more cluttering up your list of breakpoints with unnecessary ones.
You can also use breakpoint groups to quickly toggle all the breakpoints on and off. This is especially useful when you need to activate a set of breakpoints, but the activation point is not relevant to you.
Suspend Thread
By default, breakpoints suspend all threads, but you may want to verify that your app works while debugging a specific thread.
You can suspend only the current thread by opening the breakpoint options and selecting Thread in the Suspend settings.
This is useful for testing how your app behaves when a thread is blocked, allowing the rest of your app to keep running.
For example, you might want to see if your UI continues to render while you investigate a background task.
Evaluator and Watchers
To evaluate and log app state information, use the breakpoint options to disable Suspend and enable Evaluate and log. This allows you to add any code expression, which will be evaluated and logged to the console.
You can quickly verify that your breakpoint was triggered by using the "Breakpoint hit" message to log that the breakpoint has been hit. To create this sort of breakpoint, press the Shift key and click in the gutter.
The Evaluate expression feature is also useful for exploring your code more freely. Access this feature from the debugger toolbar and enter any expression in the Expression text input. Press Evaluate to evaluate it, and you can even browse the object in the Result section if you evaluate an object.
The Evaluate Expression dialog may open in single-line mode, but you can easily expand it to multiple lines by clicking Expand. This allows you to enter complex, multiline expressions that can include variables and if statements.
Watchers are a useful tool for observing variable values during debugging. They are displayed on top of the Variables section and can be added by clicking on the + sign and defining a function call. You can also add a watcher by right-clicking on a variable and choosing Add to Watches.
Here are some key things to keep in mind when using watchers:
- Add a watcher by clicking on the + sign and defining a function call.
- Right-click on a variable and choose Add to Watches to add a watcher.
- Watchers are displayed on top of the Variables section.
Track Object Allocation
Tracking object allocation in Android Studio is a powerful tool for assessing memory usage and identifying performance issues. You can start tracking object allocation by running your app in debug mode and following a few simple steps.
To begin, start your app as described in Run Your App in Debug Mode. This will allow you to see which objects are being allocated on the Java heap. Next, click Android to open the Android DDMS tool window.
Select the Devices | logcat tab and choose your device from the dropdown list. Then, select your app by its package name from the list of running apps. Click Start Allocation Tracking and interact with your app on the device. This will give you valuable information about which classes and threads are allocating objects.

Once you've finished interacting with your app, click Stop Allocation Tracking to view the results. Android Studio will show you the objects that the system allocated, including the allocation order, allocated class, allocation size, thread ID, allocation method, class, and line number. You can also see the stack trace at the point of allocation.
Here's a summary of the information you'll see:
- Allocation order
- Allocated class
- Allocation size
- Thread ID
- Allocation method, class, and line number
- Stack trace at the point of allocation
Using Watchers
Watchers are a powerful tool in Android Studio that help you observe variables and their values during debugging. They're displayed on top of the Variables section and can make debugging easier and faster.
You can add a watcher by right-clicking on a variable and choosing Add to Watches. This allows you to define a function call that will be evaluated and displayed in the Watches section.
Watchers can be especially useful when dealing with complex variables, such as nested classes. Without them, you'd need to click through each nested class to observe their values.
Here are some key benefits of using watchers:
By using watchers, you can streamline your debugging process and get to the root of issues faster.
Debugger Configuration
To configure your breakpoints, click on View Breakpoints on the left side of the Debug tool window. This will open the Breakpoints window, which lists all the current breakpoints and includes behavior settings for each.
You can enable or disable each breakpoint from the list, and if a breakpoint is disabled, Android Studio won't pause your app when it hits that breakpoint. You can also configure a breakpoint to be disabled at first and have the system enable it after a different breakpoint is hit.
Here are some key things to know about configuring breakpoints:
- Enable or disable each breakpoint from the list.
- Configure a breakpoint to be disabled at first and have the system enable it after a different breakpoint is hit.
- Set a breakpoint for any exception by selecting Exception Breakpoints in the list of breakpoints.
By configuring your breakpoints, you can control how your app behaves when it hits certain lines of code, making it easier to debug and identify issues.
View and Configure
To view and configure breakpoints, click View Breakpoints on the left side of the Debug tool window. This opens the Breakpoints window, which lists all current breakpoints and includes behavior settings for each.
You can enable or disable each breakpoint from the list on the left. If a breakpoint is disabled, Android Studio won't pause your app when it hits that breakpoint. To configure a breakpoint, select it from the list and make your desired changes.
You can also configure a breakpoint to be disabled at first and have the system enable it after a different breakpoint is hit. This is useful for debugging specific scenarios. To set a breakpoint for any exception, select Exception Breakpoints in the list of breakpoints.
Here are some key things to know about configuring breakpoints:
These settings can be accessed by right-clicking on a breakpoint and selecting "More" or "Edit Breakpoint".
Apply Changes
Conditional breakpoints can be a powerful tool, but they can also cause issues in tight loops, making the application stutter.
If you're experiencing this problem, you can add the conditional expression to your code and use a no-op expression to attach the breakpoint.

You can then use the "Apply Code Changes" feature on Android 8 and above to patch your code with the embedded expression.
However, you might see the method you updated marked as Obsolete in the Frames window, because the debugger is still pointing at old code.
To fix this, you can use the drop frame feature to leave the old method and step into the new one.
Alternatively, you can use the "Apply Changes and Restart Activity" option, which also restarts the activity, useful if you modified layout resources or code in an onCreate method.
Related reading: Android Auto in Old Car
Debugger
To debug an app that's already running, click Attach debugger to Android process. This will allow you to attach the debugger to the running app without needing to restart it.
You can then select the device and app you want to attach the debugger to from the Choose Process window. Once you've selected the process, click OK to attach the debugger.
To view and configure breakpoints, click View Breakpoints on the left side of the Debug tool window. The Breakpoints window will list all the current breakpoints and allow you to enable or disable each one.
You can also configure a breakpoint to be disabled at first and have the system enable it after a different breakpoint is hit. Additionally, you can configure whether a breakpoint should be disabled after it's hit.
To set a breakpoint for any exception, select Exception Breakpoints in the list of breakpoints.
Here are the basic steps to debug your app with breakpoints:
- Set breakpoints in your code
- Click Rerun to start the app again
- When a breakpoint is hit, Android Studio pauses the app and highlights the breakpoint in the source code
- You can then examine variables and control the execution step by step using the Debug tool window
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.
You can also evaluate an expression at the current execution point by clicking Evaluate Expression. This will allow you to see the value of the expression in the current context.

To advance to the next line in the code, 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.
Here are the basic debugger controls:
Debugging Workflow
To start debugging your app, you can attach the debugger to a running process by clicking "Attach debugger to Android process" in Android Studio.
You can also start debugging from scratch by setting breakpoints in your code. To do this, open your project in Android Studio, navigate to the line of code where you want to start debugging, and click in the left gutter of the code editor next to the line number. A red dot will appear, indicating a breakpoint has been set.
Once you've set your breakpoints, click on the "Debug" button (usually represented by a bug icon) in the toolbar or press Shift + F9 to launch your app in debug mode.
When your app hits a breakpoint, the debugger will pause execution at that line. You can now inspect variables, step through the code, and evaluate expressions.
To inspect variables, you can hover over them to see their current values or open the "Variables" window to get a detailed view of all variables in scope.
Here are the debugger controls you can use:
To use conditional breakpoints, right-click on a breakpoint and select "More" or "Edit Breakpoint" to set conditions under which the breakpoint should trigger.
Finally, to run your app in debug mode, you can build an APK signed with a debug key and install it on a physical Android device or on the Android emulator.
Frequently Asked Questions
How do I get to the Android debug menu?
To access the Android debug menu, go to Settings > About device and tap the Build number 7 times to enable Developer options. From there, you can enable USB Debugging and other useful features.
Featured Images: pexels.com

