属性 checkstyle 配置文件中不存在错误

Property does not exist error in checkstyle configuration file

我使用 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

我哪里做错了或者我对文档有什么误解?

在撰写本文时,Gradle uses Checkstyle 5.9 by default. The allowSingleLineStatement property was only added in Checkstyle 6.5。因此,您应该能够通过使用更新的 Checkstyle 版本来实现这一点:

checkstyle {
    configFile = file("${project.rootDir}/config/checkstyle.xml")
    toolVersion = '6.7'
}

不幸的是,Checkstyle 文档没有版本控制,因此该网站始终只有最新的文档,这使得很难弄清这样的内容。