Youth must be early, how long young.

Why check improved code through Lint?

In addition to building tests to ensure that your application meets its functional requirements, you must also run your code through Lint to ensure that there are no structural problems with your code. Poorly structured code can affect the reliability and efficiency of an Android application and make your code harder to maintain, and the Lint tool can help find it. For example, if an XML resource file contains unused namespaces, it not only takes up space, but also leads to unnecessary processing. Other structural problems, such as using deprecated elements or API calls that are not supported by the target API version, can cause the code to not work properly. Lint can help you solve these problems.

How does the Lint tool handle application source files?

  • Application source files: The source files contain the files that make up the Android project, including Java, Kotlin, and XML files, ICONS, and ProGuard configuration files.
  • Lint.xml file: A configuration file that can be used to specify any Lint checks to exclude and custom problem severity levels.
  • Lint tool: A static code scanning tool that you can run on Android projects from the command line or in Android Studio
  • Lint check Results: You can view lint check Results on the console or in the Inspection Results window of Android Studio.

Field experience

(1) Practical operation environment

  • Optional, use your own environment and code as well
  • The SamplePop environment is as follows:

Android Studio 4.0 Gradle Version 6.1.1 Android API Version 30

(2) Command line operation

// Windows gradlew lint //Linux or Mac./gradlew lint // If you only want to run lint tasks for a specific build variant, Gradlew lintDebug // will have the following output: > Task :lint:lint Ran lint on variant debug: 9 issues found Ran lint on variant release: 9 issues found Wrote HTML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.html Wrote XML report to file:///F:/Git/Blog/SamplePop/lint/build/reports/lint-results.xmlCopy the code

Go to the folder, go to lint-results.html, and double-click the file

Common problems can be classified into the following categories:

(1) Accessibility options such as ImageView contentDescription are usually recommended to be defined in properties. (2) Compliance, which violates Google Play’s requirements, such as using expired library versions, performance, security, API levels, etc., and failing to comply with the requirements of the new system. (3) coding that is not perfect, such as hard coding, using outdated API, etc. (4) Internationalization, directly using Chinese characters without using resource references, etc. (5) Interoperability, like Interoperability with Kotln. Performanc, Performanc, Performanc, Performanc, Performanc, Performanc (7) Insecure encoding, such as JavaScriptInterface allowed in WebView. There are better Usability alternatives such as typography, icon format suggestion, PNG format, etc.

(3) Independent tool operation

Not practical, just ignore it

// Run lint [flags] <project directory> on the list of files in the project directory. Lint --check MissingPrefix myproject // Check help lint --helpCopy the code

(4) Lint configuration

You can configure different levels of Lint checking:

  • Overall (entire project)
  • The project module
  • Production module
  • Test module
  • Open file
  • Class hierarchy
  • Version control System (VCS) scope

(4.1) Configuring Lint in Android Studio (built-in Lint)

  • Pop-up text view in the AS code editor. When lint detects a problem, it highlights the offending code in yellow, or for more serious problems, it adds a red underline under the code.
  • Click Analyze > Inspect Code in order to view in the Lint Inspection Results window.

(4.2) Configure the lint.xml file

//lint. XML is recommended to be placed in the root directory of your Android project with build.gradle configuration as follows: Android {lintOptions {lintConfig file("lint.xml")}} //lint.xml example <? The XML version = "1.0" encoding = "utf-8"? > <lint> <! -- Disable the given check in this project --> <issue id="IconMissingDensityFolder" severity="ignore" /> <! -- Ignore the UselessLeaf issue in the specified file --> <issue id="UselessLeaf"> <ignore path="res/layout/main.xml" />  </issue> <! -- Change the severity of hardcoded strings to "error" --> <issue id="HardcodedText" severity="error" /> </lint>Copy the code

4.3 Configure Lint checking for Java, Kotlin, and XML source files

//1 Disable Lint checking for Java or Kotlin Please add @SuppressLint to this code @SuppressLint("NewApi") Override fun onCreate(savedInstanceState: Bundle?) {super.oncreate (savedInstanceState) setContentView(r.layout.main) //1.2 Disable Lint checking for ParserError problems in FeedProvider classes @SuppressLint("ParserError") class FeedProvider : ContentProvider() {//1.3 Disallows Lint from checking for all problems in files @SuppressLint("all") //2 Configure LINT checking for XML //2.1 Tools: Ignore attribute for XML Disable lint to check the <LinearLayout> element in the // layout file for UnusedResources issues close Lint to check the <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" Tools: Ignore ="UnusedResources" > <TextView Android :text="@string/auto_update_prompt" /> </LinearLayout> Please use comma-separated string list to ban inspection issue tools: ignore = "NewApi, StringFormatInvalid" / / 2.3 to ban lint to check all the problems in the XML elements, Use all keyword tools:ignore="all"Copy the code

Configure lint options using Gradle

// Build. gradle is configured 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 } }Copy the code

(5) Create warning benchmarks

You can create a snapshot of your project’s current warning set and then use that snapshot as a baseline for running future checks to report only new problems. With baseline snapshots, you can start using Lint to fail builds without having to go back and fix all existing problems first. Personal impressions this benchmark is especially useful when collaborating on multi-person projects.

Android {lintOptions {baseline file("lint-baseline.xml")}} // Customize the baseline: android {lintOptions {baseline file("lint-baseline. If you want to add some problem types (but not all) to the benchmark Android {lintOptions {check 'NewApi', 'HandlerLeak' baseline file("lint-baseline.Copy the code

When this line of code is first added, the lint-baseline. XML file is created to establish the baseline. After that, the Lint tool only reads the file to determine the baseline. To create a new benchmark, manually delete the file and run Lint again to recreate it.

When you implement a benchmark, you receive an informational warning telling you that one or more issues have been filtered out because they are listed in the benchmark. This warning is issued to help you remember that you have configured the baseline, because ideally, you want everything resolved at some point.

(6) Manually run the check

You can manually run configured Lint and other IDE checks by selecting Analyze > Inspect Code in order. The Inspection Results are displayed in the Inspection Results window. For more details, check out the Google website at the end of this article.

Start working on your own project code and you will be surprised

Xiaobian extension links

  • SamplePop code download
  • Android Performance Optimization family Bucket

Refer to the link

  • This is the first article that should be read
  • Google official website -> LintOptions configuration items reference
  • Excellent article -> The most thorough article with detailed field analysis

Beautiful luxury, light flatly do not boast

❤ ❤ than heart