PMD,checkstyle和findbugs android安装程序

我如何使用gradle的最新版本为Android项目设置PMD,Findbugs和Checkstyle静态代码分析工具? 我尝试了几件事,但我没有设法让他们工作。

谢谢

Solutions Collecting From Web of "PMD,checkstyle和findbugs android安装程序"

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:

 // 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 } } 

在这里你可以find插件的主页和源代码。

FindBugs的

// UPDATE //

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. } 

// UPDATE END //

我运行findbugs检查以下gradle脚本代码片段,你添加到你的模块的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