Property does not exist error in checkstyle configuration file

10.1k views Asked by At

I create an Android project using Android Studio. In the build.gradle of the app add:

apply from: '../config/quality.gradle'

Then I create the config dir with two files: quality.gradle like:

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
    configFile file("${project.rootDir}/config/checkstyle.xml")
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    classpath = files()
}

And checkstyle.xml like:

<?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>

Running gradle checkstyle gives me following error:

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

If I remove line:

<property name="allowSingleLineStatement" value="true"/>

it works. But reading the documentation, first version should work too.

It happens similar with:

<module name="EmptyCatchBlock">
    <property name="exceptionVariableName" value="expected|ignore"/>
</module>

that throws me:

* What went wrong:
Execution failed for task ':app:checkstyle'.
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock

What am I doing wrong or in what way am I missunderstanding the docs?

1

There are 1 answers

5
barfuin On BEST ANSWER

At the time of this writing, Gradle uses Checkstyle 5.9 by default. The allowSingleLineStatement property was only added in Checkstyle 6.5. So, you should be able to get this working by using a newer Checkstyle version like this:

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

Unfortunately, the Checkstyle documentation is not versioned, so the Website always has the latest docs only, which makes it hard to figure stuff like this out.