In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Lint to optimize code in Android development. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Preface
In order to ensure that there are no functional problems in the code and complete the business development, the pursuing programmers should also pursue the standardization and maintainability of the code.
Today, the goal of "becoming a good programmer" will work with you to improve and learn to use Lint to optimize our code.
What is Lint?
Lint is a code scan analysis tool provided by Android Studio, it can help us find code structure / quality problems, while providing some solutions, and this process does not require our handwritten test cases.
Each problem found by Lint has a description and level (similar to the bug found by the test), and we can easily locate the problem and solve it according to its severity.
Of course, we can manually adjust this "severity". Some issues of principle are inviolable and must be upgraded to error, while some individual problems can also be ignored. after all, it is impossible for a man to be a sage.
A brief introduction to the working Mode of Lint
Lint will check the source files of our Android project according to the pre-configured testing standards to find potential bug or areas that can be optimized. The optimization mainly includes the following aspects:
Correctness: insufficient coding, such as hard coding, using outdated API, etc.
Performance: coding that affects performance, such as static references, circular references, etc.
Internationalization: internationalization, direct use of Chinese characters, no use of resource references, etc.
Security: unsafe coding, such as allowing the use of JavaScriptInterface in WebView
The process of Lint detecting the code is shown in the following figure:
App source files: including Java code, XML code, icons, ProGuard configuration files, etc.
Lint.xml: the execution standard configuration file for Lint detection, which we can modify to allow or disable reporting of some problems
Run Lint from the command line
Lint's command is simple:
Lint [flags]
Lint can also be run using Gradle:
Windows:
Gradlew lint
Mac:
. / gradlew lint
The specific operation related to the command line will not be described here, because this process is too painful. You can experience the screenshot of the XML file obtained after running Lint on the last command line:
Let's go directly to the GUI operation of Lint.
Using Lint in Android Studio
Lint is built into Android Studio, so we can use it directly with a little hand.
The path to use Lint:
Toolbar-> Analyze-> Inspect Code...
When you click Inspect Code, the dialog box for the scope of inspection will pop up:
The default is to check the entire project, and we can click Custom scope to customize the check scope.
Click the drop-down box on the right and the following choices appear:
They are:
Project Files: all project files
Project Production Files: the code file for the project
Project Test Files: test file for the project
OpenFiles: currently open file
Module 'app': main app module
Current File: current fil
In addition to the built-in options, we can also select a specific class to check, click on the red box in the following figure:
The custom range selection box will pop up. The default is empty. We can click the "+" sign in the upper left corner to add a new check range:
-Local: can only be used by the current project
-Shared: other Android Studio projects can also be used
We choose Shared, and then give it the handsome name "ShixinCuteLint", which is displayed by project by default. The number of files checked at this time is 0:
The four buttons on the right in the above figure indicate the type of operation to be performed:
Include: includes files in the current folder, but does not include its subfolders
Include Recursively: including all folders within the current folder and its subfolders, recursively add
Exclude: removes the current folder, excluding subfolders
Exclude Recursively: removes the current folder and all subfolders
After clicking on the app folder on the left, click the Include Recursively button on the right to add all the files under app to the checklist:
As you can see, all the files under app turn green, and there are a total of 689 folders to scan.
Click OK to test, wait a moment, the Inspection dialog box will pop up, showing the results of the check, unexpectedly my code has 1769 warnings! The figure is shocking:
Let's focus on the warnings in the red box. Let's take a look at what's wrong with my code Performance:
Haha, I didn't expect that I still have so much room for improvement!
As you can see in the image above, Lint is a real artifact that can help us find problems that we ignore or are not aware of, especially in terms of performance. if you think your code wants to optimize but don't know where to start, let Lint show you the way.
Build a code specification weapon in the team: raise and lower the level of the problem
Although Lint can help us check for problems with our code, when many people work together, we are more likely to find and solve problems while writing code.
In view of the mixed levels of team members, it is sometimes difficult to guarantee quality by personal awareness, so you can modify the Lint warning level for specific problems to alert members with the most intuitive IDE tips.
The severity of Lint's warnings are as follows:
Unused Entry: unused attributes, gray, obscure
Typo: spelling mistakes, green wavy underlines, not very eye-catching
Server Problem: server error? I don't think so.
Info: comment document, green, more conspicuous
Weak Warning: weak warnings, weak prompts
Warning: warning, slightly more conspicuous
Error: error, the most obvious one
In daily development, better programmers will follow Warning's warnings and optimize their code according to them, but that's only a small part of it. But the red Error is different, basically see it and want to destroy it.
Let's take the misspelling of names as an example.
Classes, objects, and traversing spelling errors may not seem like a problem, but if you've seen a lot of meaningless or misnaming, you'll agree with what I'm going to do next.
The default spelling error is Typo, and the prompt is weak, so it is often ignored:
The above String type variable login is written as logn,Lint default is a sliding wavy line for spelling errors, which is very inconspicuous. Let's fix it.
Open Preferences/Settings and search for Inspections, and the detection configuration page of Lint will appear:
To change the spelling warning level, search for "spelling":
Then select the Typo that appears, and then click the Severity on the right to change the severity to Error,OK.
As you can see, now there will be a red error warning for spelling mistakes, so that you don't write variable names properly!
Although Lint is good, you can't drink too much.
Lint is like a cleanliness addict, although it can make our code much cleaner, but if you really want to solve all its hints, I'm afraid the boss will be angry: pay you to play the computer every day, why not work?
Some of the warnings in the Lint are really unnecessary, and we can choose to ignore them. There are two types of warnings to ignore:
In the Java code
In the XML folder
Ignore the Lint warning in the Java code:
The comment that ignores Lint warnings is similar to @ SuppressWarnings, @ SuppressLint ("ignored warning name").
The following code demonstrates how to ignore Lint's warning to use the new API:
SuppressLint ("NewApi") @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main);}
If you don't know the exact name of the warning to ignore, just ignore all, which is, of course, the current class / method / object:
@ SuppressLint ("all")
Ignore the Lint warning in the XML code:
It only takes two steps:
Declare the tools namespace in xml
Use tools:ignore= ignored warning name
For example:
Configure Lint in Gradle
Key actions of Lint can also be configured in Gradle, such as whether to turn on Lint warnings or to turn off specified warnings.
Add lintOptions {… to the build.gradle under module. }, the sample code is as follows:
Android {... LintOptions {/ / Turns off checks for the issue IDs you specify. Disable 'TypographyFractions','TypographyQuotes' / / Turns on checks for the issue IDs you specify. These checks are in / / addition to the default lint checks. Enable 'RtlHardcoded','RtlCompat',' RtlEnabled' / / To enable checks for only a subset of issue IDs and ignore all others, / / list the issue IDs with the 'check' property instead. This property overrides / / any issue IDs you enable or disable using the properties above. Check 'NewApi',' InlinedApi' / / If set to true, turns off analysis progress reporting by lint. Quiet true / / if set to true (default), stops the build if errors are found. AbortOnError false / / if true, only report errors. IgnoreWarnings true}}...
Automatically delete the found useless resource files
With more than one iterative version of the code, it is easy to leave some useless code and resource files behind, which we can use Lint to clear.
Click Android Studio toolbar-> Analyze-> Run Inspection By Name.., to enter the content to be detected. Here are the useless resources:
Then select Unused resources, and then select the range to start the detection.
So many useless files have been detected:
Note that there is a solution on the right: Remove All Unused Resources, make the big picture stand out:
On Android development how to use Lint optimization code to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.