Google checkstyle 配置在 Android 项目上失败

Google checkstyle configuration is failing on an Android project

我正在尝试在 Android 项目中使用 Checkstyle。我认为 Google java 编码约定很好用。解析此文件时出现错误:

https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

我在堆栈溢出时查过这个错误,之前出现过一次,

似乎错误是使用 gradle 时旧版本的 checkstyle 被拉下了。所以我的问题是,是否只有一个旧的 checkstyle 配置文件我可以使用,它是为 gradle 下拉的当前版本的 checkstyle 制作的?我是否应该为 Android 项目使用 Google 的 java 指南?有更好的默认文件吗?正如其他 SO 问题的答案中所述,我真的不想将新版本的 checkstyle 导入到我的项目中。

您应该始终指定要使用的 Checkstyle 的确切版本。 Checkstyle 人员经常进行重大更改,除非您设置特定的固定版本,否则这些更改会使您的构建失败。例如:

checkstyle {
    configFile file('downloaded_google_checks.xml')
    toolVersion '6.9'    // set Checkstyle version here
    showViolations = true
}

然后,您还可以使用适合您的版本的配置文件,例如:https://github.com/checkstyle/checkstyle/blob/checkstyle-6.9/src/main/resources/google_checks.xml。请注意 URL 中的版本号 - 这可以调整为您在 Gradle 配置中选择的版本。

不要使用 master 分支的最新配置文件,因为这与 master 分支(尚未发布)上的当前代码匹配。

以上内容允许您根据需要调整配置。如果你确定你想要原始规则文件,你也可以按照@BenMane 评论中的建议,即直接引用捆绑的google_checks.xml。