Menu Close

Debugging with android studio in 3 simple steps

Today we will look at how to debug an android application in android studio. Debugging is one of the most crucial concepts you need to learn before you start developing android applications (or any kind of programming for that matter). It can help you save a ton of time especially when you are developing really big applications.

1) Adding breakpoints

The first step is to add a breakpoints. A breakpoint tells android to pause execution at a particular line in you code. Go to the line in your code where you want to attach the breakpoint and click next to the line number. A red circle shows up and the whole line gets highlighted in red. You can see this in the image below. So when you run your application, the code will pause execution at this point (in the image below, execution will pause at line 14 i.e code will be executed till line 13).


 

2) Running the code in debug mode

The next step is to run your code in debug mode. If you are familiar with shortcuts, then Ctrl + R simply runs the code. To run the code in debug mode, use Ctrl + D (control + D in mac). You can also click on the debug icon   which is next to the run icon . Then select the device/emulator you want to run the application in. When your code reaches the breakpoint, it pauses execution and opens up the debug window.

3) Understanding the debug window

This is the most important step in debugging with android studio. The debug window is where the magic happens. In the image below, you can see the debug window and the 4 important debug actions that you will use :

Step Over   

Step Into .

Resume Program

Stop .

Let’s look at the 4 actions in detail :

Stop

Does exactly what the name suggests : stops the execution of the program and ends the debug mode.

Resume Program

Resumes the execution of the program and pauses at the next breakpoint. This is not to be confused with the Run option  . Resume program is a debugger action. The code still executes in the debug mode. If there is a breakpoint somewhere later in the code, then the code pauses there.

Step Over

This is the most often used command in debugging. Like the name suggests, step over steps over the current line of execution. Meaning that it executes the current line ( i.e the line with the breakpoint) and pauses in the next line of code. Note that the next line does not need to have a breakpoint. For example in the image above, we have a breakpoint at line 14. When we step over, android executes line 14 and pauses at line 15. 

Step over steps over each line of code, irrespective of whether the next line has a breakpoint or not.

Step Into 

Again, like the name suggests, this action steps into a function. For example in our code above, the execution is stopped at line 14. The code at line 14 is a call to function1(). If you perform Step Into action, the debugger enters function1 and pauses execution at line 19, the first line of function1. Remember how Step Over on the other hand, doesn’t enter the function. Instead it executes function1 and pauses at line 15 where function2() would be executed. This is the primary difference between Step Over and Step Into.

 

If you need a more detailed explanation, checkout this video:

You can also read google’s official documentation for more details.

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *