Minimal working SpotBugs setup for Android Studio-ThrowExceptions

Exception or error:

How do I set up SpotBugs for Android?

I tried following the official documentation and that of the gradle plugin, but the setup for Android is incomplete and confusing, and didn’t work.

I tried the following setup.

build.gradle (project):

buildscript {
  repositories {
    // ...
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    // ...
    classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.4"
  }
}

build.gradle (app):

//...
apply plugin: "com.github.spotbugs"

android {
  // ...
  sourceSets {
    main {
      java.srcDirs = ['src/main/java']
    }
  }
}

// ...

spotbugs {
    toolVersion = "3.1.3"
    ignoreFailures = true
    reportsDir = file("$project.buildDir/findbugsReports")
    effort = "max"
    reportLevel = "high"
}

tasks.withType(com.github.spotbugs.SpotBugsTask) {
  // What do I need to do here?
}

I tried running it with ./gradlew spotbugsMain, but the gradle task is missing.
Am I supposed to add the task manually? How do I do that?

Could you show me an example of a minimal working setup for an Android project?

How to solve:

I made some tests on my side and I manage to make it work like this :

1) Move the sourceSets declaration outside the android block. Leave it empty, it’s just for the spotbugsMain task generation, it won’t impact the global Android build.

android {
   // ...
}

sourceSets {
    main {
        java.srcDirs = []
    }
}

2) Keep your spotbugs block and configure the SpotBugsTask tasks like this :

tasks.withType(com.github.spotbugs.SpotBugsTask) {
    classes = files("$projectDir.absolutePath/build/intermediates/classes/debug")
    source = fileTree('src/main/java')
}

It will generate reports in app/build/findbugsReports

Important :

It only works with the ./gradlew build command, ./gradlew spotbugsMain won’t work as the project must be built before

You can fix that adding an assemble dependency :

tasks.withType(com.github.spotbugs.SpotBugsTask) {
    dependsOn 'assemble'
    classes = files("$projectDir.absolutePath/build/intermediates/classes/debug")
    source = fileTree('src/main/java')
}

###

Following on from ToYonos answer (9 October 2018); Use this for Android Studio 3.4 and above:

project/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https:// maven url 1'
        }
        maven {
            url "https://plugins.gradle.org/m2/" // For SpotBugs
        }
    }
    dependencies {
        classpath '...'
        classpath "com.github.spotbugs:spotbugs-gradle-plugin:2.0.1" // For SpotBugs
    }
}

project/app/build.gradle

apply plugin: 'com.android.application'
apply plugin: '...'
apply plugin: "com.github.spotbugs"

dependencies {
    ...
}

// For SpotBugs to create 'spotbugsMain' gradle task
sourceSets {
    main {
        java.srcDirs = []
    }
}

spotbugs {
    ignoreFailures = true
    reportsDir = file("$project.buildDir/SpotBugsReports")
    effort = "max"
    reportLevel = "high"
}

tasks.withType(com.github.spotbugs.SpotBugsTask) {
    dependsOn 'assembleDebug'
    classes = files("$project.buildDir/intermediates/javac") // Important to use this path
    excludeFilter = file("$project/spot-bugs-exclude.xml") // Optional - Explained below
    source = fileTree('src/main/java')
    reports {
        // Enable HTML report only
        html.enabled = true
        xml.enabled = false
    }
}

You can generate a report for your debug build by running the gradle task:

./gradlew spotbugsMain

It’s important to use classes = files("$project.buildDir/intermediates/javac") , otherwise you’ll get an error "java.io.IOException: No files to analyze could be opened" — see Findbugs fails with "java.io.IOException: No files to analyze could be opened"

You’ll also need to enable the HTML report and disable XML report, to see a human-readable format.

To exclude some generated classes from the analysis, setup an excludeFilter. For a sample exclude file, check here or here (same as findbugs-exclude.xml)

Leave a Reply

Your email address will not be published. Required fields are marked *