質問

I am trying to use the FindBugs plugin for Gradle with an Android build.

The build.gradle file

buildscript {
  repositories {
    mavenCentral()        
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.6.+'
  }
}
apply plugin: 'android'
apply plugin: 'findbugs'

android {
  compileSdkVersion 19
  buildToolsVersion "19.0.0"
  defaultConfig {
    minSdkVersion 8
    targetSdkVersion 19        
  }
}

dependencies {
  compile 'com.android.support:appcompat-v7:+'
}

But when I execute the check task it says

No classes configured for FindBugs analysis.

How do I configure classes for FindBugs analysis?

役に立ちましたか?

解決

This is not possible at the moment as findbugs expect Gradle's normal Java SourceSets but the android plugin uses custom ones.

There's work planned in both Gradle and in the Android plugin to allow using the default SourceSets which will enable FindBugs.

You track this issue here: https://code.google.com/p/android/issues/detail?id=55839

他のヒント

In newer versions of Android Studio this problem could be attributed to the fact that the location of the classes directory has changed.

The new location (as of 0.8.2) is:

build/intermediates/classes/{build_variant_name}/

for example

build/intermediates/classes/debug/
task findbugs(type: FindBugs) {
    excludeFilter = file('config/findbugs/findbugs.xml')
    ignoreFailures = true
    classes = fileTree('build/intermediates/classes/preproduction/')
    source = fileTree('src/main/java/')
    classpath = files()
    effort = 'max'
    reports {
        xml {
            destination "reports/findbugs.xml"
        }
    }
}

This is definitely possible. At least now. The answer can be seen on https://gist.github.com/rciovati/8461832 at the bottom.

apply plugin: 'findbugs'

// android configuration

findbugs {
    sourceSets = []
    ignoreFailures = true
}

task findbugs(type: FindBugs, dependsOn: assembleDebug) {

    description 'Run findbugs'
    group 'verification'

    classes = fileTree('build/intermediates/classes/debug/')
    source = fileTree('src/main/java')
    classpath = files()

    effort = 'max'

    excludeFilter = file("../config/findbugs/exclude.xml")

    reports {
        xml.enabled = true
        html.enabled = false
    }
}

check.doLast {
    project.tasks.getByName("findbugs").execute()
}

The important part here is dependsOn: assembleDebug. Without that you will get a No classes configured for FindBugs analysis error message.

Refer to this https://stackoverflow.com/a/7743935/1159930 for the exclude file.

I was able to solving the problem

by adding find bug as separate task

 task findbugs(type: FindBugs) {
    ignoreFailures = true
    classes = fileTree('build/classes/debug/')
    source = fileTree('src/main/java/')
    classpath = files()
   effort = 'max'
 }

this task can run using

gradle findbugs

If you are using android-test plugin you have to exclude findbugsTestDebug task when build.

gradle build -x  findbugsTestDebug

I have to specify the default sourceSet for findbugs. Initially it wasnt there so I was getting the error.

    findbugs {
    sourceSets = []    //Default sourceSet
    toolVersion = "3.0.1"      
    ignoreFailures = true      
    reportsDir = file("$project.buildDir/findbugsReports")
    effort = "max"
    reportLevel = "low"
}

As other people wrote, you have to set sourceSets, i.e.:

task findbugs(type: FindBugs) {
    // ignoreFailures = false
    ignoreFailures = true
    effort = 'max'
    reportLevel = 'low'
    excludeFilter = file("quality/findbugs/findbugs-filter.xml")
    classes = files("${project.buildDir}/intermediates/javac/debug/classes",

    source "${file(getProjectDir().getPath()).getAbsolutePath()}/src"
    include '**/*.java'
    exclude "${project.buildDir}/generated/**/*.java"

    reports {
        xml.enabled = true
        xml {
            destination file("findbugs/report.xml")
        }
        /*
        html.enabled = true
        html {
            destination file("findbugs/report.html")
        }
        */
        /*
        text.enabled = true
        text {
            destination file("findbugs/report.txt")
        }
        */
    }

    classpath = files()
}

The problem is that when you upgrade version of Android Gradle Plugin, this path changes now and then.

In our project in different times it was of following values:

"${project.buildDir}/intermediates/classes/debug"
"${project.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes"
"${project.buildDir}/intermediates/javac/debug/classes"

So if none of mentioned above values worked out, try to find actual classes in your build tree, maybe they just changed it again.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top