代码之家  ›  专栏  ›  技术社区  ›  Terry

Android的最小工作点错误设置

  •  7
  • Terry  · 技术社区  · 6 年前

    我试着跟着 official documentation 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项目最小工作设置的例子吗?

    1 回复  |  直到 6 年前
        1
  •  12
  •   ToYonos    6 年前

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

    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')
    }
    
        2
  •  8
  •   Mr-IDE    4 年前

    ToYonos answer后续(2018年10月9日);在Android Studio 3.4及更高版本中使用此选项:

    项目/构建.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" 
        }
    }
    

    项目/app/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 6.x: ./gradlew spotbugsDebug

    ./gradlew spotbugsMain

    使用它很重要 classes = files("$project.buildDir/intermediates/javac") "java.io.IOException: No files to analyze could be opened" --看到了吗 Findbugs fails with "java.io.IOException: No files to analyze could be opened"

    ignoreFailures = true 是可选的。当spotbug检测到一个代码警告时,默认情况下它将以 "BUILD FAILED" 意味着gradle任务将以 "BUILD SUCCESSFUL"

    excludeFilter . 对于示例排除文件,请选中 here here (same as findbugs-exclude.xml)

    https://mikedemaso.com/tech/2020-06-10-spotbugs-gradle-plugin-android/