Formatting Kotlin Code with ktlint – Adding ktlint to Your Kotlin Project

In this post, we’ll introduce you to formatting Kotlin code with ktlint, and specifically focus on adding ktlint to your Kotlin project.

If you aren’t familiar, ktlint is an open-source linting tool for your Kotlin code which also includes a built-in code formatter.

Ktlint aims to take the debate out of Kotlin code formatting by following the standard Kotlin style guide provided by JetBrains.

Why use ktlint?

By allowing a tool to check your code’s formatting, you don’t have to do it. You can maintain consistent code formatting throughout your codebase with minimal effort. In some cases, ktlint can even reformat your code for you when there is an issue. This allows developers to focus their time and energy on the more interesting problems of at hand.

What does ktlint provide?

Ktlint provides two tools

  • a linter to check for formatting errors
  • a formatter to fix formatting errors

Adding ktlint to your project

How do you add ktlint to your kotlin project?

There are several options, but the easiest way is to make use of the ktlint-gradle plugin which provides out of the box Gradle tasks for ktlint’s tools

Adding ktlint to your Android project

  1. Adding the ktlint-gradle plugin

    Add the ktlint-gradle plugin to your root-level build.gradle file

  2. Apply the plugin to subprojects

    Apply the ktlint-gradle plugin to any Gradle modules that you would like to check

  3. Verify added Gradle tasks

    Check that ktlintCheck and ktlintFormat tasks have been added for your various build targets

Adding the ktlint-gradle plugin

If using a version of Gradle which supports the plugins DSL, you can add ktlint-gradle to your project with the following code:

// root-level build.gradle
plugins {
    id "org.jlleitschuh.gradle.ktlint" version "7.1.0"
}

In this case, the version is "7.1.0" and could be substituted for whatever the current version is.

If you can’t, or prefer not to, use the Gradle plugins DSL you could add the dependency like this:

// root-level build.gradle
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.jlleitschuh.gradle:ktlint-gradle:7.1.0"
  }
}

Apply the ktlint-gradle plugin to all subprojects

Next, you’ll want to apply the ktlint-gradle plugin to the various modules within your project. You can do this using the allProjects{} block in the root-level build.gradle file.

// root-level build.gradle
allprojects {
    ...
    apply plugin: "org.jlleitschuh.gradle.ktlint"
}

Verify added Gradle tasks

Finally, you’ll want to verfiy that the ktlint Gradle tasks are now available for use. You can do this in three ways

  1. run ./gradlew tasks from the command line and look for any ktlint tasks
  2. try to run ./gradlew ktlintCheck from the command line
  3. use the Gradle tool window in IntelliJ or Android Studio to see if the tasks are listed

Checking Kotlin code formatting with ktlint

To actually check your code’s formatting, run the following command from the command line:

./gradlew ktlintCheck
Output from running ktlintCheck with several errors

This will run through your project and report back any errors which are found using the default ktlint-gradle plugin configuration.


Reformatting Kotlin code with ktlint

To automatically fix any errors which are reported by ktlintCheck, you can run the following command from the command line:

./gradlew ktlintFormat
Output from running ktlintFormat with several non-fixable errors

This will attempt to auto-fix any errors, and will report back any issues that could not automatically be fixed.

Everything is formatted correctly

When everything is correctly formatted, you should see something like the following image when you run ktlintCheck.


Related Resources