Letting Android Lint fix your code

Niklas Baudy
Nov 5, 2018 · 2 min read

Android Lint is deeply integrated into Android Studio. Issues that are reported will be highlighted either as warnings or errors.

Image for post
Image for post
Lint warning about synthetic accessor methods

Since Android Studio 3.0.0 the integration has gotten even better as we can quick-fix issues by pressing Alt + Enter.

Image for post
Image for post
Lint telling us to make Foo#foo package private

After doing so the warning is gone.

Image for post
Image for post
No Lint warning anymore \o/

While that is super good there hasn’t been anything for the Terminal. Not anymore. Starting with Android Gradle Plugin 3.2.0 Lint got a lot smarter and you can simply execute the lintFix Gradle task.

Image for post
Image for post

It will fix all of the issues it can and break the build since changes were done after compilation. Doing a git diff yields the correct change that is required:

Image for post
Image for post
Quick-fix diff

Super handy. With this, you can let Lint auto perform a bunch of fixes and you don’t need to do them hand by hand.

Not every Lint rule supports this out of the box. Authors of Lint rules need to opt-in:

This is an example of one of my lint rules that replaces org.assertj.core.api.Assertions imports with org.assertj.core.api.Java6Assertions. As you can see it’s pretty trivial to add quick-fix support.

If you want to learn more about writing your first custom Lint rules check out my article for more information.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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