Thiago Lopes Silva
3 min readMay 18, 2020

Configuring and running Ktlin on Android Studio

Barcelona, Spain

Hello Folks!! The goal of this post is help you to configure and run locally the static code analysis called Ktlint on Android Studio.

This post has the premise that you are using Gradle and Kotlin as your primary programming language.

According to the official Ktlint’s website, it is an anti-bikeshedding Kotlin linter with a built-in formatted that contains the following features to help you to format and organize your code style.

Features

  • No configuration. Which means no decisions to make, nothing to argue about, and no special files to manage.
    While this might sound extreme, keep in mind that ktlint tries to capture (reflect) official code style from kotlinlang.org and Android Kotlin Style Guide.
  • Built-in formatter. So that you wouldn’t have to fix all style violations by hand.
  • Customizable output. plain, json and checkstyle reporters are available out-of-the-box. It's also easy to create your own.
  • A single executable jar with all dependencies included.

I got the above content from the official Ktlint website into features section .

Finally, If you need more information about the benefit of using Ktlint you should go to the official link and checks it out all greats features provided by it.

Contextualization

This post is going to be separated into three sections. The first one is going to lead you to configure the ktlint plugin into Android Studio, the second one to format your code and the third to run and found out issues reported by it.

Configuring the Ktlint

To achieve this goal let’s go use the plugin jlleitschuh/ktlint-gradle once it creates automatically all basic tasks to run Ktlint into Gradle.

First of all, you need to configure the classpath in top-level build.gradle file to use the plugin jlleitschuh/ktlint-gradle.

classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"

Don’t forget to check it out the latest version of plugin on the official website.

Then, still on the top-level build.gradle file you need to add the repository maven as a dependency of the project.

repositories {
...

maven {
url "https://plugins.gradle.org/m2/"
}
}

Next, still on the top-level build.gradle file you must apply this dependency to all modules into allProjects tag.

allprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint"
...
}

Finally, you must sync your project on Android Studio to get all new dependencies. To do it you can go to File->Sync project with gradle files on Android Studio or click on the sync shortcut button as will be shown in the next picture.

Automatically formatting the code by klint task

To achieve this goal you must run the task ktlintFormat on the terminal and check it out the results.

./gradlew ktlintFormat

I’d recommend you to run this task before go to the next section as it going to try to format your code using ktlint rules.

If you had any problems after ran this task, then you can use the parameter stacktrace to get more details about the error.

./gradlew ktlintFormat --stacktrace

Checking code issues by ktlint task

To check if exists any issue regarding ktlint, you must run the task ktlintCheck on your terminal.

./gradlew ktlintCheck

If you had any problems after ran this task, then you can use the parameter stacktrace to get more details about the error.

./gradlew ktlintCheck --stacktrace

That’s it, folks!! Thanks for your reading and I hope it helps you. See you in the next post.

Keep in touch and follow me on Twitter or Linkedin and don’t forget to clap if you enjoin it. :D