Documente Academic
Documente Profesional
Documente Cultură
In This Document
Overview
Running lint from Android
Studio
Running lint from the
command-line
Conguring lint
Conguring Lint in Android
Studio
Conguring the lint le
Conguring lint checking in
Java and XML source les
See Also
Using Android Annotations
Overview
Android Studio provides a code scanning tool
called Lint that can help you to easily identify and correct problems with the
structural quality of your code, without having to execute the app or write any test
cases. Each problem detected by the tool is reported with a description message
and a severity level, so that you can quickly prioritize the critical improvements that
need to be made. You can also congure a problem's severity level to ignore issues
that are not relevant for your project, or raise the severity level. The tool has a
command-line interface, so you can easily integrate it into your automated testing
process.
The Lint tool checks your Android project source les for potential bugs and
optimization improvements for correctness, security, performance, usability,
accessibility, and internationalization. You can run Lint from the command-line or
from Android Studio.
Note: In Android Studio, additional IntelliJ code inspections
(https://www.jetbrains.com/help/idea/2016.1/code-inspection.html)
or higher.
Note: To view and modify inspection severity levels, use the File > Settings >
Editor > Inspections menu to open the Inspection Conguration page with a list
of the supported inspections.
With Android Studio, you can also run Lint inspections for a specic build variant, or
for all build variants from the build.gradle le. Add the lintOptions property to
the android settings in the build le. This code snippet from a Gradle build le
shows how to set the quiet option to true and the abortOnError option to false.
android {
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet ttrruuee
// if true, stop the gradle build if errors are found
abortOnError ffaallssee
// if true, only report errors
ignoreWarnings ttrruuee
}
...
}
For example, you can issue the following command to scan the les under the
myproject directory and its subdirectories. The issue ID MissingPrefix tells Lint
to only scan for XML attributes that are missing the Android namespace prex.
lint --check MMiissssiinnggPPrreeffiixx myproject
To see the full list of ags and command-line arguments supported by the tool:
lint --help
The output above lists four warnings and no errors in this project. Three warnings
(ManifestOrder, UsesMinSdkAttributes, and UnusedResources) were found in
the project's AndroidManifest.xml le. The remaining warning
(IconMissingDensityFolder) was found in the Preferences.xml layout le.
Conguring lint
By default, when you run a Lint scan, the tool checks for all issues that are
supported by Lint. You can also restrict the issues for Lint to check and assign the
severity level for those issues. For example, you can disable Lint checking for
specic issues that are not relevant to your project and congure Lint to report
non-critical issues at a lower severity level.
You can congure Lint checking at different levels:
Globally, for the entire project
Per project module
Per production module
Per test module
Per open les
Per class hierarchy
Per Version Control System (VCS) scopes
for
1. In Android Studio, open your project and select a portion of your project that you
want to test.
2. Select Analyze > Inspect Code.
3. In the Specify Inspection Scope dialog, select the inspection scope
(https://www.jetbrains.com/help/idea/2016.1/specify-inspection-scope-dialog.html?origin=old_help)
and
prole.
The scope species the les you want to analyze, and the prole species the
Lint checks youd like to perform.
4. If you want to change the Lint settings, click . In the Inspections dialog,
optionally click Manage to dene a new prole, specify the Lint settings you
want, and then click OK.
In the Inspections dialog, you can search for a string to nd Lint checks. Note
that changing Lint settings for a prole in the Inspections dialog doesnt change
the default settings, as described in the previous procedure. It does change the
settings for proles displayed in the Inspections dialog, however.
5. Click OK.
The results appear in the Inspection Results window, organized by category.
By setting the severity attribute value in the <issue> tag, you can disable Lint
checking for an issue or change the severity level for an issue.
Tip: To see the full list of issues supported by the Lint tool and their
Sample lint.xml le
The following example shows the contents of a lint.xml le.
<?xml version="1.0" encoding="UTF-8"?>
<<lliinntt>>
<!-- Disable the given check in this project -->
<<iissssuuee id="IconMissingDensityFolder" severity="ignore" //>>
<!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
<<iissssuuee id="ObsoleteLayoutParam">>
<<iiggnnoorree path="res/layout/activation.xml" //>>
<<iiggnnoorree path="res/layout-xlarge/activation.xml" //>>
<<//iissssuuee>>
<!-- Ignore the UselessLeaf issue in the specified file -->
<<iissssuuee id="UselessLeaf">>
<<iiggnnoorree path="res/layout/main.xml" //>>
<<//iissssuuee>>
ssuuppeerr.onCreate(savedInstanceState);
setContentView(R.layout.main);
The following example shows how to turn off Lint checking for the ParserError
issue in the FeedProvider class:
@SuppressLint("ParserError")
ppuubblliicc ccllaassss FFeeeeddPPrroovviiddeerr eexxtteennddss CCoonntteennttPPrroovviiddeerr {
To suppress checking for all Lint issues in the Java le, use the all keyword, like
this:
@SuppressLint("all")
The following example shows how you can turn off Lint checking for the
UnusedResources issue for the <LinearLayout> element of an XML layout le.
The ignore attribute is inherited by the children elements of the parent element in
which the attribute is declared. In this example, the Lint check is also disabled for
the child <TextView> element.
<<LLiinneeaarrLLaayyoouutt
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="UnusedResources" >>
<<TTeexxttVViieeww
android:text="@string/auto_update_prompt" //>>
<<//LLiinneeaarrLLaayyoouutt>>
To disable more than one issue, list the issues to disable in a comma-separated
string. For example:
tools:ignore="NewApi,StringFormatInvalid"
To suppress checking for all Lint issues in the XML element, use the all keyword,
like this:
tools:ignore="all"