Improving Kotlin code quality

Helder Pinhal
Helder Pinhal
Oct 22 2021
Posted in Engineering & Technology

Integrating ktlint & detekt with minimal effort

Improving Kotlin code quality

In today's brief post I want to go over setting up ktlint and detekt to helps us, developers, produce better, more elegant code. We are often a lazy breed and overlook the quality of the code we're writing. Nowadays, we have several tools that can help with that task, with us having to do very little to set it up.

ktlint

ktlint is a tool that analyses source code, looks for programming errors, bugs and style errors. It comes with a code formatter out of the box to help automate and fix certain errors that don't require human intervention. To integrate ktlint, we'll take a look at another library that helps a lot with the task at hand. ktlint doesn't include a Gradle plugin which means we would have to take additional steps, like setting up Gradle tasks, to seamlessly integrate the tool.

Instead, ktlint-gradle solves this challenge for us. The first and only step is to include the plugin in your app's build.gradle file.

plugins {
    id('org.jlleitschuh.gradle.ktlint') version '<current_version>'
}

Sync your project and the ktlint tasks should become visible in the formatting and verification sections.

The ktlintCheck task will analyse your source code, point out the errors it finds and which of those are automatically fixable. If you want to automatically fix what's possible, the ktlintFormat does just that.

ktlint might prove to be a bit strict, but as they mention in their documentation, it follows Kotlin's official coding conventions which may prove to be a positive thing to keep everyone's coding style in check.

detekt

While some of detekt's features overlap with ktlint when it comes to code formatting, its focus is on code analysis — code complexity, code smells, etc. Now the good part is we can run both tools without them conflicting with each other.

Just like ktlint, all that we need is to do is set up the plugin.

plugins {
    id('io.gitlab.arturbosch.detekt') version '<current_version>'
}

After syncing the project, the detekt should become available in the verification section.

By default, detekt runs just fine with the standard rules. However, if you want to modify something or loosen the rules, you can run the detektGenerateConfig task which will place a detekt.yml configuration file under $projectDir/config/detekt/detekt.yml. For the tool to respect the changes you've made, you need to adjust its settings by adding the following to your build.gradle file:

detekt {
    config = files('config/detekt/detekt.yml')
}

Lastly, for existing projects, enforcing all rules at once might prove quite a challenge. For cases like this, detekt is able to create a baseline for the project, ignoring the existing errors but enforcing the rules for new code. If your project is quite large, this can be a good approach to incrementally adopt detekt. In order to enable this baseline, you need to run the detektBaseline which will generate a detekt-baseline.xml file for each module and handle that in the following checks.

Conclusion

To sum up, we were able to add both a code formatter and a static analyser with minimal effort that will keep us in check when it comes to the code we produce.

As always, we hope you liked this article and if you have anything to add, we are available via our Support Channel.

Keep up-to-date with the latest news