Ktlint static code analyser for your Kotlin code

Paul Stanescu
3 min readNov 11, 2018

Static code analyser?

First of all we have to understand what static code analyser is and what alternatives exist. So…a static code analyser is a method of debugging that is done by examining the code without executing the program. The process provides a feedback where you can find some suggestions about industry standards that you may want to follow or some possible bugs, critical errors or warnings. All of these are based on some rules that are predefined at the beginning by the tool, that you are using, but you can create or adjust these to have your own rules.

The alternative of a static code analyser is a dynamic analyser performed on programs while they are executing.

Advantages

The main advantage of using a code analyser is the fact that it can reveal some software quality issues during the development phase and not after the code is already in production. It also may have some suggestions about areas of code that needs re-factoring or simplifications.

Another benefit is to help training developers to produce high quality code as the more seniors colleagues. The process of searching possible bugs or/and errors is improved. It’s very helpful to identify issues in less time, so you can find them, repair them, saving a lot of time in the end.

Having high quality code will make the end user and the programmer happy!

Time to code

I told you a story about benefits to be able to understand the need and also the process but now let’s see how we can configure Ktlint within an Android project that was already created or even if is a new one.

In order to be more easy to track, share and follow the issues, I will also show you how you can configure the report. The report will be done into a HTML file. That means, if you are using a CI tool like Jenkins, you will be able to share results with other team members.

The work will be done in 3 steps as I will describe below. The only assumption that I will make is that you already have two gradle files, one in root of the project and another one within app module.

Step 1

On the build.gradle file, placed in the root of the project, you have to ensure that under the repositories you have defined jcenter(). If not, please define it.

Also, under this step, we should define also a variable for the ktlint version. It will be part of this gradle file as well. Then the file should look like this:

buildscript {
ext.kotlin_version = '1.3.0'
ext.ktlint_version = '0.2.0'

repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Step 2

In this step, the configuration will be done on the app build.gradle file. We have to add a custom configuration for Ktlint like:

configurations {
ktlint
}

Two more dependencies will be added, one for ktlint and one for the report.

ktlint "com.github.shyiko:ktlint:0.29.0"ktlint "me.cassiano:ktlint-html-reporter:$rootProject.ext.ktlint_version"

In order to be able to run the analyser new tasks will be added in this gradle file:

task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args "src/**/*.kt"
args "--reporter=html,artifact=me.cassiano:ktlint-html-reporter:$rootProject.ext.ktlint_version,output=${buildDir}/ktlint.html"
}
check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args "-F", "src/**/*.kt"
}

Step 3

In this last step we will test our work by running the analyser using the Terminal from Android Studio and the following command:

gradlew ktlint

This command will check the code style. It’s also bond to the gradlew check command.

To run the formatter, the command is: gradlew ktlintFormat

If the analyser found some errors, you will see a failed message on the terminal and the path to the report will be displayed. Otherwise, a success message will be displayed.

For more details, configurations or curiosities please access the Ktlint page

Just code it!

--

--