Read Time: 3 mins

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.

"Adding ktlint to your Kotlin project" Click To Tweet

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


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTubeInstagram and Facebook.

Check Out My YouTube Channel

5 thoughts on “Formatting Kotlin Code with ktlint – Adding ktlint to Your Kotlin Project

  1. Thank you.I tried this solution but ./gradlew ktlintCheck
    command in not looking into subprojects. It is showing format errors only for root project files.

    1. Interesting. It’s possible that the setup instructions have changed since this was originally written. Did you have errors in the root project? I think I’ve seen before where if checks for one module failed, the other modules wouldn’t be checked. Maybe something like that is going on 🤔

  2. Thank you for the reply, yes you are correct, root project issues were stopping it to go into sub projects. I used ./gradlew —continue ktlintCheck and it did the trick.My problem now is I am not able to format using IntelliJ reformat option. I applied ktlint-idea plugin but code is not getting reformatted using hot keys. Please help. Thank you.

Leave a Reply

Back to Top