Android Studio Breakpoint Tips

Source: Internet
Author: User

Writing code inevitably has a bug, usually in addition to the most direct debugging method is debug, then your debugging technology stay in which stage? Is it just the next breakpoint stepping into the execution? Or you know Evaluate Expression , know the conditional breakpoint; but have you ever heard of a log breakpoint, Method Breakpoint Exception Breakpoint then? And the tall one Field Watchpoint ?

Have you ever noticed the difference between a breakpoint next to Android studio? For example, what is the difference between three breakpoints? This article will tell you everything about the Android Studio Debug.

Debugging Basics

In general, we have two ways to debug a debuggable apk, one is to put a good breakpoint, and then install the app in debug mode, and the other is attach process a dialog box in Android Studio:

The second method is more commonly used, we can start the APK, directly down the breakpoint, and then attach process to the development processes, the conditional trigger can be directly into the debug mode.

Some of the other steps,, step into , step out force step into etc., are not mentioned; basic tracking means.

Or mention, the simplest way to break the breakpoint, is on the left side of the code Editor, the right mouse click on the line number.

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 object if you want to view one of its properties it is simple to 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 an REPL environment, very practical. Forgot to say, shortcut key Alt + F8 :P

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 only want to break it when you encounter this element; Do you always have human flesh 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.

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, Instant Run and it is very painful to do so in the dark Ages, 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 export the information, and then right-click the breakpoint and set the breakpoint's property in the box that appears, which is suspend False called a "breakpoint", but it does not really break down; then we fill in the log message 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.

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.

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, just go straight to it Throwable .

To do this, enter Run -> View BreakPoints or use the shortcut key to open the Breakpoint Settings window, such as:

Click on the top left: heavy_plus_sign: A selection box will appear, Exception Breakpoint and a dialog box will appear to select the exception you are interested in:

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 high concurrency in languages such Erlang as Scala a 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 -> View BreakPoint open the setting, similar to an exception breakpoint.

Far more than that.

There are a lot of details on the above-mentioned functions. Open the Breakpoint Settings window (Run, View breakpoint '):

We can be interested in the class, the interest of a particular object under a breakpoint, you can set the number of breakpoints, but also to make breakpoints in a particular line friend broken down, these details are not detailed introduction, we have to explore!

Have fun!!

Android Studio Breakpoint Tips

Related Article

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.