Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property does not exist error in checkstyle configuration file

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?

like image 933
Birei Avatar asked Jun 23 '15 11:06

Birei


People also ask

How do I activate Checkstyle?

To enable the Checkstyle plugin for a particular project: Right-click on the project. Click on Checkstyle and Activate Checkstyle . (Or, if there is no Checkstyle entry, click on Properties and then select “Checkstyle”, check “Checkstyle active…” and click on OK .)

How do I check my Checkstyle violations?

To view to violation report, go to Window -> Show View -> Other, and search for Checkstyle. Options for Violations and Violations Chart should be displayed.


1 Answers

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.

like image 180
barfuin Avatar answered Oct 04 '22 21:10

barfuin