Painless Gradle plugin for linting and formatting Kotlin source files using the awesome ktlint engine.
It aims to be easy to set up with zero required configuration and behaves as you'd expect out of the box.
It's also fast because it integrates the ktlint engine directly with Gradle's incremental build and uses the Worker API to parallelize work.
Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jmailen.kotlinter
Kotlin
plugins {
id("org.jmailen.kotlinter") version "2.4.1"
}
Groovy
plugins {
id "org.jmailen.kotlinter" version "2.4.1"
}
Kotlin
Root `build.gradle.kts`buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:2.4.1")
}
}
Each module build.gradle.kts
with Kotlin source
apply(plugin = "org.jmailen.kotlinter")
Groovy
Root `build.gradle`buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:2.4.1"
}
}
Each module build.gradle
with Kotlin source
apply plugin: "org.jmailen.kotlinter"
Kotlinter is compatible with Kotlin Gradle plugins 1.3.30+ and Java 13/12/11/10/9/8.
- Supports Kotlin Gradle plugins: JVM and Android
- Supports
.kt
and.kts
files - Standalone
LintTask
andFormatTask
types for defining custom tasks - Incremental build support and fast parallelization with Gradle Worker API
- Configures from
.editorconfig
when available - Configurable reporters
If your project uses the JetBrains Kotlin JVM or Android Gradle plugins, standard tasks are created:
formatKotlin
: format Kotlin source code according to ktlint
rules or warn when auto-format not possible.
lintKotlin
: report Kotlin lint errors and by default fail the build.
Also check
becomes dependent on lintKotlin
.
Granular tasks exist for each source set in the project: formatKotlin
SourceSet
and lintKotlin
SourceSet
.
Kotlinter can install a hook to run pre-push (installKotlinterPrePushHook
). The hook runs lintKotlin
and, if there are errors, formatKotlin
and exits
non-zero leaving changed files to be committed.
You must apply the kotlinter plugin to your root project to make this task available.
Options are configured in the kotlinter
extension. Defaults shown (you may omit the configuration block entirely if you want these defaults).
Kotlin
kotlinter {
ignoreFailures = false
indentSize = 4
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
disabledRules = emptyArray<String>()
fileBatchSize = 30
}
Groovy
kotlinter {
ignoreFailures = false
indentSize = 4
reporters = ['checkstyle', 'plain']
experimentalRules = false
disabledRules = []
fileBatchSize = 30
}
Options for reporters
: checkstyle
, html
, json
, plain
Reporters behave as described at: https://github.com/pinterest/ktlint
The experimentalRules
property enables rules which are part of ktlint's experimental rule set.
The disabledRules
property can includes an array of rule ids you wish to disable. For example to allow wildcard imports:
disabledRules = ["no-wildcard-imports"]
You must prefix rule ids not part of the standard rule set with <rule-set-id>:<rule-id>
. For example experimental:annotation
.
The fileBatchSize
property configures the number of files that are processed in one Gradle Worker API call.
Kotlinter will configure itself using an .editorconfig
file if one is present in your root project directory.
If a non-empty disabledRules
value is specified in the kotlinter
extension, it will take precedence over any disabled_rules
in .editorconfig
.
See Ktlint editorconfig for supported values.
The formatKotlin
SourceSet
and lintKotlin
SourceSet
tasks inherit from SourceTask
so you can customize includes, excludes, and source.
Kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
tasks {
"lintKotlinMain"(LintTask::class) {
exclude("**/*Generated.kt")
}
}
Groovy
lintKotlinMain {
exclude '**/*Generated.kt'
}
If you aren't using autoconfiguration from a supported plugin or otherwise need to handle additional source code, you can create custom tasks:
Kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
val ktLint by tasks.creating(LintTask::class) {
group = "verification"
source(files("src"))
reports = mapOf(
"plain" to file("build/lint-report.txt"),
"json" to file("build/lint-report.json")
)
}
val ktFormat by tasks.creating(FormatTask::class) {
group = "formatting"
source(files("src"))
report = file("build/format-report.txt")
}
Groovy
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
task ktLint(type: LintTask, group: 'verification') {
source files('src')
reports = [
'plain': file('build/lint-report.txt'),
'json': file('build/lint-report.json')
]
disabledRules = ["import-ordering"]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
disabledRules = ["import-ordering"]
}
If you need to use a different version of ktlint
you can override the dependency.
Kotlin
buildscript {
configurations.classpath
.resolutionStrategy.force("com.github.pinterest:ktlint:0.36.0")
}
Groovy
buildscript {
configurations.classpath {
resolutionStrategy { force 'com.github.pinterest:ktlint:0.36.0' }
}
}
You can add custom ktlint RuleSets using the buildscript
classpath:
Kotlin
buildscript {
dependencies {
classpath(files("libs/my-custom-ktlint-rules.jar"))
classpath("org.other.ktlint:custom-rules:1.0")
}
}
Groovy
buildscript {
dependencies {
classpath files('libs/my-custom-ktlint-rules.jar')
classpath 'org.other.ktlint:custom-rules:1.0'
}
}