Getting The Most Out Of Android Lint

  • Understanding Lint
  • Improving Lint Performance
  • Configuring Lint for better code review

Ways to use Lint

  1. In Android Studio
  2. via Gradle
  3. As a Standalone tool.

In Android Studio

  • On the Fly : Lint is integrated deeply into the Android Studio IDE. It shows error messages or warnings as you type your codes.
lint warning and error on the fly
  • Inspect Code : This is an action in android studio that can keep your coding standard in check by running a batch analysis on your entire project or even a single file . It’s a great way to audit your project for common errors.
running inspect code in a playground project.
  • Run Inspection by name : This is closely related to Inspect Code but with this you can audit your project on a look out for bugs or practices that match a pattern or name. To run inspection by name:

via Gradle

./gradlew lint[flavour][variant] e.g lintDebug or lintStagingDebug
insightful analysis that can help build better apps.

Standalone Tool

lint [flags] <project directory>
lint --help

Advance Use Cases

Configuration

configuring lint using android studio
<?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>
android{
lintOptions{
// ... lint configurations goes here
}
}
  1. Baseline: You can take a snapshot of your project’s current set of warnings, and then use the snapshot as a baseline for future inspection runs so that only new issues are reported. The baseline snapshot lets you start using lint to fail the build without having to go back to address all existing issues first. It’s like giving you a fresh start from old issues.
android {
lintOptions {
baseline file("lint-baseline.xml")
}
}
android {
lintOptions {
warningsAsErrors true
}
}
android {
...
lintOptions {
// Turns off checks for the issue IDs you specify.
disable 'TypographyFractions','TypographyQuotes'
// Turns on checks for the issue IDs you specify. These checks are in
// addition to the default lint checks.
enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
// To enable checks for only a subset of issue IDs and ignore all others,
// list the issue IDs with the 'check' property instead. This property overrides
// any issue IDs you enable or disable using the properties above.
check 'NewApi', 'InlinedApi'
// If set to true, turns off analysis progress reporting by lint.
quiet true
// if set to true (default), stops the build if errors are found.
abortOnError false
// if true, only report errors.
ignoreWarnings true
}
}
  • try to avoid checkAllWarning Options. Be very selective in the checks that are turned on.
  • Also, avoid running gradle lint task. Be specific !
DO NOT DO THIS./gradlew lintDO THIS INSTEAD./gradlew lintDebug or ./gradlew lintProdRelease (depending on whether you have build variant)
@SuppressLint("ParserError")
class FeedProvider : ContentProvider() {
<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>

Annotations

annotations that helps lint help you
example: using thread annotations

Writing Custom Lint rules

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store