gpt4 book ai didi

android - 将 SpotBugs 添加到我的项目中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:42 27 4
gpt4 key购买 nike

我一直在努力添加 SpotBugs到我目前正在处理的android项目。我设法让它工作,但我对它的设置方式并不过分兴奋。目前,配置位于我的 app/build.gradle 文件中,这使得该文件不易管理。

我想知道是否有 SpotBugs/Gradle 专家知道将配置拉出到单独文件中的方法。

这是我的 app/build.gradle(删除了样板):

buildscript {
repositories {
...
}

dependencies {
classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
classpath 'io.fabric.tools:gradle:1.25.4'
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
}
}

plugins {
id 'com.gladed.androidgitversion' version '0.4.3'
id "com.github.spotbugs" version "1.6.2"
}

...
apply plugin: 'com.github.spotbugs'
apply from: '../config/quality/quality.gradle'
apply from: '../app/jacoco.gradle'
apply from: '../app/ktlint.gradle'
apply from: '../app/androidgit.gradle'

...

spotbugs {
toolVersion = '3.1.3'
ignoreFailures = false

effort = "min"
// This selects what level of bugs to report: low means low priority issues will be reported
// (in addition to medium+high), which corresponds to warning about everything.
// TODO: boost this to low once low priority issues are fixed.
reportLevel = "medium"

excludeFilter = new File("$project.rootDir/config/quality/spotbugs/android-exclude-filter.xml")
}

task spotbugs(type: com.github.spotbugs.SpotBugsTask, dependsOn: 'assemble', group: 'verification') {
classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

source = fileTree('src/main/java')


// Only one report format is supported. Html is easier to read, so let's use that
// (xml is the one that's enabled by default).
reports {
xml.enabled = false
html.enabled = true
}

classpath = files()
}

编辑

每当我尝试将 SpotBugs 从我的 app/build.gradle 中分离出来时,我都会遇到以下错误:

无法为类型为 org.gradle.api.Project 的项目“:app”获取未知属性“SpotBugsTask”。

这是我的 gradle 文件:

apply plugin: 'com.github.spotbugs'

dependencies {
checkstyle 'com.puppycrawl.tools:checkstyle:8.11'
spotbugs "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.2"
// spotbugs configurations.spotbugsPlugins.dependencies
// spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0'
}

def qualityConfigDir = "$project.rootDir/config/quality";
def reportsDir = "$project.buildDir/reports"

check.dependsOn 'checkstyle'

task checkstyle(type: Checkstyle, group: 'Verification', description: 'Runs code style checks') {
configFile file("$qualityConfigDir/checkstyle/checkstyle-config.xml")
source 'src/main/java'
include '**/*.java'
exclude '**/model/**'
exclude '**/AppLogger.java'
reports {
xml.enabled = true
xml {
destination file("$reportsDir/checkstyle/checkstyle.xml")
}
}

classpath = files()
}

spotbugs {
toolVersion = '3.1.3'
ignoreFailures = false

effort = "min"
// This selects what level of bugs to report: low means low priority issues will be reported
// (in addition to medium+high), which corresponds to warning about everything.
// TODO: boost this to low once low priority issues are fixed.
reportLevel = "medium"

excludeFilter = new File("$project.rootDir/config/quality/spotbugs/android-exclude-filter.xml")
}

task spotbugs(type: SpotBugsTask, dependsOn: 'assemble', group: 'verification') {
classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

source = fileTree('src/main/java')


// Only one report format is supported. Html is easier to read, so let's use that
// (xml is the one that's enabled by default).
reports {
xml.enabled = false
html.enabled = true
}

classpath = files()
}

最佳答案

终于找到了解决办法。

我必须将以下内容添加到我在 app/build.gradle 文件中应用所有插件的部分:

project.extensions.extraProperties.set('SpotBugsTask', com.github.spotbugs.SpotBugsTask)

所以它最终看起来像这样:

buildscript {
repositories {
mavenCentral()
jcenter()
maven { url 'https://maven.fabric.io/public' }
}

dependencies {
classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
classpath 'io.fabric.tools:gradle:1.25.4'
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
}
}

plugins {
id 'com.gladed.androidgitversion' version '0.4.3'
id "com.github.spotbugs" version "1.6.2"
}

// Workaround to be able to access SpotBugsTask from external gradle script.
// More info: https://discuss.gradle.org/t/buildscript-dependencies-in-external-script/23243
project.extensions.extraProperties.set('SpotBugsTask', com.github.spotbugs.SpotBugsTask)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'org.jetbrains.dokka-android'
apply plugin: 'io.fabric'
apply plugin: 'spoon'
apply from: '../app/checkstyle.gradle'
apply from: '../app/jacoco.gradle'
apply from: '../app/ktlint.gradle'
apply from: '../app/androidgit.gradle'
apply from: '../app/spotbugs.gradle'

android {
...

我的 spotbugs.gradle 文件:

dependencies {
spotbugs configurations.spotbugsPlugins.dependencies
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0'
}

def qualityConfigDir = "$project.rootDir/config/quality"
def reportsDir = "$project.buildDir/reports"

spotbugs {
toolVersion = "$spotbugs_version"
ignoreFailures = false

effort = "min"
// This selects what level of bugs to report: low means low priority issues will be reported
// (in addition to medium+high), which corresponds to warning about everything.
// TODO: boost this to low once low priority issues are fixed.
reportLevel = "medium"

excludeFilter = new File("$qualityConfigDir/config/quality/spotbugs/android-exclude-filter.xml")
}

tasks.register("spotbugs", SpotBugsTask) {
dependsOn 'assemble'
group = "verification"
classes = files("$projectDir.absolutePath/build/intermediates/app_classes/debug")

source = fileTree('src/main/java')


// Only one report format is supported. Html is easier to read, so let's use that
// (xml is the one that's enabled by default).
reports {
xml.enabled = true
xml {
destination file("$reportsDir/spotbugs/spotbugs.xml")
}
html.enabled = true
}

classpath = files()
}

关于android - 将 SpotBugs 添加到我的项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com