25

如何为 Android 设置 SpotBugs?

我尝试遵循官方文档gradle plugin的文档,但 Android 的设置不完整且令人困惑,并且不起作用。

我尝试了以下设置。

build.gradle(项目):

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

build.gradle(应用程序):

//...
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?
}

我尝试使用 运行它./gradlew spotbugsMain,但缺少 gradle 任务。
我应该手动添加任务吗?我怎么做?

你能告诉我一个 Android 项目的最小工作设置的例子吗?

4

2 回答 2

12

我做了一些测试,我设法让它像这样工作:

1)将sourceSets声明移到android块外。留空,它只是用于spotbugsMain任务生成,不会影响全局 Android 构建。

android {
   // ...
}

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

2)保留你的spotbugs块并配置这样的SpotBugsTask任务:

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

它将在app/build/findbugsReports

重要的 :

它只适用于./gradlew build命令,./gradlew spotbugsMain不能工作,因为必须先构建项目

您可以解决添加assemble依赖项的问题:

tasks.withType(com.github.spotbugs.SpotBugsTask) {
    dependsOn 'assemble'
    classes = files("$projectDir.absolutePath/build/intermediates/classes/debug")
    source = fileTree('src/main/java')
}
于 2018-10-09T10:32:53.777 回答
8

继 ToYonos 回答(2018 年 10 月 9 日)之后;将此用于 Android Studio 3.4 及更高版本:

项目/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https:// maven url 1'
        }
        maven {
            url "https://plugins.gradle.org/m2/" // Add this, for SpotBugs
        }
    }
    dependencies {
        classpath '...'

        // If you're using gradle 6.x, add this to use SpotBugs app version 4.0.2
        classpath "gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.3.0"

        // If you're using gradle 4.x or 5.x, add this to use SpotBugs app version 3.1.2
        classpath "com.github.spotbugs:spotbugs-gradle-plugin:2.0.1" 
    }
}

项目/应用程序/build.gradle

apply plugin: 'com.android.application'
apply plugin: '...'
apply plugin: "com.github.spotbugs" // <- Add this
    
dependencies {
    ...
}

// This block is only needed for gradle 4/5 only.
// It's for SpotBugs to create a 'spotbugsMain' gradle task.
sourceSets {
    main {
        java.srcDirs = []
    }
}
    
spotbugs {
    ignoreFailures = true
    reportsDir = file("$project.buildDir/SpotBugsReports")
    effort = "max"
    reportLevel = "high"
}

// Note: gradle 4/5 should use "com.github.spotbugs.SpotBugsTask"
tasks.withType(com.github.spotbugs.snom.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') // Only needed on gradle 4/5
    reports {
        // Enable HTML report only
        html.enabled = true
        xml.enabled = false
    }
}

您可以通过运行 gradle 任务为您的调试构建生成报告:

对于 gradle 6.x:./gradlew spotbugsDebug

对于 5 级或 4 级:./gradlew spotbugsMain

使用 很重要classes = files("$project.buildDir/intermediates/javac"),否则你会得到一个错误"java.io.IOException: No files to analyze could be opened"——见Findbugs failed with "java.io.IOException: No files to analyze could be open"

您还需要启用 HTML 报告并禁用 XML 报告,以查看人类可读的格式。

ignoreFailures = true是可选的。当 SpotBugs 检测到代码警告时,默认情况下它将以"BUILD FAILED"+ 报告文件结尾。设置ignoreFailures = true意味着 gradle 任务将以"BUILD SUCCESSFUL"+ 报告文件结束。

要从分析中排除某些生成的类,请设置一个excludeFilter. 对于示例排除文件,请在此处此处查看(与 findbugs-exclude.xml 相同)

更多信息和教程在这里:https ://mikedemaso.com/tech/2020-06-10-spotbugs-gradle-plugin-android/

于 2019-06-05T20:47:14.860 回答