Reprint please indicate the source: a column of Maple Leaf
In the previous article we explained the knowledge of Android UI optimization. We have explained the include, Marge, viewstub tags in android, which simplifies our layout files, optimizes the component drawing process, and explains the over-drawing related knowledge points in Android. By optimizing our app over-drawing, you can improve the UI drawing process and performance of your app, and we've also explained some of the little tips for UI optimization in the app. For more information on Android UI optimization, refer to my: Android product development (21) UI optimization in –>android
In this article we will explain the debugging techniques in Android. program debugging, is to put the programmed program into the actual operation, using manual or compiling procedures and other methods to test, correct syntax errors and logic error process. This is an essential step to ensure the correctness of the computer information system. A very important aspect of using debugging techniques in Android development. Android's debugging tips include proficiency in using the log API in Android, customizing the Android log framework, configuring debug logs via Gradle, debugging tips for Android studio, and more. Through the study of this article we can have a general understanding of the debugging techniques in Android.
The Print API in Android:
Let's start by looking at the Print log API available in Android:
android.util.Log.java
This class is more commonly used to print the log method has 5, these 5 methods will print the log to Androidmonitor, Android logs are divided into five levels:
-
LOG.V (Tag,message); Verbose mode, print the most detailed log
-
LOG.D (Tag,message); Debug-level logs
-
LOG.I (Tag,message); Log at info level
-
LOG.W (Tag,message); Warn-level logs
-
LOG.E (Tag,message); Log at error level
Where tag and message are two string values. From the Android Development Help documentation, the definitions for tag and message are:
Tag:used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.
Msg:the message would like logged.
You can see that tag is used to mark the source of the log message. And the message is the contents of this log.
Log Demo Code:
/** * 按钮点击事件,打印各个级别的日志 */button6.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v) { "this is a verbose log!!!"); "this is a debug log!!!"); "this is a info log!!!"); "this is a warning log!!!"); "this is a error log!!!"); } });
OK, let's click on the button and look at the print results in Androidmonitor:
-- - +: A:16.202 7530-7530/Uuch.com.Android_activityanim V/mainactivity: This is a verbose Log!!! -- - +: A:16.202 7530-7530/Uuch.com.Android_activityanim D/mainactivity: This is a Debug Log!!! -- - +: A:16.202 7530-7530/Uuch.com.Android_activityanim I/mainactivity: This is a Info Log!!! -- - +: A:16.202 7530-7530/Uuch.com.Android_activityanim W/mainactivity: This is a Warning Log!!! -- - +: A:16.203 7530-7530/Uuch.com.Android_activityanim E/mainactivity: This is a Error Log!!!
Through this Android own log API to print the way, is the most common kind of print logs, debugging code, basically all of the projects you may encounter this kind of log code, in a previous article I also analyzed the log log part of the source, Specific reference to my: Android source code Analysis (vi) –>log
However, you will find that this time the printed log format is relatively simple, such as when there is an exception when we want to quickly locate the code where, this is more difficult, so let's talk about the custom log framework.
Customizing the Android print frame Mlog:
Earlier we found that using the Android native log API to print the log format is relatively simple, then can be customized to display some of the friendly log information? The answer is yes, in the previous article I introduced a custom log framework Mlog, which can refer to my: GitHub Project parsing (v) –>android log Framework
About the use of the Mlog framework, the implementation process has been done in the article a simple introduction, here to see its printed log format:
Custom button click events:
- Initializing the Mlog framework in application
/** * 传入为true,则表示执行打印操作,否则不显示日志 */MLog.init(true);
Try to do it in the application OnCreate method, which is the process initiation method of the app.
- To perform a print log operation in the source code
/** * 自定义按钮的点击事件 */button6.setOnClickListener(new View.OnClickListener() { @Override publicvoidonClick(View v) { "this is a verbose log!!!"); "this is a debug log!!!"); "this is a info log!!!"); "this is a warning log!!!"); "this is a error log!!!"); } });
Log format and content printed in Androidmoitor:
So we can click on the log information to jump to this log code print location, is not very convenient? And if you think the log format is not very good, you can also customize the display of Austria.
Like what:
What do you think? Isn't it nice?
How to implement the custom log frame and customize the display style of the log can refer to my: GitHub Project parsing (v) –>android log Framework
Most of the time our app has a test environment and the formal environment of the two environments, when the switch environment requires us to print logs in the test environment, the formal environment masking logs, through the code can also be implemented, but more trouble, there is a relatively simple method? The answer is yes, you can configure Gradle to print logs in a test environment, masking log printing operations in a formal environment.
Configure the formal test print frame in Gradle:
We know that in the process of Android development, in order to debug code often add some log information in the code, but the formal environment does not require these log information, and the log printing operation will have an impact on the performance of the app.
A better way is to print the log information in the test environment of the app, to block the log information in the formal environment, then how to do it? Through the code? Through the code is also possible, but it seems too primitive, in fact, Android studio Gradle plug-in has provided such a function.
So how do I print information through the Gradle configuration log?
Buildtypes {debug {//Display logBuildconfigfield"Boolean","Log_debug","true" //ConfusionMinifyenabledfalse //zipalign OptimizationZipalignenabledtrue //Remove useless resource filesShrinkresourcestrue //Load Default obfuscation configuration fileProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro ' //SignatureSigningconfig Signingconfigs.debug} release {//Do not display logBuildconfigfield"Boolean","Log_debug","false" //ConfusionMinifyenabledtrue //zipalign OptimizationZipalignenabledtrue //Remove useless resource filesShrinkresourcestrue //Load Default obfuscation configuration fileProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro ' //SignatureSigningconfig Signingconfigs.relealse}}
In the Gradle configuration file of the module in Android Studio, custom parameters can be configured under the Buildtypes node, where we define log_debug as true in the debug version, defined in release version Log_ Debug is False. This will generate the member variables in Gradle's compiled class buildconfig at compile time:
LOG_DEBUG
-
If the formal environment is log_debug the value is False
-
If the test environment is log_debug, the value is True
Therefore, you can control whether the log is printed by the value of the Log_debug variable.
/** * 在Application的onCreate方法中初始化MLog日志框架 * 并根据apk环境判断是否显示日志信息 */iftrue) { MLog.init(trueelse { MLog.init(false);}
We implement the Init method of the Mlog framework
-
If the value passed in is true, the log print operation is performed, which means that the log information can be displayed.
-
If the value passed in is false, the log print operation is not performed, that is, the log information is not displayed.
This enables us to print logs in the test environment and to mask the logs in a formal environment.
Debugging Tips for Android Studio:
The process of writing code is inevitably a bug, usually in addition to the most direct debugging method is debug; So what is your debugging technique? Is it just the next breakpoint stepping into the execution? Do you know debugging techniques such as evaluation debugging, conditional breakpoints, log breakpoints, method breakpoints, and exception breakpoints? Here's a look at the debugging tips in Android studio.
Debugging Basics
In general we have two ways to debug a debuggable apk;
-
Set the breakpoint, and then use debug mode to compile and install the app;
-
Executes the attach debugger to Android process, adding the debug process to the current process.
The commonly used debug mode is attach debugger to Android process:
The two button on the right side of the button with the bug is actually the attach Debugger to Android process button.
After starting the APK, we can go directly to the breakpoint and then attach process to the specified processes, which can be directly entered into debug mode after the condition is triggered.
Breakpoint Debugging
Breakpoint debugging is the most basic operation, Keane's breakpoint debugging has the following several common debugging commands:
How do I add breakpoints?
Directly to the left of Java source code in Android studio, if you want to debug a line of code, click on the left side if there is a small red circle, then the breakpoint has been added:
When the code is in debug mode and executes here, it will be stuck here.
Perform breakpoint debugging
Executes the attach debugger to Android process, and when the code executes to the code that contains the breakpoint, it can snap the program to the breakpoint:
At this point we can see that the corresponding local variable value can be seen in the line code, the execution F6 goes to the next step, executes the F8, and debugs to the next breakpoint.
Adding observation variables
In debug mode, select the variable and right-click:
When you choose Add to watches, you can add variables to the watch list.
Expressions Debugging: Evaluate expression
This debugging function is very practical, but also my favorite feature, using the CTRL + U shortcut can be used to pop-up window expression debug pop-up window:
This is a very useful feature that allows you to enter an evaluation environment directly at a breakpoint, where you can perform any expression you are interested in to write any Java expression in the expression Debug pop-up window, such as:
For example, there is an object at the breakpoint, and if you want to see one of its properties it's easy 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 a REPL environment, very practical.
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 right mouse button on your breakpoint will appear a small window, write the conditions.
Other ways to debug breakpoints
In addition to the evaluation breakpoint here, conditional breakpoints, there are log breakpoints, abnormal breakpoints, method breakpoints, and so on, the specific can be self-Google Kazakhstan.
Summarize:
-
Android default provides the log API to achieve the printing function;
-
You can implement a custom log framework, customize the display log style, customize the configuration to display the log, etc., for reference: Android log frame
-
You can configure Gradle to have the log information displayed in the test environment and not display the log information in the formal environment;
-
Android Studio provides breakpoint debugging features, including evaluation debugging, conditional breakpoints, log breakpoints, method breakpoints, abnormal breakpoints, and so on;
In addition to product development technology, skills, practice interested students can refer to my:
Android Product Development (12) –>app Long Connection implementation
Android Product Development (13) –>app rotation operation
Android Product Development (14) –>app Upgrade and update
Android Product Development (15) –> Memory object serialization
Android Product Development (16) –> Developer Options
Android Product Development (17) –>hybrid Development
Android Product Development (18) –>webview problem Collection
Android Product Development (19) unit tests in –>android Studio
Android Product Development (20) –> code review
Android Product Development (21) UI optimization in –>android
This article synchronizes to GitHub: Https://github.com/yipianfengye/androidProject, Welcome to star and follow
Android product Development (22)-->android Practical Debugging skills