Overview of the Android Studio debugging tool

Source: Internet
Author: User

Preface: Writing code inevitably has a bug, usually in addition to the most direct debugging method is debug;when a bug occurs in our program, debugging can quickly find the bug. into the debug state, we can clearly understand the entire process of execution of the program, memory data can be monitored. Here is a brief summary of the basic use of debugging and some debugging techniques. This article is by looking for the online multi-resource collation,If there is a fallacy, please point out, I hope this article can help you. First, insert a breakpointSelect the line of code where you want to set the breakpoint, and click the left mouse button after the area of the row numberSecond, enter the debugging stateWhen the breakpoint is set, click the Small Bug (Debug) in the toolbar to enter the debug state.

 when an app enters the debug state, Android studio pops up such a debug window, the debugger state. We are here to monitor and debug our programs.
The debug view appears below the IDE, and the method used by the program to execute to the breakpoint is displayed in the ① area, the sooner the following method is called, and the ② area can assign a value to the specified variable (the left mouse button selects the variable, the right-click popup menu selects the SetValue ... )。 This feature allows you to detect your conditional statements and loop statements more quickly. A specific variable can be monitored in the ③ area, which is discussed again in the Advanced Debugging section.
Three, the commonly used debugging mode and shortcut keys1. Common debugging functions and shortcut keys:
    • Step Into (F7): Enter sub-function
    • Step Over (F8): crosses the sub function, but the child function executes
    • Step Out (Shift + F8): Jump out of child function
    • Run to Cursor (ALT + F9) runs to the location where the cursor is located.
    • Show execution Point (Alt + F10) quickly locates the location of the current debug and highlights the row.
2. Debug function explanation
    • Step into: Just step into, encounter the child function to enter and continue stepping, for example, when you execute to System.out.println ("XXXX"), Using this feature will go into the System.out.println method of the class of the Println method (of course, do not need to do things, if you want to jump out of the execution step out on it.) )
    • Step over : In the case of a single step execution, when the child function is encountered within the function will not go into the sub-function of stepping, but the entire execution of the child function and then stop, that is, the whole function as a step. For example, in the above example,System.out.println ("XXXX") after execution is jumping into the next statement without jumping in, this function is more commonly used, always press F8 on it.
    • Step out: But when stepping into a child function, you can execute the remainder of the child function with step out and return to the previous layer function.
    • Run to Cursor: Runs to the cursor location, after performing this function, no matter where you go, the program can be executed to the line where your cursor is located.
    • Show execution point: When you don't know where the program is currently executing, you can use this feature, and Android Studio jumps to the interface where the line is executing and highlights the row.
Four, advanced debugging functionThe above debugging method is only simple debugging, the following describes some of the more large debugging functions. 1. Evaluate Expression    This is a useful feature that allows you to enter an evaluation environment directly at a breakpoint where you can perform any expression you are interested in:
For example, there is an object at the breakpoint, and if you want to see one of its properties is simple, you can see it in the Debug window, but what if you want to execute one of its methods to see what the result is? With this can be achieved. Of course, its function is far more than this, the equivalent of directly into a REPL environment, very practical. Forgot to say, shortcut key Alt + F8:P 2, conditional breakpoint     Suppose your breakpoint is in the loop of a list, but you are only interested in one of the elements of the list, and you only want to break it when you encounter this element; Have you been human F9 until you meet the conditions? A conditional breakpoint is a breakpoint that satisfies this requirement, as the name implies, under certain conditions. It is also very simple to use, the mouse on your breakpoint will appear a small window, write the conditions can be.
3. Log Breakpoint    Many times when we debug more is the print log to locate the exception code, reduce the scope after the use of breakpoints to solve the problem, so often do is to add log information in the code, output function parameters, return information, output the variable information we are interested in.    But the problem with this is that we have added the log code that needs to be recompiled, and it is very painful to do so in the Dark Ages before the Instant Run, with less than dozens of seconds per compilation and a few minutes, so meaningless waiting is simply torture; in fact, in addition to the hot deployment tool, We can also use a log breakpoint to solve this problem.    First we place the next breakpoint where we want to output the information, and then right-click the breakpoint and set the breakpoint's Suspend property to False in the box that appears, so it's called a breakpoint, but it's not really broken; then we're in the log message Fill in the log information we want to output. such as (note the red position):
In this way, every time the code executes to the breakpoint's location, this lovely breakpoint does not stop our program, but instead outputs the log information we tell it, and then it is very convenient to do so. 4. Method Breakpoint    The traditional way of debugging is in the behavior unit, the so-called single-step debugging; But most of the time we care about the parameters of a function, the return value; (Recall that the most information we print when we use the log is not the parameter and the return value of the function?) Using a method breakpoint, we can debug at the function level, and this type of breakpoint is useful if you frequently jump out of a function or are only interested in the parameters of a function. There are two ways to use it, and the simplest is to put a breakpoint on the line of the method you're interested in, and you'll find that the breakpoint icon is a little different, which is the method breakpoint, like this:
Another way is through the Breakpoint Setup window, which is described later. 5.Exception Breakpoint    In some cases, we are only interested in certain exceptions, or we are only interested in exceptions, and we hope that as long as the program is abnormal the program will be able to break down; this is like saving the scene, as long as the occurrence of a homicide (abnormal), the first time to save the scene, so what fingerprints and other clues will be much clearer, Bad guys are chachinanfei even if they want to escape.    Android Studio has given us this ability! That's the exception breakpoint! You can break the entire program directly when a particular exception occurs, and if you are interested in all the anomalies, throwable directly.    To do this, go to View breakpoints, Run, or use the shortcut key to open the Breakpoint Settings window, such as:
In the upper left-hand corner, click: heavy_plus_sign: A selection box appears, select Exception Breakpoint, and a dialog box appears to select the exception you are interested in:

6. Field Watchpoint    When we add the exception breakpoint above, when we click on the plus sign, there are four options; The first is the second method that we said before, the third one is the exception breakpoint, then the second Field Watchpoint is what?    Is there a scenario where you find that a value is somehow unknown and who is responsible for the change? Although Java is a value pass, but the reference can also be a value, the object is all stored on the heap, and the heap is shared by all threads, so in a very complex scenario, you have no idea who modified the shared variables, it is very dangerous; in a multithreaded environment, invariance is a very important feature, We see the language of high concurrencysuch as Erlang, Scala has some degree of support for this invariance.     Well, pull it off, so how do we find the troublemaker who modified our values? That is the function of the field watchpoint; we can break the program when a field is accessed or modified, and solve the problem perfectly.    There are two ways to place breakpoints in the same way as method breakpoints, and the first is to place a breakpoint directly at the declaration of a field, when the breakpoint icon changes, such as:
Right-click this breakpoint we can make some settings, such as the default is modified by the time of the break down, you can also change every time you access this field to break down.    Another way is to Run the View breakpoint open setting, similar to an exception breakpoint.
Follow-up: Due to my limited ability, can only be summed up here, in fact, the function of debugging has a lot to summarize, will be in the later time to modify and increase the article. I hope this article can let everyone have some gains. ^-^
Here is a blog post on debugging for Android studio for a link: Android Studio you don't know debugging tips [Android Studio authoritative tutorial] breakpoint Debugging and advanced debugging
Android Studio Debugging features use summary "Go"


Overview of the Android Studio debugging tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.