Use lint tool to optimize Android code, lintandroid

Source: Internet
Author: User

Use lint tool to optimize Android code, lintandroid
I. Overview

Android lint is a static code analysis tool used to check potential problems in the Code and improve code correctness, security, availability, internationalization and performance.
Make sure there are no structural errors in the Code and discover potential problems. Android-Lint provides command line execution, integration with IDE (eclipse, Idea, and AndroidStudio), and html output reports. Android-Lint can be easily integrated with other automatic systems in the project (configuration/Build/test, etc.

Ii. Automatic Execution of application 2.1

Taking AndroidStudio as an example, lint will automatically run when you build an application. If an error is reported, the build will be stopped. We can configure the lint option in the gradle configuration file of the project.

android {    lintOptions {       // set to true to turn off analysis progress reporting by lint       quiet true       // if true, stop the gradle build if errors are found       abortOnError false       // if true, only report errors       ignoreWarnings true       }       ...    }

The above code indicates silent execution and ignore lint errors.

2.2 manual execution

Of course, we can also manually execute lint to operate Analyze> Inspect Code.

2.3 code line execution

Lint provides command line execution. If you do not know how to execute it, you can use lint-help.
The potential problems detected by Android-Lint can be obtained through the command line $ lint-show. You can refer to here:

Http://tools.android.com/tips/lint

Check that the approximate format is as follows:

lint [flags] <project directory>

The simplest usage:

Lint Android project directory

Example:

lint app/                                                                                                                                             Scanning 1.0.1: .....Scanning 1.0.1 (Phase 2): ..Scanning umengupdate-release: ......................................Scanning unspecified: ...............................................Scanning unspecified (Phase 2): ....................build/intermediates/exploded-aar/umengupdate-release/res/layout/umeng_update_dialog.xml:47: Warning: Consider adding android:layout_marginEnd="8dp" to better support right-to-left layouts [RtlHardcoded]                android:layout_marginRight="8dp"                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~build/intermediates/exploded-aar/com.afollestad/material-dialogs/0.7.3.1/AndroidManifest.xml: Warning: The project references RTL attributes, but does not explicitly enable or disable RTL support with android:supportsRtl in the manifest [RtlEnabled]26 errors, 444 warnings

The following command can detect errors without naming prefixes in xml files:

lint --check MissingPrefix myproject

For specific commands, refer to the official website

Http://developer.android.com/tools/help/lint.html

3. Configure lint 3.1 in settings

In AndroidStudio, We can configure lint in File> Settings> Project Settings in our Project.

3.2 lint. xml

Reference an image in the official document:

It intuitively expresses the role of the lint. xml file. Through the combination of lint. xml and lint Tool, you can check the problems in the code.
Example:

<?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>
3.3 configure lint in Java code and XML Code

Of course, lint can still be configured in Java code. Example:

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

The code above indicates that the lint check "NewApi" is disabled in the onCreate method, but the lint still checks other methods without the @ SuppressLint ("NewApi") Mark.
Similar:

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

Disable the "ParserError" check.
If you want to disable the search check items, you can set them as follows:

@SuppressLint("all")

In XML, we can use tools: ignore to disable the corresponding lint check items. To use tools:, add the corresponding namespaces

namespace xmlns:tools="http://schemas.android.com/tools"

Example:

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

Avoid checking unused resource files. There are also:

tools:ignore="NewApi,StringFormatInvalid"

Close all check items accordingly:

tools:ignore="all"
Iv. Summary

The lint tool is of little use to individual developers. However, if it is a team project, it will play a significant role, because everyone's code habits are different, in addition, a lot of resource files will be defined, so that the size of the apk will not be necessary to increase a lot over time. I personally feel that one of the most important functions of the lint tool is poor unUseResources, which can delete a lot of unused resource files and slim down the apk.
Check the result in Android Lint-> Unused resources.

This is a useless resource file in our project, a lot of useless pictures !!
It's refreshing after deletion!

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.