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.

Common errors below:

  1. File must end with a newline (\n)
  2. Wildcard import (cannot be auto-corrected)
  3. Needless blank line(s)
  4. Imports must be ordered in lexicographic order without any empty lines in-between
  5. Unexpected indentation

Most errors can be automatically fixed when you run (./gradlew base:ktlintFormat)

Add Ktlint ignore rule:

  1. Create a new file .editorconfig
  2. Add ignore rule (example below ignores no-wildcard-imports)
[*.{kt,kts}]
# possible values: number (e.g. 2), "unset" (makes ktlint ignore indentation completely)
indent_size=4
# true (recommended) / false
insert_final_newline=true
# possible values: number (e.g. 120) (package name, imports & comments are ignored), "off"
# it's automatically set to 100 on `ktlint --android ...` (per Android Kotlin Style Guide)
max_line_length=off

# Comma-separated list of rules to disable (Since 0.34.0)
# Note that rules in any ruleset other than the standard ruleset will need to be prefixed
# by the ruleset identifier.
disabled_rules=no-wildcard-imports,experimental:annotation,my-custom-ruleset:my-custom-rule

3. Run Gradle task (./gradlew base:ktlint) now it ignored the rules.

After configuring Ktlint in your project, you can add the Ktlint Gradle task to your CI.

File formatter on Android Studio:

To enable file formatter on Android Studio automatically. go to AS -> Preferences -> Editor -> Inspections -> check “File is not formatted according to project” -> Apply -> OK

this will result in warning, which is very easy to fix.

Thanks for reading! let me know if you have any questions.

  • Andres

software engineer, read, swim, travel. apps: https://goo.gl/rDuuQw https://goo.gl/15GvfE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store