Add Ktlint to Android app

Andres Sandoval
2 min readOct 6, 2019

What is Ktlint? is a static code analysis tool maintain by Pinterest. Linter and formatter for Kotlin code, more details here.

  1. Create a new file ktlint.gradle, and add below code:
repositories {
jcenter()
}

configurations {
ktlint
}

dependencies {
ktlint "com.pinterest:ktlint:0.36.0"
}

task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "src/**/*.kt"
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "-F", "src/**/*.kt"
}

2. Add below “apply” to your Gradle scripts modules:

apply from: "../ktlint.gradle"

3. Sync project

4. Run below Gradle tasks

The below task runs Kotlin lint check and outputs Kotlin file errors.

./gradlew base:ktlint

The below task automatically formats all Kotlin files and fixes some of the lint errors outputted from the above Gradle task.

./gradlew base:ktlintFormat

How to run Ktlint on Android project:

When you run (./gradlew base:ktlint) it will output Kotlin code/file errors.

--

--