PMD、Checkstyle和FindBugs Android设置

9
如何在最新版本的gradle中为Android项目设置PMD、Findbugs和Checkstyle静态代码分析工具?我尝试了几种方法,但无法使它们正常工作。谢谢。

你有没有找到解决方案? - Nevin Raj Victor
这是我的试验 https://github.com/onmyway133/blog/issues/257 - onmyway133
1个回答

7

Checkstyle / PMD

有一个很不错的插件可以用于 Checkstyle 和 PMD。只需添加:

buildscript {
    repositories {
        // ...
    }
    dependencies {
        // ...
        classpath 'com.android.tools.build:gradle:1.2.3'

        // Enables checkStyle and pmd gradle support for android modules
        classpath 'com.noveogroup.android:check:1.1.2'
    }
}

将以下内容添加到全局 gradle.build 文件中,并在模块中使用它:

apply plugin: 'com.noveogroup.android.check'

check {
    abortOnError false
    checkstyle {
        config "$rootProject.rootDir/path/to/your/checkstyle.xml"
    }
    pmd {
        config "$rootProject.rootDir/path/tp/your/pmd-ruleset.xml"
    }
}

或任何这些配置:

// configuration is optional
check {
    // skip source code checking or not, false by default
    skip true/false
    // fails build if code style violation is found, false by default
    abortOnError true/false

    checkstyle {
        // skip Checkstyle, false by deafult
        skip true/false
        // fails build if Checkstyle rule violation is found, false by default
        abortOnError true/false
        // configuration file
        config project.file('path/to/checkstyle.xml')
        // configuration resource
        // see http://gradle.org/docs/2.2/release-notes#sharing-configuration-files-across-builds
        config resources.text.fromFile(someTask)
        // configuration path
        config 'path/to/checkstyle.xml'
        // predefined configurations: easy and hard
        config easy()
        config hard()
        // plugin find configuration file in project.file('config/checkstyle.xml') by default
        // if there are no configuration file, easy() configuration will be used
    }

    pmd {
        // the same configuration as for Checkstyle
        // plugin find configuration file in project.file('config/pmd.xml') by default
        // if there are no configuration file, easy() configuration will be used
    }
}

这里可以找到该插件的主页和源代码。

Findbugs

// 更新 //

noveogroup 最新的插件(1.2.3)现在也支持 findbugs。因此,您可以像定制 checkstyle 或 pmd 一样定制它:

// configuration of FindBugs checker
findbugs {
  // the same configuration as for Checkstyle

  // by default plugin finds configuration file in <rootProject>/config/findbugs.xml,
  // after that in <project>/config/findbugs.xml and if there are no configuration
  // file, easy() configuration will be used.
}

// 更新结束 //
我使用以下Gradle脚本片段运行findbugs检查,您需要将其添加到您的模块的build.gradle:
apply plugin: 'findbugs'

task customFindbugs(type: FindBugs) {
    ignoreFailures = true
    effort = "default"
    reportLevel = "medium"
    classes = files("$project.buildDir/intermediates/classes")
    excludeFilter = file("$rootProject.rootDir/config/findbugs/exclude.xml")

    source = fileTree('src/main/java/')
    classpath = files()
    reports {
        xml.enabled = false
        xml.withMessages = true
        html.enabled = !xml.isEnabled()
        xml.destination "$project.buildDir/outputs/findbugs/findbugs-output.xml"
        html.destination "$project.buildDir/outputs/findbugs/findbugs-output.html"
    }
}
// UPDATE: renamed the task to customFindbugs and made it automatically be called when build is called
build.dependsOn customFindbugs

我的exclude.xml文件如下所示:

<FindBugsFilter>
    <Match>
       <Class name="~.*R\$.*"/>
    </Match>
    <Match>
        <Class name="~.*Manifest\$.*"/>
    </Match>
    <Match>
        <Class name="~.*_"/>
    </Match>
</FindBugsFilter>

最后一个检查用于省略由AndroidAnnotations生成的类,您很可能不会使用此检查...

之后,我可以通过运行任务来完成

./gradlew customFindbugs
// or it is also included in the build task like the checks, too
./gradlew build

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接