FindBugs for Android

The very useful code quality plugins for Gradle (Checksytle, FindBugs, PMD, etc) expect to have the Java plugin applied, which is incompatible with the new Android gradle build system. Some developers, like Netflix, resolve this problem by omitting the plugins from their otherwise standardized build process. Others have tried to create a build system inside another build system, at which point you can have whatever plugins you want, and most use a wrapper around the Ant tasks which predate Gradle. Behind the scenes, this is what the Gradle plugins do anyway.

Manually adding sourcesets will allow us to add the plugins, but FindBugs depends on the tasks configured as part of the Java plugin. Obviously, the task dependencies are not going to work, unless you want to configure a Java build parallel to an Android build.

Verification tasks
check - Runs all checks.
    classes - Assembles binary 'main'.
    compileJava - Compiles source set 'main:java'.
    findbugsMain - Run FindBugs analysis for main classes
    processResources - Processes source set 'main:resources'.

Alternatively, one can write a Task class which understands Android SourceSets and wraps the Ant task.

This is pretty straightforward for Checkstyle:

and PMD:

These are included like any other tasks or plugin definition:

...
buildscript {
    dependencies {
        classpath "com.android.tools.build:gradle:$versions.android"
    }
}

apply plugin: 'android'
apply from : "$rootDir/gradle/checkstyle.gradle"
...

FindBugs is a little more difficult, since it expects to have the library JAR files locally installed or included in the source distribution. This isn’t very Gradle-y, but since a Gradle Configuration is also FileCollection, we can add the files from the Gradle cache as an Ant node.

See the full examples on GitHub.