Setup Android Studio lint checks without copying lint.jar to ~/.android/lint

1,204 views
Skip to first unread message

Andrew Kelly

unread,
Apr 9, 2017, 11:13:24 PM4/9/17
to lint-dev
Hi,

I'm just getting started in custom lint rules, I have a basic rule that I want to have Gradle and Android studio highlight - use my logger instead of android.util.Log.

Is the only way to package lint checks as a lint.jar that is included in ~/.android/lint ? This seems cumbersome at scale e.g. if there are many developers on the team, and keeping them all up to date.

Ideally I'm looking for a way that I can specify the path to my issue registry inside the gradle lintOptions{} tag. Is this possible, or is there an alternate way? I'm just following another example that creates a lint-checks java module that other modules depend upon.

Thanks.

PS: So far I've been reading here.




Tor Norbye

unread,
Apr 9, 2017, 11:18:16 PM4/9/17
to Andrew Kelly, lint-dev
The best way to distribute custom lint rules is to include them in an AAR file.
When your project depends on a library AAR, lint will look inside that AAR to see if it contains custom lint rules (named lint.jar) and if so will load them and apply them.

That means that if you for example have a custom logging library, you can also supply lint rules to enforce correct usage of your library, and anyone depending on your library will also get those lint checks applied automatically.

(The ~/.android/lint mechanism is older, and is obsolete now.)

AAR files are created automatically when you apply the com.android.library Gradle plugin and run the assemble target.  Unfortunately, that doesn't package the lint rules into it - because right now there isn't any packaging support for lint rules. That's planned, but not done yet (it's pending the lint APIs becoming stable), so right now you'll need to do manual surgery on the aar file during/after the build (e.g. build your lint.jar then insert it into the AAR).

-- Tor

--
You received this message because you are subscribed to the Google Groups "lint-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lint-dev+u...@googlegroups.com.
To post to this group, send email to lint...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lint-dev/82976ad1-a7f6-4b6d-93c5-3eb662ee7dfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tor Norbye

unread,
Apr 9, 2017, 11:19:05 PM4/9/17
to Andrew Kelly, lint-dev
On Sun, Apr 9, 2017 at 8:18 PM Tor Norbye <tno...@google.com> wrote:
The best way to distribute custom lint rules is to include them in an AAR file.
When your project depends on a library AAR, lint will look inside that AAR to see if it contains custom lint rules (named lint.jar) and if so will load them and apply them.

....and if you just have some other type of lint check (e.g. a team wide convention) you can put them in an empty Gradle library project and then you're depending on your lint-rules wrapper project.

Marc Prengemann

unread,
Jun 1, 2017, 5:52:40 AM6/1/17
to lint-dev, andrew...@swizel-studios.com
That is exactly what I'm doing. The checks are also picked up when I execute lint through the Gradle task. But it seems that they are ignored when I depend on the library directly within Gradle (`compile project(":lintlib")`). Is this supposed to work?

Tor Norbye

unread,
Jun 12, 2017, 11:15:33 AM6/12/17
to lint-dev, andrew...@swizel-studios.com
On Thursday, June 1, 2017 at 2:52:40 AM UTC-7, Marc Prengemann wrote:
That is exactly what I'm doing. The checks are also picked up when I execute lint through the Gradle task. But it seems that they are ignored when I depend on the library directly within Gradle (`compile project(":lintlib")`). Is this supposed to work?

That used to work, but I know that in 3.0 there's been a lot of optimizations to the build pipeline where for example it doesn't generate AAR files when it doesn't need to -- so maybe this doesn't work anymore. 
This -- making support for custom rules work directly as part of the build system, e.g. with a dedicated lint source set that compiles lint rules and packages them etc -- is something we plan on doing once the lint API itself is stable.

-- Tor 

Lin Wang

unread,
Aug 17, 2017, 8:34:57 PM8/17/17
to lint-dev, andrew...@swizel-studios.com
My build process copies a pre-generated lint.jar into the `build/intermediates/lint` build folder which is kinda working fine for us, but every time testing a new lint.jar I have to clean project, kill gradle daemon runner, etc to make the system pick up the new lint.jar

Also looking forward to have the aar packing approach supported as a gradle task.

Lin 

yuan...@bytedance.com

unread,
Feb 14, 2018, 9:44:13 AM2/14/18
to lint-dev
What can I do in 3.0?I want to run lint on main project and check all library projects

在 2017年6月12日星期一 UTC+8下午11:15:33,Tor Norbye写道:

Tor Norbye

unread,
Feb 14, 2018, 9:47:22 AM2/14/18
to lint-dev
This should be possible now; see https://github.com/googlesamples/android-custom-lint-rules/tree/master/android-studio-3 for a sample which uses the new lintChecks packaging mechanism to locally build and package lint rules that you can then apply to for example app modules.

-- Tor
Reply all
Reply to author
Forward
0 new messages