天天看點

CheckStyle出現Unable to create a Checker: configLocation問題CheckStyle出現Unable to create a Checker: configLocation問題

CheckStyle出現Unable to create a Checker: configLocation問題

最近使用了Android Studio 3.0版本,同時Gradle版本由3.3更新到了3.5版本。

突然發現原先項目中靜态代碼分析工具——CheckStyle不能使用了,出現類似如下的錯誤。

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:checkstyle'.
> Unable to create Root Module: configLocation {xxxx/xxxx/checkstyle/checkstyle.xml}, classpath {null}.
           

經過查找資料獲得,大緻是這樣的,Gradle3.5版本中預設使用的是CheckStyle7.2版本,3.3版本中使用的是5.9版本,有相容性錯誤。

原文是這樣的:

The specific problem here is that OperatorWrap did not support the METHOD_REF token until Checkstyle . Technically this configuration file is invalid unless you specify Checkstyle version  or higher.

With Gradle  (default Checkstyle ), that configuration was still not correct, but you did not receive an error due to looser validation (in Checkstyle, not Gradle). That version only failed on tokens that were completely unknown, not those that were only valid for other rules.
           

是以在官方暫時沒有解決方案的情況,可以在Gradle中指定CheckStyle的版本。

如下所示

apply plugin: 'checkstyle'

checkstyle {
    toolVersion = '5.9'
}

task checkstyle(type: Checkstyle) {
    configFile file("checkstyle.xml")

    ignoreFailures false
    showViolations true

    source 'src'
    include '**/*.java'
    exclude '**/gen/**', '**/test/**', '**/build/**'

    classpath = files()
}
           

官方位址