Optimizing Android Code with the Lint tool

Source: Internet
Author: User

I. Overview

The Android lint tool is a static code analysis tool that examines potential problems in your code and improves code correctness, security, usability, internationalization, and performance.
Make sure that there are no structural errors in the code and identify potential problems. Android-lint provides command-line execution, integrates with the IDE (Eclipse, Idea, Androidstudio), and provides output reports in HTML form. The android-lint can be easily integrated with other automated systems in the project (Configuration/Build/test, etc.).

II. Application of 2.1 automatic execution

In Androidstudio, for example, lint will run automatically when build is applied. And if you make an error, you stop the build. We can configure the lint option in the project's gradle configuration file

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 represents silent execution, and ignores lint error.

2.2 Manual execution

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

2.3 Line of code execution

Lint provides command-line execution and does not know how to execute it lint–help under.
Android-lint the potential problems checked by the command line $lint–show can be obtained. You can refer to this:

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

Check the approximate format for this:

<project directory>

One of the simplest uses:

lint Android项目目录

Example:

Lint app/Scanning1.0. 1:.....Scanning1.0. 1(Phase2):..Scanning umengupdate-release:......................................Scanning unspecified:...............................................Scanning unspecified (Phase2):....................build/intermediates/exploded-aar/umengupdate-release/res/layout/umeng_update_dialog.XML: -: Warning:consider Adding Android:layout_marginend="8DP"  toBetter support Right-to-leftLayouts[rtlhardcoded]android:layout_marginright= "8DP" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~build/intermediates/ex Ploded-aar/com.afollestad/material-dialogs/0.7.3.1/androidmanifest.xml:warning:the Project References RTL attributes, but does isn't explicitly enable or disable RTL support with Android:supportsrtl in the manifest[Rtlenabled]errors, 444 warnings

The following command detects an error that does not have a named prefix in the XML file:

lint--checkMissingPrefixmyproject

Please refer to the official website for specific commands.

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

Three, lint configuration 3.1 in the settings of the configuration

In Androidstudio, we can configure lint in our project in File > Settings > Project Settings.

3.2 Lint.xml

Refer to a picture in the official documentation:

Very intuitive expression of the role of the Lint.xml file, through the Lint.xml and lint tool together to examine the code in the problem.
An 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 --and    <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 --and    <issue id="Uselessleaf">        <ignore path="Res/layout/main.xml" />    </Issue>    <!--The severity of hardcoded strings to "error" -    <issue id="Hardcodedtext" severity="error" /></Lint>
3.3 Configuring Lint in Java code and XML code

Of course, lint can still be configured in Java code, for example:

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

The above code indicates that the lint check "Newapi" is turned off in the OnCreate method, but lint still checks for other methods that do not have @SuppressLint ("Newapi") tags.
Similar to the following:

@SuppressLint("ParserError")publicclass FeedProvider extends ContentProvider {

Close the "ParserError" check.
If you want to turn off the search check, you can set this:

@SuppressLint("all")

In XML, we can use Tools:ignore to close the corresponding lint check, and to use "Tools:", first add the corresponding namespaces

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

Examples are as follows:

<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 for unused resource files. Likewise, there are:

tools:ignore="NewApi,StringFormatInvalid"

Close all the checks accordingly:

tools:ignore="all"
Iv. Summary

The lint tool is relatively small for individual developers, but if it is a team project, it can be very useful, because everyone's code habits are different, and will define a lot of resource files, so that the cumulative, the size of the APK will not necessarily increase a lot. Personal feeling lint tool The most important one function is poor unuseresources, you can delete a lot of unused resource files, to the APK slimming.
Check results on Android lint–> Unused resources.

This is a useless resource file in our project, a lot of useless pictures Ah!!
After the deletion of a lot of refreshing!

Optimizing Android Code with the Lint tool

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.