首页 > 解决方案 > Android Studio 的最小工作 SpotBugs 设置

问题描述

如何为 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 项目的最小工作设置的例子吗?

标签: androidandroid-studiogradlespotbugs

解决方案


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

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')
}

推荐阅读