What you don't know about Android Studio debugging tips

Source: Internet
Author: User

Reprint: http://www.jianshu.com/p/011eb88f4e0d

Android Studio has become the main tool to develop Android, and it is quite handy to be familiar with it. As a developer, debug and find bugs to solve, but our housekeeping skills. Just so-called, 工欲善其事 its prerequisite, like the other development tools, such as Eclipse, Idea,android Studio also provides us with powerful debugging techniques, and today we'll look at the debugging techniques in Android Studio.

First, take a look at the debug panel provided for us in Android Studio (in the standard case):


Write a picture description here

Click Restore ' Threads ' view in the top right corner to show the currently relevant thread information:


Write a picture description here

Android Studio generally provides us with 7 functional areas:

    1. Single Step Debug Area
    2. Breakpoint Management Area
    3. Evaluation expressions
    4. Thread Frame Stack Area
    5. Object variable Area
    6. Variable Observation Area

Here we introduce each of the seven regions.

Single Step Debug Area

The area provides the main debugging operations, as you know, with the following: Step over, step in, Force Step in, step out, drop frame.

Show Execution Point
Write a picture description here

Click this button and the cursor will navigate to the location you are currently debugging.

Step over
Write a picture description here

Stepping over, clicking this button will cause the program to execute down one line. If the current row is a method call, the method called by this line is executed and then to the next line. For example, the current code is:

int num=10;int min=Math.min(num,100);System.out.println(min);

If the second row is currently being debugged, when you click Step Over, the Math.min (num,100) method executes and jumps to the third line.

Step into
Write a picture description here

Stepping in, doing so causes the program to execute down one line. If the row has a custom method, proceed inside the method and be aware that if it is a method in the class library, it will not go inside the method.

Force Step into
Write a picture description here

Forced stepping, similar to the step into function, is the main difference: if the current line has any method, regardless of whether the method is defined by us or provided by the class library, you can jump into the inside of the method to continue execution

Drop Frame
Write a picture description here

There is no well-remembered name, which is understood as an interrupt execution and returns to the initial point of execution of the method, in which the corresponding stack frame of the method is removed from the stack. In other words, if the method is called, it is returned to the current method being invoked, and the value of all context variables reverts to the state when the method was not executed A simple example:

PublicClassdebugdemo {private String name =  " Default "; public void alertname ( OUT.PRINTLN (name); Debug (); } public void debug (this.name = " "Debug"; } public static void main (string[] args) { new Debugdemo (). Alertname ();}           

When you debug Debug (), the operation is performed, and the callback is called to Debug (), which is the Alertname () method. If you continue execution of the drop frame at this point, the callback will be to the place where Alertname () is called, that is, main ().

Force Run to Cursor
Write a picture description here

A very useful feature that ignores existing breakpoints and jumps to where the cursor is located. For a simple example:


Write a picture description here


For example, now the 10th line, at this time I would like to debug 18 lines and do not want to step by step debugging, can one be in place? We just need to position the cursor to the appropriate location and then execute the force Run to cursor:


Write a picture here describing evaluate expression
Write a picture description here

Clicking this button embeds an interactive interpreter at the currently debugged statement, where you can perform any evaluation of the expression you want to perform. For example, when we debug, we execute the following code:


Write a picture description here

The execution of evaluate Expression at this time is equivalent to embedding an interactive interpreter before the trial, so what can we do in the interpreter? Here, we can evaluate the result: you want to ask for the right mouse button, select Evaluate Expression. This will appear as follows:


Write a picture description here

Enter the evaluation expression in the pop-up input box, for example, here we enter Math.min(result,50) , as


Write a picture description here

Click Execute, we find that the result has been output in result, as follows:


Write a picture description here Breakpoint Zone return
Write a picture description here

Clicking this button stops the current app and restarts. In other words, you can use this action when you want to re-debug, well, it means to start over.

Pause Program
Write a picture description here

Clicking this button pauses the execution of the app. If you want to recover, you can use the Resume program mentioned below.

Resume Program
Write a picture description here

This operation has the meaning of the recovery application, but there are two behaviors:

    1. When the app is paused, tapping the button will restore the app to run.
    2. In many cases, we will set multiple breakpoints for debugging. In some cases, we need to move from the current breakpoint to the next breakpoint, the code between the two breakpoints is automatically executed, so that we do not need to step-by-step debugging to the next breakpoint, save time and effort. To illustrate:
public void test(){ test1(); ... test2();}

Let's say we add breakpoints in lines 2nd and 4th, respectively. If we debug at the 2nd line at this point, click to do this, the current debug location will be automatically executed to line 4th, that is, the 2nd to 4th line between the code will be automatically executed.

Stop
Write a picture description here

Clicking this button terminates the current process with the associated closing script. In other words, there may be different stopping behavior for different types of projects, such as: For normal Java projects, clicking on this button means exiting debug mode, but the app will also perform. And in Android, click the button, This means that the app is finished running.

Here we take an example of a common Java project:


Write a picture description here

At this point, if we do stop operation, the Discovery program exits debug mode, and the normal execution is complete, console results are as follows:


Write a picture here to describe the view breakpoints
Write a picture description here

Click this button will go to the breakpoint management interface, where you can view all breakpoints, manage or configure the behavior of breakpoints, such as: Delete, modify property information, etc.:


Write a picture here describing mute breakpoints
Write a picture description here

Use this button to toggle the state of a breakpoint: Start or disable. During debugging, you can disable all breakpoints temporarily, and the application runs correctly. This feature is useful, such as when you suddenly do not want breakpoints to interfere with the process you care about during debugging, you can temporarily disable breakpoints.

Get Thread Dump
Write a picture description here

To get the thread dump, click the button to enter the thread dump interface:


Write a picture description here

Let's take a look at the dump interface:
The most common thread in the Threading tool area is


Write a picture description here

That can be used to filter threads, others do not explain

Parsing let's get to know the types of threads, which are represented as different icons:

Thread Status Description icons
Thread is suspended.
Write a picture description here
Thread is waiting on a monitor lock.
Write a picture description here
Thread is running.
Write a picture description here
Thread is executing network operation, and are waiting for data to be passed.
Write a picture description here
Thread is idle.
Write a picture description here
Event Dispatch Thread is busy.
Write a picture description here
Thread is executing disk operation.
Write a picture description here
Settings
Write a picture description here

Clicking this button opens a list of settings:


Write a picture description here

We describe several of them:

Show Values Inline

The function is turned on during debugging, and the value of the variable is displayed to the right of the code, which is the part shown in the Red box:


Write a picture here describing show Method Return Values

When this feature is enabled during debugging, the return value of the last execution method is displayed in the variable area. For example, first, to turn off this function, we debug the code and look at its variable area:


Write a picture description here


After you turn on this feature, observe the change in the variable area:


Write a picture description here

Continue debugging:


Write a picture description here

Continue debugging:


Write a picture description here

This is a great feature, and it's very useful to debug a piece of code and want to see the final result of calling the method in that code.

Auto-variables Mode

When this function is turned on, the debugger of idea automatically evaluates certain variables, presumably when you execute at a breakpoint, debugger detects the state of the variable before or after the current tuning, and then selectively outputs it in the variable area. For example, before the feature is turned on, The variable area outputs all the variable information:


Write a picture description here

After opening, when you debug to the 13th line, debugger detects that the NUM variable is not being used, then the variable's information is not output in the variable area.


Write a picture here to describe sort values alphabetically

With this feature turned on, the output in the variable area is sorted alphabetically, very simple, infrequently used, or in the default order.

Help
Write a picture description here

Needless to say, anything that doesn't understand can be viewed in the official Help documentation, which is one of the best documents I've ever seen.
Several other operations: Settings,pin,close left to you to use.

modifying variable values

During the debugging process, we can easily modify the value of a variable, as follows:


Write a picture description here


In, the value of the current result is calculated as 10, where we modify its result to 100 by set value.

Variable Observation Area

The area will display the value of the variable you are interested in. In debug mode, you can add a variable to observation area by adding to watches, and the change will be displayed in the variable observation area. The operation is as follows:


Write a picture description here


Here we are interested in name and want to see the change in its value, so we will take its "special care". Note that because name is a member variable, you can also see the value in the object's observer area. If it is a local variable, it can only be used in this way.

Classification of breakpoints

So far, we have simply introduced the debug Ribbon, Breakpoint Management area, evaluation expression, the functions of these three regions. On top of that, we constantly mention the break point once, but what is the breakpoint? Presumably most people already know that we are here under a simple note:

Breakpoints are one of the functions of the debugger, allowing the program to pause where it is needed and help us run the analysis program.

In Android studio, breakpoints are also listed in the following five categories:

    1. Conditional breakpoint
    2. Log Breakpoint
    3. Exception Breakpoint
    4. Method Breakpoint
    5. Property Breakpoint

Where the method breakpoint is the type of breakpoint we are most familiar with, I believe no one will. We highlight the other four types of breakpoints below.

Conditional breakpoint

The so-called conditional breakpoint is the breakpoint that occurs on a particular condition, that is, we can set a breakpoint to be interested only in an event, the most typical application being in a list loop, we want to pause the program when a particular element appears. For example, now that we have a list that contains four elements of q,1q,2q,3q, and we want to pause the program when we traverse to 2q, we need to do the following:
Add breakpoints where needed, as follows:


Write a picture description here

Left-click at the breakpoint and fill in the filter conditions at condition. Here we only care about 2q, so fill ins.equals("2q")


Write a picture here to describe the log breakpoint

This type of breakpoint does not cause the program to stop, but it outputs the log information that we want it to output, and then resumes execution. Here's how:
Also left-click at the breakpoint and uncheck suspend in the popup dialog box.


Write a picture description here

In the pop-up Control Panel, select Log evaluated expression, and then fill in the log information you want to output, as follows:


Write a picture description here

When the debugging process encounters the breakpoint, the result is output as follows:


Write a picture here to describe an abnormal breakpoint

The so-called exception breakpoint is in the debugging process, in the event of an exception (you can specify a class of exceptions), will immediately locate the exception throws the place. For example, in the debug exception, we are very concerned about the runtime exception, in the hope of generating any running exception in time to locate, then you can take advantage of this type of exception, before the launch, the exception breakpoint debugging is very helpful to reduce the chance of crash in the formal environment.
Here's how to do this: In the Run menu item, select View breakpoints (or in the Breakpoints Management panel, click


Write a picture description here

), as follows:


Write a picture description here

In the Manage Breakpoints panel, click +


Write a picture description here

In the drop-down selection list that pops up, we select Java Exception breakpoints


Write a picture description here

Here we select Search by Name and enter the type of exception we care about in the input box below. Here we care about NullPointerException, and the debugger locates the exception where the nullpointerexception occurs once the debugging process has occurred.


Here to write a picture description method breakpoint
Write a picture description here

(Skip it, no one should know)

Filed Watchpoint
Write a picture description here

Filed Watchpoint is essentially a special breakpoint, also known as a property breakpoint: When a field value is modified, the program pauses at the point of modification. This is especially useful when debugging multiple threads, which can help us locate concurrency errors in a timely manner. It has no difference in using and adding a normal breakpoint, and the breakpoint icon is slightly different

Two ways to debug

At present, the relevant basis for debugging we have been introduced, but a lot of children's shoes on the android studio


Write a picture description here

The two buttons were confused: Debug and attach process.
Here's a brief look at the difference between the two:

    • Debug: Install in debug mode, breakpoints can be set before running, or can be set after running, is the most common mode of most people
    • Attach Process: The debugger can be Attach to any running process compared to the debug mode. For example, we can pass the attach process to the processes that we want to debug. Then, set the relevant breakpoint where needed.

In the specific commissioning process, you can choose your own discretion. After that, I will take you step after step to debug the Android framework-related source code, see:
DIY to compile the latest Android source code and SDK and DIY debugging Android source code

What you don't know about Android Studio debugging tips

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.