2.1, Android Studio improves your code through Lint

In order to test your Android application meets the functional requirements. The most important thing is to ensure that your code has no structural issues. Poorly structured code affects the reliability of your Android application and makes your code difficult to maintain. For example, if your XML resource file contains unused clear space, this will cost space and unnecessary processes. Other organization issues, such as an outdated API call, if the device API version is not compatible, it may cause the operation to fail.

Overview

Android Studio provides a code scanning tool called Lint, which can easily help you identify and correct structural quality problems in your code without you having to execute the app or write test cases. Each error detected by the tool will be marked with a description of the severity, so you can quickly determine the priority. You can also ignore errors that are not important in your project. This tool has a corresponding command line interface, so you can easily integrate it into your testing process.
The Lint tool detects potential bugs and provides code optimization suggestions by inspecting your Android project source code. You can run Lint from the command line or Android Studio.
Write picture description here

Application source files

The source file contains the files that make up your Android project, including Java and XML files, icons, and ProGuard configuration files.

Lint.xml file

The configuration file is used to configure the problem you want to ignore, or the severity level of a higher problem.

Lint tools

You can run the static code scanning tool on your Android project through the command line or Android Studio. The Lint tool detects code structural issues that affect the quality and performance of your Android applications. It is strongly recommended to fix any issues detected by Lint before publishing your app

Lint test results

You can view the results of Lint in the Event Log of Android Studio (or on the command line).
The Lint tool is part of the Android SDK tool (>=16).

Run Lint in Android Studio

In Android Studio, Lint runs automatically when you build your app.
In Android Studio you can add the lintOptions attribute in the android settings. as follows:

android {
    lintOptions {
       // 设置为true会关闭lint分析进度
       quiet true
       // 如果为true,则在发现错误时停止gradle构建
       abortOnError false
       // 如果为true,则只报告错误
       ignoreWarnings true
       }
}

Run Lint from the command line

Run lint detection project:

lint [flags] <project directory>

An error in the detection project, the following is used to detect the Android namespace prefix:

lint --check MissingPrefix myproject

View lint help, available:

Lint output example

Next, show the output information of lint detecting an Earthquake project:

$ lint Earthquake

Scanning Earthquake: ...............................................................................................................................
Scanning Earthquake (Phase 2): .......
AndroidManifest.xml:23: Warning: <uses-sdk> tag appears after <application> tag [ManifestOrder]
  <uses-sdk android:minSdkVersion="7" />
  ^
AndroidManifest.xml:23: Warning: <uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?" [UsesMinSdkAttributes]
  <uses-sdk android:minSdkVersion="7" />
  ^
res/layout/preferences.xml: Warning: The resource R.layout.preferences appears to be unused [UnusedResources]
res: Warning: Missing density variation folders in res: drawable-xhdpi [IconMissingDensityFolder]
0 errors, 4 warnings

Configure lint

By default, when you run a scan, all issues supported by Lint will be detected. You can prohibit Lint from detecting an issue or set the security level of the issue.
You can configure Lint detected at different levels:
1, the overall situation for the entire project
2, each project module
3, each test module
and so on.

Configure Lint in Android Studio

When you use Android Studio, the built-in Lint tool can detect your code. You can view warnings and errors in two ways:
1. In the code editor, when Lint finds an error, it will display the problem code in yellow.
2. Select Analyze> Inspect Code to open Lint Inspection Results.

Set the default Lint detection:

1. In Android Studio, open your project.
2. Select File> Other Settings> Default Settings
3. Select Editor> Inspections to open the Default Preferences dialog box.
4. In Profile, select Default or Project Default.
5. Change Lint settings as needed.
6. Click OK.

Display the Lint inspection in the Inspection Results window:

1. In Android Studio, open the project and select the part you want to test.
2. Select Analyze> Inspect Code.
3. In the Specify Inspection Scope dialog box, select the part to be tested.
4. Click OK.
The structure will be displayed in the Inspection Results window according to the classification.

Configure Lint file

You can declare Lint detection parameters in the lint.xml file. If you create this file manually, place it in the root directory of your Android project. If you configure your Lint parameters in Android Studio, lint.xml is automatically generated and added to your project.
The Lint.xml file contains a parent tag, which contains one or more tags. as follows:

<?xml version="1.0" encoding="UTF-8"?>
    <lint>
        <!-- list of issues to configure -->
</lint>

By setting tags, you can disable Lint's detection of an issue or higher level of an issue.
Note: View a list of lint support issue, you can run lint -list command
Lint.xml file example
Here is an example lint.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <!-- Disable the given check in this project -->
    <issue id="IconMissingDensityFolder" severity="ignore" />

    <!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
    <issue id="ObsoleteLayoutParam">
        <ignore path="res/layout/activation.xml" />
        <ignore path="res/layout-xlarge/activation.xml" />
    </issue>

    <!-- 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>

Configure lint detection in Java and XML source files

You can disable Lint detection in Java and XML files

Configure lint detection in Java

In order to detect Jin Yong Lint in the specified class or method in your Android project, add @SupprewwLint annotation to the Java code.

The following example shows how to turn off Lint detection in the OnCreate method, and the Lint tool continues to detect NewApi issues in other methods.

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);

The following example shows how to close the ParserError issue:

@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {}

Disable all Lint detection in Java files, as follows:

@SuppressLint("all")

Configure Lint detection in XML

In the XML file, you can use the tools: ignore attribute to detect Jin Yong Lint. In order for the relevant attributes to be recognized by the Lint tool, you must add the following namespace to your XML document namespace xmlns:tools= http://schemas.android.com/tools :

<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>

Disable multiple issues, as follows:

tools:ignore="NewApi,StringFormatInvalid"

Disable all issue detection, as follows:

tools:ignore="all"

Author: Song Zhihui
personal micro-blog: Click to enter

Guess you like

Origin blog.csdn.net/song19891121/article/details/51726750