Ensuring code quality: Using Ktlint in your Kotlin project

In this blog post, I would be talking about the following:

  • What is Ktlint?
  • Why Ktlint?
  • Setting up Ktlint
  • Using Ktlint
  • Specifying Ktlint rules
  • Automating Ktlint check?

So let’s get started πŸ‘‡πŸ½

What is Ktlint?

Ktlint is a static code analysis tool maintained by Pinterest. According to the documentation, it is an anti-bikeshedding Kotlin linter with built-in formatter.

Why Ktlint?

Kotlin has a coding style guide, available on kotlinlang.org and Android Kotlin Style Guide. You’d most likely agree with me that it is not very easy to memorize all these coding styles, and even if you would, it could take a long time of use and some getting used to. How about having a tool that could help you check your code against these Kotlin coding styles and even help with formatting your code? Yeah, that tool is Ktlint. Ktlint checks your code against the official coding style. It also has a built-in formatter, so that you wouldn’t have to fix all style violations by hand.

Setting up Ktlint

Setting up Ktlint in your android studio project is easy, especially when a gradle plugin is used (which is recommended). I would be making use of this ktlint gradle plugin, which I would recommend as well. The gradle plugin automatically creates convenient gradle tasks in your android project that perform ktlint check and code formatting.

I have a simple sample android project set up, which is used in this blog post. You can clone and check it out as you read or after reading.

Adding the plugin is as simple as adding the following code snippet to your build.gradle plugins block.

plugins {
  id "org.jlleitschuh.gradle.ktlint" version "9.4.1"
}

However if your project is not using the new Plugin API, then add the plugin to your build.gradle file as follows

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.jlleitschuh.gradle:ktlint-gradle:<current_version>"
  }
}

apply plugin: "org.jlleitschuh.gradle.ktlint"

That’s all. Sync the project.

Using Ktlint

Using Ktlint is simple, you simply have to run one or two commands and that’s it.

Using Terminal:

πŸ‘‰πŸ½ To ask Ktlint to check your code for violations, you simply run this command in your terminal:

 ./gradlew ktlintCheck

Running this command on my freshly created sample project, I could see the following issues below:

ktlint sample project – build.gradle app 2020 11 15 21 47 39

As you may notice from the screenshot, some of the issues Ktlint pointed out are; newline at the end of a file, imports not ordered in lexicographic order, wildcard imports, indentation, etc.

I can simply fix these issues to match what is expected and then re-run the command till I’m free of issues. Or I can ask Ktlint to help with formatting.

πŸ‘‰πŸ½ To ask Ktlint to help with auto-formatting and fixing your code violations, you simply run this command

 ./gradlew ktlintFormat

Running this command auto-formats all your kotlin code to match the style guide as expected.

ktlint sample project – ExampleUnitTest.kt ktlint sample project.app 2020 11 15 21 58 38

NB: Not all code violations can be automatically fixed by Ktlint, such violations would have β€œ(cannot be auto-corrected)” appended to its description, as you may notice in the screenshot above.

For such cases, you can simply locate the files and fix the issues within them manually. After doing that, you can simply run ktlint check all over again to confirm all issues are fixed. Here, I fix the issues and run ktlint check again and voila!, all issues fixed βœ….

ktlint sample project – ExampleInstrumentedTest.kt ktlint sample project.app 2020 11 15 22 03 10 1

Using Android Studio Gradle Tab:

Instead of going to the terminal to type and run the commands as described above, you can also make use of the gradle tab on the right hand side of your android studio.

In the gradle tab, under the Tasks section, select the verification drop-down, you’d see the ktlintCheck task, simply double click on it and the gradle task would run a ktlint check. You’d see the result displayed at the bottom of your android studio.

To perform Ktlint auto-formatting, simply select the formatting drop-down from the gradle tab, you’d see the ktlintFormat task, double click it and you’d see the task run and display to you the result at the bottom of your android studio.

ktlint sample project – build.gradle app 2020 11 15 21 52 48

Specifying Ktlint rules

Even though there are standard rules that are being followed by Ktlint, you can as well modify or specify rules that fit your taste. For example, you might prefer an indentation size of 2, compared to the default 4. Or maybe you prefer to have no newline at the end of your file. You can also disable some rules completely, which makes Ktlint ignore them.

To do all these, simply create a .editorconfig file in your project directory. In the file, you specify these rules and Ktlint will consider these rules when running a check.

For my sample project, I’m going to simply specify that Ktlint should ignore the no-wildcard-imports rule. See a list of possible rules you can modify/specify here.

[*.{kt,kts}]
disabled_rules=no-wildcard-imports

Automating Ktlint check?

Yeah, you can automate your Ktlint check. You can save yourself the stress of having to manually run checks by making use of Git pre-commit/pre-push hook. It can also help you ensure that your code goes through a Ktlint check before being committed or pushed, especially when working in a team. Watch out for my next blog post on how to go about this.


And that’s it! Easy-peasy! We’ve gone through what Ktlint is, why Ktlint is great and how to set up and use it. It is one of those things that could make a difference when you use it in your android project. Your code looks much cleaner and easy to work with. Do try it out.

You can also check out the sample android project for this blog post. Also, feel free to buzz me if you need some help or need to discuss anything ✌🏽