The Lint tool checks your Android project source files 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.
Configure LintOptions with gradle
You can configure lint by adding a
lintOptions section in the
You can run lint for a specific variant (see below), e.g.
./gradlew lintRelease, or for all variants (
./gradlew lint), in which case it produces a report which describes which specific variants a given issue applies to.
Check here for the DSL reference for all available options.
Configuring lint checking in Java and XML source files
You can disable Lint checking from your Java and XML source files.
Configuring lint checking in Java
To disable Lint checking specifically for a Java class or method in your Android project, add the
@SuppressLint annotation to that Java code.
To disable checking for all Lint issues:
Configuring lint checking in XML
You can use the
tools:ignore attribute to disable Lint checking for specific sections of your XML files.
To suppress checking for all Lint issues in the XML element, use
How to configure the lint.xml file
You can specify your Lint checking preferences in the
lint.xml file. If you are creating this file manually, place it in the root directory of your Android project. If you are configuring Lint preferences in Android Studio, the lint.xml file is automatically created and added to your Android project for you.
By setting the severity attribute value in the tag, you can disable Lint checking for an issue or change the severity level for an issue.
The following example shows the contents of a
Importing resources without "Deprecated" error
Using the Android API 23 or higher, very often such situation can be seen:
This situation is caused by the structural change of the Android API regarding getting the resources. Now the function:
should be used. But the android.support.v4 library has another solution.
Add the following dependency to the build.gradle file:
Then all methods from support library are available:
Moreover more methods from support library can be used:
Mark Suppress Warnings
It's good practice to mark some warnings in your code. For example, some deprecated methods is need for your testing, or old support version. But Lint checking will mark that code with warnings. For avoiding this problem, you need use annotation @SuppressWarnings.
For example, add ignoring to warnings to deprecated methods. You need to put warnings description in annotation also:
Using this annotation you can ignore all warnings, including Lint, Android, and other. Using Suppress Warnings, helps to understand code correctly!
Using tools:ignore in xml files
tools:ignore can be used in xml files to dismiss lint warnings.
BUT dismissing lint warnings with this technique is most of the time the wrong way to proceed.
A lint warning must be understood and fixed... it can be ignored if and only if you have a full understanding of it's meaning and a strong reason to ignore it.
Here is a use case where it legitimate to ignore a lint warning:
- You are developing a system-app (signed with the device manufacturer key)
- Your app need to change the device date (or any other protected action)
Then you can do this in your manifest : (i.e. requesting the protected permission and ignoring the lint warning because you know that in your case the permission will be granted)