Android Studio 的最小工作 SpotBugs 设置 [英] Minimal working SpotBugs setup for Android Studio

查看:62
本文介绍了Android Studio 的最小工作 SpotBugs 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 Android 设置 SpotBugs?

我尝试遵循 官方文档gradle 插件,但是Android的设置不完整且混乱,无法正常工作.>

我尝试了以下设置.

build.gradle(项目):

buildscript {存储库{//...行家{网址https://plugins.gradle.org/m2/"}}依赖{//...类路径gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.4"}}

build.gradle(应用):

//...应用插件:com.github.spotbugs"安卓 {//...源集{主要的 {java.srcDirs = ['src/main/java']}}}//...臭虫{工具版本 = "3.1.3"忽略失败 = 真reportDir = file("$project.buildDir/findbugsReports")努力=最大"报告级别 = "高"}任务.withType(com.github.spotbugs.SpotBugsTask){//我需要在这里做什么?}

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

您能否向我展示一个 Android 项目的最小工作设置示例?

解决方案

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

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

android {//...}源集{主要的 {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) {取决于组装"classes = files("$projectDir.absolutePath/build/intermediates/classes/debug")source = fileTree('src/main/java')}

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?

解决方案

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

这篇关于Android Studio 的最小工作 SpotBugs 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆