An article to let you understand Android Debug debugging

During the special period of the epidemic, I hope everyone will pay attention to safety and Wuhan, come on. The article is mainly for recording and can help students who don’t understand

1. Put a breakpoint and start the debug mode

First, slightly to the right of the number of lines of code where we need to break, click the left mouse button, as shown in the figure:


Click the small crawler button to start the debug mode.




After running successfully as follows. You can see that in the red box, the methods run sequentially from bottom to top have been blocked in the method where we interrupted; the green section shows the values ​​of variables and parameters in the current blocking method.



2. Next, we break down each button operation of debug together

2.1、Step Over(F8)

The meaning of this button: the program executes to the next step, but it should be noted that this button will not actively enter the method body, but will directly run the complete method and then directly run the next step.

For example: the debugger I am currently running, if I keep clicking this button, he will execute add() in the onCreate() method, then execute sub(), and then end directly, and will not enter the add and sub methods Go to print.



2.2、Step Into(F7)


The meaning of this button: the program is executed to the next step. The difference from Step Over is that if the line has a method call and is a custom method, it will run into the custom method (the method will not enter the official class library).

For example: I am currently running debug, if you want to enter the add() method, then click Step Into. If in my add() method, other custom methods are called, if you want to enter each method to view at this time, continue to use Step Into. If you only want to stay in the add() method, other methods only need If you get a return value, you should use Step Over at this time;



2.3、Force Step Into


You can also see it literally: you can enter any method including the official class library. Generally I think this is more suitable for studying the source code.



2.4、Step out


If debugging is in the add() method at this time, if we feel that there is no problem with the add() method and want to jump out of this method to continue debugging other breakpoints, then click Step out to jump out of this method.



2.5、Run to Cursor


Literally, he means moving to the next breakpoint. The test is as follows:

1. If we are currently running debug, if it is still in the onCreate() method, and the current breakpoint is in the add() method, click Run to Cursor, the breakpoint will indeed move to the next breakpoint and stay in the sub() method .

2. If we have entered the add() method body at this time, and click Run to Cursor, we will see that it continues to block after running the for loop once. As shown below:


So I conclude that Run to Cursor is in the body of the current method and runs to the next breakpoint. (If there is an error, please correct it in time);

So at this time, if we are already in the add() method, we just want to run directly to the next breakpoint sub(), how do we do it? Click the Resume Program

button regardless of whether you are in the method or not, and go directly to the next breakpoint.



3. Advanced Debug usage.

3.1、Watches

If we are debugging, there may be many variables, and we want to observe a few variables. We can add him to watches. For example, the variable i in my add() method,

Method 1: In the Variables we observe, find the variable and right click, select Add to Watches

Method 2: On our Watches interface, click the + sign, enter i in the input box to search, and it can also be added to Watches to facilitate our debugging

3.2、Set Value

For example, in our add() method, there is a for loop. Normal debugging is to debug from i=0 every time. If we want to debug directly from i=5, then we can use the Variables interface, Find the value of that variable, right-click and select Set Value, and enter 5 to skip the first few loops.

3.3, view all breakpoints

If you hit a lot of breakpoints and forget to cancel during development, you can click View Breakpoints to view all breakpoints and

open the following interface:

3.4, stop debugging

3.5. How to add debug debugging for the program that has been running to avoid re-running the program.

Here are a few more words, because it is to add debug debugging to the program that is already running, then for example, when entering a page, the code in the onCreate() method has all been executed, such as the method that needs to be run when we click a button, In this method, we can add debug debugging. For example, first is a normal running program, we add breakpoints to the click event.

Then, click Attach Debugger to Android Process

The following page pops up, just click OK, click the button to start debugging

Guess you like

Origin blog.csdn.net/leol_2/article/details/104516670