Kotlin 静态代码检查(ktlint)
Plugins
ktlint
主页:https://ktlint.github.io/
ktlint
GitHub:https://github.com/pinterest/ktlint
ktlint
插件:https://plugins.jetbrains.com/plugin/15057-ktlint-unofficial-
Without Plugins
Add the following snippet to build.gradle
apply plugin: "java"
repositories {
jcenter()
}
configurations {
ktlint
}
dependencies {
ktlint "com.pinterest:ktlint:0.41.0"
// additional 3rd party ruleset(s) can be specified here
// just add them to the classpath (ktlint 'groupId:artifactId:version') and
// ktlint will pick them up
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
main = "com.pinterest.ktlint.Main"
classpath = configurations.ktlint
args "src/**/*.kt"
// to generate report in checkstyle format prepend following args:
// "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
// see https://github.com/pinterest/ktlint#usage for more
}
check.dependsOn ktlint
task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
main = "com.pinterest.ktlint.Main"
classpath = configurations.ktlint
args "-F", "src/**/*.kt"
}
Usage
# check code style (it's also bound to "gradle check")
$ gradle ktlint
src/main/kotlin/Main.kt:10:10: Unused import
# fix code style deviations (runs built-in formatter)
$ gradle ktlintFormat