Androidstudio application debugging tips (UP)

Source: Internet
Author: User

Objective

In the process of Android development, the development of the program needs to be debugged. Google official and unofficial, provides a lot of tools and methods to help us debug our code. Some are simple to use, some are powerful, and few have access to them. Therefore, we are going to introduce the students to the class, and know that the appropriate tools are used for different scenarios.

The reader for this article is:

    1. For Android program debugging needs to guide the novice;
    2. Developers who do not have much experience in program debugging;

Before you start, let's say you're ready to do the following:

    • is already building the Android software development platform
    • An Android device (mobile or tablet);
    • A data cable (usually a micro USB cable) connecting the computer and the Android device;
    • Half day time.
    • Patience and curiosity.

This article will cover:

    1. breakpoint debugging;
    2. Output log debugging;
    3. Android Device Monitor Preliminary use
    4. ADB tools
    5. WiFi connection device commissioning;
1th section dynamic Debugging and static debugging

The debug program on the Android system, mainly in two forms:
1. Static debugging: When the program runs to a certain state, let it pause, with the tool to view the running information of the program at this time, such as the value of a variable;
2. Dynamic debugging: In the program, add log information (logs), when the program is running, the log specified information output to the debugging computer. The entire process does not interrupt the program's operation.

Either way, you need to connect your phone to the computer you are debugging and pass the debugging information through the data cable.

1.1 Connection of the device to the computer

To debug on the device, first open the device's developer options, different brands of Android device interface, although not the same, but the use of a similar way:

    1. Launch the " settings " app on your Android device and go to the " about Mobile " tab;
    2. Click the " version number " continuously until 您现在处于开发者模式! the prompt message appears;


    3. Return to the parent menu, go to "developer Options", 开启 debug mode, hook up USB调试 ;

Connect your phone and computer with a USB cable.

On Windows systems, you need to install ADB drivers for the devices on the connection:

    1. Download ADB's Windows driver to PC locally by downloading the resources on the Bean network;
    2. Right click on " My Computer ", select " Manage ", open " Device Manager ", you can see the device without installing the driver;
    3. To update the driver for it, select Browse computer Find ,


    4. To specify the location of the downloaded ADB driver directory, click OK and the driver will install successfully.

    5. Click on the windows of Android studio to Android Monitor see the device on this connection, and this window also outputs the running information of the mobile-side printing.

1.2 Deployment of applications

There are two ways to run an application through Android studio to the device: run app and debug app .

debug appYou can set breakpoints for static debugging of code, and you run app cannot set breakpoints without static debugging of your code. Either way, you can start with a menu item, or you can start with a shortcut key.

    1. Click on the small green triangle in the menu bar, that is run app ;

      Or use debug app the shortcut keys shift+F10 ;

    2. Double-click on the selected device,

      At this point you can see it on the device and our program is running.

1.3 Static debugging methods

Static debugging is the freezing of the application running state, as if the time to stop the general, and then we observe each time the program's parameters are consistent with our expectations. This debugging method applies to programs that are insensitive to time. This means that the program thread being debugged does not need to rely on other threads, even if it is temporarily stopped, it will not affect other worker threads or be affected by other worker threads.

  1. Break points where you want your code to pause--click in front of the code, a red dot appears, and if you want to cancel, click once.

  2. debug runDeploy the program in the same way. When the program runs to this location of the code, the program stops and switches to the Debug window. At this point, we can observe each parameter. For example, the right-hand area lists the values of each variable when it is stopped, and the left area shows the function to the call stack (which is called by the function). We can analyze them in detail to see if these values are in line with our expectations.

  3. The following features will specify the program to pause after execution of the next move. These are often used in breakpoint debugging, by our control procedures to run the function, so as far as possible to remember their corresponding shortcuts;

    Step Over: Executes the code that completes the current breakpoint, and then stops at the next line of code to be executed.

    void fun1(){    int0;    //正等待执行的代码    a++;    //下一步待执行的代码}void fun2(){    int0;    b++;}

    Step Into: If the current breakpoint execution is a function, then Step Into after execution, go to the function.

    void fun1(){    int0;    //正等待执行的代码    a++;}void fun2(){    int0;  //下一步待执行的代码    b++;}

    Step Out: After executing at the current breakpoint execution Step Out , return to the place where the function is called to wait for execution.

    void fun1(){    int0;    //下一步待执行的代码    a++;}void fun2(){    int0;  //正等待执行的代码    b++;}

    Resume Program: The program continues down until the next breakpoint is encountered.

There are debugging features force , for example force step over , that they can be used in this scenario: when you want to enter a source code that is not written by you to view the calling process, but using force features that do not, it does not work. In short, it is used to keep track of the code in the 不带force 带force SDK.

1.4 Dynamic Debugging methods

For those with time-related programs (you can't pause the program, and so you look at it), we can't use the static debugging method, you have to use dynamic debugging, add log way.

The Chinese name of log is called the journal, and in the programming world, the information printed during the program's operation is represented. According to log, we know where the program is now, and log can carry the information output of some variables in the program, so that we know the state of the program's current operation more precisely.

Add Log in 1.4.1 code

Adding a function to your code allows you to output these logs through special tools.
The way to add logs to your Android code is as follows:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Log.d("TAG""debug info: function=""onCreate()");}

This uses the log library provided by Android.

View of 1.4.2 Log

Once you have added the log information, you debug app can Android Monitor see the corresponding information in the tool's window by deploying the program to the device logcat .

The debug information for the output is as follows:
03-22 03:22:39.778 8357-8357/com.anddle.calculator D/TAG: debug info: function=onCreate()

03-22 03:22:39.778: The time the log was generated;
8357-8357: Process ID (PID) and thread ID (TID) for running this code on the device;
com.anddle.calculator: The package name for running this code on the device;
D: The type of this log. We output this log using the Log.d() , so it is displayed D , if the use of the Log.i() display I , and so on;
TAG: Log.d() The first parameter of a function;
debug info: function=onCreate(): Log.d() The second parameter of a function;

The log library for Android app development provides several different levels of log: Verbose Debug Info Warining Error We can add different levels of log according to our own log, using the form:

Log.v(“TAG”,”content is verbose”);Log.d(“TAG”,”content is debug”);Log.i(“TAG”,”content is info”);Log.w(“TAG”,”content is waring”);Log.e(“TAG”,”content is error”);

We debug the program in the app, usually using D.

Add rule for 1.4.3 Log

In the development of the application, we have some unwritten techniques for the addition of log, which can improve the development efficiency of the program.

  1. Defines the same TAG for the application when it is debugging the same functionality. For example, an application has two large functions for network communication and local file reading. Then we can define a tag called network for network traffic, and a tag called FileAccess for another local file read. That way, when we look at the corresponding log information, it's easy to separate them from the ocean of log by keyword. The TAG is defined as a string constant to facilitate modification of the log output information.

    static  final  String TAG1 =  "Network" ; static  final  String TAG2 =  "FileAccess" ; ... LOG.D (Tag1, "This is the network communication related log! "); LOG.D (Tag2, "This is the file read log! ");
  2. Use to Debug开关 control whether the log information is output. When debugging an application, add a lot of logs, and when the app is published, remove the log information. Adding or deleting these logs will add a lot of work. So we need to use Debug开关 .

    staticfinalbooleantrue//true表示打开开关,false表示关闭开关;......if(DEBUG){    Log.d(TAG,"Debug开关打开才输出!");}
  3. The call stack for the output function. Sometimes, we are not only concerned about what the values of the variables are at the time of the program execution, but also about how the function is called. Then we can add the information of the output call stack in the code:

    Log.d(TAG, Log.getStackTraceString(new Throwable()));
  4. Log.d()Use when debugging code, use it in the event of Debug开关 Log.e() an unexpected and important error condition, and use without control when it is Debug开关 necessary to output a Log.v() Log.i() Log.w() running state and not involve exposing the application to implement information Debug开关 ; Developers want to use these log types to how they can be used, and there is no special constraints.

Androidstudio application debugging tips (UP)

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.