Detekt in 2018

Alfredo Cerezo Luna
2 min readSep 5, 2018

--

Back in 2017, I wrote about how to configure Detekt for an Android multi-module project:

And now is time to update this article with the latest state of this static analysis tool.
This process is much easier now, and it only takes a few steps to have it checking your Kotlin modules selectively.

First: Add the classpath dependency to your existing dependencies in your root build.gradle.

Second: Create your Detekt rules configuration file.

In this file you can specify which rules should Detekt follow to check your code.
This file is very easy to create, just take a look at the official documentation and follow the instructions.

You can name this file detekt-ruleset.yml, and you can put it in the project root path, or create a quality folder just to store it with other configuration files for other tools.

Third: Create your Detekt configuration file.

You can call it detekt.gradle , and it would be better if you put this file also in the same location that you put the rules file before.

Keep in mind that all values can be different from the values in the snippet below since it depends on the folders structure of your project.

The field input indicates the directory where your Kotlin files reside.
The field config indicates the location of the rules files from the second step.
The filed filters indicate which files should be skipped during the scan, for instance, test files.
The field output indicates where we want to store the report file that contains all the issues that Detekt founds.
The field parallel is an optimisation for performance if your project contains more than 200 files.

Don’t forget the first line: apply plugin: “io.gitlab.arturbosch.detekt”

Last: apply the plugin to (every) kotlin module.

You just need to add

apply from: '../quality/detekt.gradle'

at the top of your module’s build.gradle, keep in mind that the path might vary depending on where you define your configuration file during the second or third step.

Repeat this step in all modules that we want to get checked by the tool.

Done!

Just run this in your terminal:

./gradlew detektCheck

Thanks for reading and happy testing!

References

Detekt — https://arturbosch.github.io/detekt/index.html

--

--