Kotlinter Gradle
Gradle plugin for linting and formatting Kotlin source files using the awesome ktlint engine.
Installation
Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jmailen.kotlinter
Single module
Kotlin
plugins {
id("org.jmailen.kotlinter") version "1.21.0"
}
Groovy
plugins {
id "org.jmailen.kotlinter" version "1.21.0"
}
Multi-module and Android
Kotlin
Root `build.gradle.kts`buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:1.21.0")
}
}
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:1.21.0"
}
}
Each module build.gradle
with Kotlin source
apply plugin: "org.jmailen.kotlinter"
Compatibility
Kotlinter 1.21.0 and later compatible with Kotlin Gradle plugins 1.3.20+ and Java 11/10/9/8.
Kotlinter 1.12.0 and later compatible with Kotlin Gradle plugins 1.2.41+ and Java 9/8.
Kotlinter 1.8.0 and later compatible with Kotlin Gradle plugins 1.2.21+ and Java 9/8.
Kotlinter 1.7.0 and later compatible with Kotlin Gradle plugins 1.2.20+
Kotlinter 1.4.0 and later compatible with Kotlin Gradle plugins 1.1.50+
Kotlinter 1.2.0 and later compatible with Kotlin Gradle plugins 1.1.3+
Kotlinter 1.1.0 and earlier compatible with Kotlin Gradle plugins 1.1 - 1.1.2-5
Features
- Extends Kotlin JVM and Android projects with lint and format tasks for each
SourceSet
- Standalone
LintTask
andFormatTask
types for defining custom tasks - Incremental build support
.kt
and.kts
source support- Console output and configurable reporters
Tasks
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
.
Custom Tasks
If you haven't applied these plugins 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')
]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
}
Configuration
Options are configured in the kotlinter
extension. Defaults shown (you may omit the configuration block entirely if you want these values).
Kotlin
kotlinter {
ignoreFailures = false
indentSize = 4
continuationIndentSize = 4
reporter = arrayOf("checkstyle", "plain")
}
Groovy
kotlinter {
ignoreFailures = false
indentSize = 4
continuationIndentSize = 4
reporters = ['checkstyle', 'plain']
}
Options for reporters
: checkstyle, html, json, plain
The html reporter is provided by ktlint-html-reporter.
Reporters behave as described at: https://github.com/shyiko/ktlint
*Note: reporter
with a single value is deprecated but supported for backwards compatibility.
Customizing Tasks
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'
}
Custom ktlint version
If you need to use a different version of ktlint
you can override the dependency.
Kotlin
buildscript {
configurations.classpath
.resolutionStrategy.force("com.github.shyiko:ktlint:0.28.0")
}
Groovy
buildscript {
configurations.classpath {
resolutionStrategy { force 'com.github.shyiko:ktlint:0.28.0' }
}
}
Custom Rules
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'
}
}