我使用Android Studio创建了一个Android项目。在应用的build.gradle
添加:
apply from: '../config/quality.gradle'
然后我创建了config
目录,其中包含两个文件:quality.gradle
,如:
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/config/checkstyle.xml")
source 'src'
include '**/*.java'
exclude '**/gen/**'
classpath = files()
}
checkstyle.xml
喜欢:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="NeedBraces">
<property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/>
<property name="allowSingleLineStatement" value="true"/>
</module>
</module>
</module>
正在运行gradle checkstyle
会出现以下错误:
Executing external task 'checkstyle'...
:app:checkstyle FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkstyle'.
> Unable to create a Checker: cannot initialize module TreeWalker - Property 'allowSingleLineStatement' in module NeedBraces does not exist, please check the documentation
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
如果我删除行:
<property name="allowSingleLineStatement" value="true"/>
它有效。但阅读documentation,第一版也应该有用。
它类似于:
<module name="EmptyCatchBlock">
<property name="exceptionVariableName" value="expected|ignore"/>
</module>
抛出了我:
* What went wrong:
Execution failed for task ':app:checkstyle'.
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock
我做错了什么或者我以何种方式理解文档?
答案 0 :(得分:18)
在撰写本文时,默认情况下为Gradle uses Checkstyle 5.9。 allowSingleLineStatement
属性仅为added in Checkstyle 6.5。所以,你应该能够通过使用更新的Checkstyle版本来实现这个目的:
checkstyle {
configFile = file("${project.rootDir}/config/checkstyle.xml")
toolVersion = '6.7'
}
不幸的是,Checkstyle文档没有版本化,所以网站总是只有最新的文档,这使得很难找到这样的东西。