What Is Android Lint?

Vishnu jm
The Startup
Published in
5 min readJun 26, 2020

--

A lot of us probably do know what actually Android Lint is, even if we don’t know the name. Because it is the thing that shows warning messages in the IDE. It may be warnings or errors that will actually fail the build. The following images shows an example of Lint suggestions.

Gradle version out of date
Not calling super method where expected

Android Studio provides a code scanning tool called lint that can help you to identify and correct problems with the structural quality of your code. For example, Lint can help us clean up these issues

  • XML resource files containing unused namespaces
  • Use of deprecated elements
  • API calls that are not supported by the target API versions, might lead to code failing to run correctly
  • Lot more..

The problems detected in our code by the Lint will be reported to us with some suggestions and a warning level. We can use the suggestion to correct our code. Lint can customized to see a particular type of error in our project. When using Android Studio, inspection process run whenever we build our project. However, we can manually run inspections or run lint from the command line.

The basic topics that is covered are:

  • How to use Lint
  • Working of Lint
  • How to Configure lint
  • Using Baseline

How to use Lint

By Android Studio:

In order to manually inspect a file in Android Studio, click on Analyze > Inspect Code. Below window will be opened:

Here we can select your inspection scope i.e. whole project or module or simply a file. After selecting the scope click on OK.

Under Inspection Results, in the left pane tree view, error categories, types, and issues is displayed.

The right pane displays the inspection report for the selected error category, type, or issue and provides the name and location of the error.

Run lint from the command line:

Invoke the lint task for your project by entering one of the following commands from the root directory of your project:

On Windows:

gradlew lint

On Linux or Mac:

./gradlew lint

When the lint tool completes its checks, it provides paths to the XML and HTML versions of the lint report.

Working of Lint

  1. The lint.xml file: We can customize the Lint checks in the lint.xml file. In this file, we can define severity for the issue such as error, ignore, warning. Apart from this, we can customize the Lint checks manually also. We will see how to do manual Lint checks in the later part of this blog.
  2. Application source file: It is the source files on which we want to perform the Lint check. It can be .java file or .kt file or any xml file of the project.
  3. The Lint tool: Finally, the lint tool takes the source and lint.xml files and checks for structural code problems and suggest some improvements in the code if any. The tool that can be run on our Android project either from the command line or manually in Android Studio

The following image shows the working of lint tool:

Code scanning workflow with the lint tool
Code scanning workflow with the lint tool

How to Configure lint:

When you run a lint scan, the tool checks for all issues that lint supports by default. It takes lots of processing time. We can also restrict the issues for lint to check and assign the severity level for those issues. For example, we can suppress lint checking for specific issues that are not relevant to our project. Lint checking can be performed at different levels such as Globally(entire project), Project module, Test module etc.

Configuring lint in Android Studio:

We can select and deselect the issues to be checked by Lint on Inspection Results window by clicking Analyze > Inspect Code

  1. Select Analyze > Inspect Code.
  2. In the Specify Scope dialog under Inspection Profile, click More.

The Inspections dialog appears with a list of the supported inspections and their descriptions.

  • Select the Profile drop-down list to toggle between Default (Android Studio) and Project Default (the active project) inspections
  • In the Inspections dialog in the left pane, we can edit the inspections
  • Click OK when done.

Configuring the lint file:

We can specify manual checks in our app by adding the list of issues to be configured in the lint.xml file. While creating a new lint.xml file, it should be placed into the root directory of our Android project.

Click here to view the list of available lint checks and corresponding issue id’s.

Configuring lint checking in Java or Kotlin:

To disable lint checking specifically for a class or method in our Android project, add the @SuppressLint annotation to that code.

The following example shows how we can turn off lint checking for the NewApi issue in the onCreate method. However the lint tool continues to check for the NewApi issue in other methods of this class.

Using Baseline:

If we are working on a large project and interested in finding the future errors that might arise while adding some more codes to our project then we can add a baseline to the project and by doing so the lint will generate the errors that have occurred after that baseline. So, lint will ignore the previous code issues and will warn us only about the new lines of code added after the baseline.

To create a baseline snapshot, modify your project’s build.gradle file as follows

android {
lintOptions {
baseline file("lint-baseline.xml")
}
}

Conclusion

Android Lint is a powerful tool where we can also write our own custom Lint checks. Lint tool can be utilized effectively to build an app with greater optimization.

--

--