Leveraging Android Lint Checks

Jeremy Rempel
ProAndroidDev
Published in
3 min readJul 11, 2020

--

What is Android Lint?

  • Android lint is a static analysis tool included in Android SDK and integrated within Android Studio. Available within Android Studio as warnings or run directly via gradle via lintDebug
  • Lint checks are run directly within Android Studio on the fly, additional checks disabled by default such as library updates or thread usage analysis can be run by “Analyze->Run Inspection by Name”
Manually running named inspections

Why Lint

  • Identify and correct issues prior to release
  • Enforce custom rules, reduce on-boarding time for new engineers. Rather than document best practices, standards and gotchas just write a lint rule
  • Library authors can ensure that library is being used correctly and any potential pitfalls are captured and reported directly to developers in IDE

Getting more from Lint

  • Many integration are disabled by default. Check disabled inspections (Editor->Inspections). For example Java/Kotlin interop inspections are disabled by default
  • Integrate with CI
  • Setup a baseline to ignore all existing issues, fail only on new issues
  • Add custom lint checks for your project either directly in app or include in library aar. Custom lint checks can check java/kotlin source, xml files such as layouts, AndroidManifest and gradle files. Custom lint checks included in aar will automatically be applied to apps that consume the library

Custom Lint Checks: Overview

All custom lint checks will have the following components:

Specification

  • Specify unique identifier, description, category, priority and severity
  • Specify unique lint check within the IssueRegistry
  • Example: LogWtfUsage

Filter

  • What should lint check be applied to? Lint check can be filtered to evaluate specific methods, classes, annotations, import statements
  • Example: Identify all methods named wtf

Evaluation and Report

  • Perform any additional analysis such as checking package names, evaluating children
  • If issue is identified report issue details along with optional metadata for quickfix
Custom Lint check with suggested replacement in Android Studio

Custom Lint Checks: Setup

  • Create new module that will contain the source of the custom lint checks
  • Lint checks can be applied directly to app implementation lintChecks(project(":mylintchecks")) or in included in an aar lib module implementation lintPublish(project(":mylintchecks"))
  • Add custom lint checks to the IssueRegistry
  • See Google Sample or Sample Project
lint check module build.gradle
Issue Registry

Recipe: Find usages of a method and suggest replacement

Identify and replace all usages of android.util.Log.wtf, notify user and allow them to apply a quickfix replacement Log.f

Recipe Deprecate Libraries

One of the libraries in use has been deprecated. Since we don’t own the code we cannot use the @Deprecated annotation but would like to avoid new usages. Identify and report all usages of MyDeprecatedLibClass()

Testing

Lint framework provides some utilities in the form of a builder pattern to test lint checks. The easiest way to debug and build a lint test is often to build an associated test.

Further Reading

--

--

Follow me on twitter @ jeremyrempel . I write and talk about things that are interesting to me. Thoughts and ideas are my own.