What is Detekt and how to set it up to use in a project? Part 1.

Yauhen Slizh
Daresay
Published in
5 min readSep 19, 2024

Hello, developers! I want to share my experience working with Detekt. Over the course of several articles, I’ll try to explain how to use it in the project, write a custom rule, test it, and distribute it. I’d appreciate any comments about your experience using Detekt and feedback about my article. Ok, let’s go.

What is Detekt?

Detekt is a static analysis tools for Kotlin. It consists of third-party libraries and Gradle plugin. Detekt analyzes your codebase and generates a report of the issues it finds. It also has the ability to autocorrect certain issues. Detekt is applicable to any project that uses Kotlin, whether it’s Android, backend, or multiplatform.

Adding Detekt to your project helps identify problematic areas in your code. For instance, it can detect performance issues related to incorrect use of collections or complexity issues, such as deeply nested if-statements or large methods. Run Detekt locally helps you check the code yourself, but integration it with CI/CD pipelines bring more benefits and unlocks the full power of Detekt.

Detekt started as an open-source project and is driven by a large community. Detekt is not just about existing rules; it provides allows you to write your own rules and test them. As a result, you can create checks specifically tailored for a particular project or team. Detekt’s simple configuration mechanism allow you to customize existing rules and integrate community rule sets.

All of these features make Detekt one of the most popular lint checkers in Kotlin world.

How does Detekt work?

Basic schema of how Detekt works

A key aspect of Detekt’s operation is the generation and analysis of Abstract Syntax Tree(AST). An Abstract Syntax Tree is a tree representation of source code, where structural component are splitting into nodes that contain information about elements and represent the hierarchy of relationships. You can learn more about AST here.

Detekt doesn’t generate AST; it simply uses the one generated by the Kotlin compiler. For this reason, Detekt isn’t a third-party library; it is a Gradle plugin with close integration to the compiler.

Get started

Let me briefly explain how to add Detekt to your project. Of course, you can find more details in the official documentation, but I’ll just cover the basics.

First, you need to add the Gradle plugin to your project. You can find the plugin’s source code here.

plugins {
...
id("io.gitlab.arturbosch.detekt") version "1.23.1"
}

That’s it! Now, you can run the Gradle command:

./gradlew detekt

For finer configuration, you can customize several options in the detekt block of your build.gradl.kts file.

detekt {
allRules = true
buildUponDefaultConfig = true
}

You can find a full list of the options here.

If you need to add third-party rule sets, that aren’t included by default, you can simply add them as regular dependencies using the detektPlugins() function. It can point to a regular Maven artifact or a local path on your machine.

detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.1")
detektPlugins(files("path/to/custome_rules/build/libs/detekt-rules.jar"))

Best practices

How you can harness the power of Detekt is something I covered in the post How to make Detekt more efficient.

Detekt is useful not only when you run it on your local machine, but also when you set it up as part of your CI/CD pipeline. For instance, running Detekt on Pull Requests helps prevent merge code with potential issues and reduces the time reviewers spend pointing out the same problems.

You can see an example in the open-source project GitWatch, where the pipeline runs on each Pull Request. There’s nothing special about it — it just runs the same shell command you’d use locally:

- name: Run detekt checks
run: ./gradlew detektMain

If your code has any issues, the pipeline will fail.

Marketplace for Rules

One of the best things about Detekt is its flexibility and customizability. In its default configuration, Detekt has more than 280 rules that cover various aspects of coding, which are categorized into code smells, complexity, style, potential bugs, performance, documentation, and best practices. Detekt is powerful by default, but you can extend it by adding third-party rule sets or creating your own.

To simplify the process of sharing new rules, a marketplace page was created. This page lists the available public rule sets, with a short description, a list of rules, and a link to the repository. If you want to add something to your project, you can do so by adding it as a dependency using detektPlugins(...). If you don't want to add all the rules but only specific ones, you should include them in your detekt.yml configuration file. In this example, only one rule DoNotSplitByRegex, from the entire set will be applied:

FaireRuleSet:  
active: true
AlwaysUseIsTrueOrIsFalse:
active: false
DoNotAccessVisibleForTesting:
active: false
DoNotSplitByRegex:
active: true

Community

One of the Detekt’s strength is its vibrant community and large number of contributors. To date, over 280 developers have contributed to Detekt, and the activity continues. This diagram below reflects the ongoing development process.

Diagram of contributions to the Detekt repository

Detekt has excellent documentation that covers all aspects of its functionality. There are also active communication channels such as GitHub issues and Slack channel in kotlinlang.slack.com.

Conclusion

I like Detekt because it doesn’t add a burden to the project like a regular third-party library that requires integration and support. It adds value by making your code more stable and clean.
In this section, I provided a brief overview of Detekt. Next, I’ll show you how to write your own rule to meet your specific needs.

I’d be happy to receive your feedback on this article. Feel free to write your comments here, share it with friends, or simply like it. I enjoy sharing my knowledge; you can find more useful content on my LinkedIn page, on X, in Medium or Mastodon.

If you’d like to buy me a coffee, you can do so through the service.

Thank you for your time, and see you in the next post!

This article was written with the support of Daresay by Knightec. Daresay is a Swedish digital consulting company that provides high-quality IT solutions, including mobile, web, and backend development. It has strong expertise in design and business transformation. Feel free to contact Daresay to find a reliable partner for your business.

--

--

Yauhen Slizh
Daresay
Writer for

Android/Flutter/KMM developer, writer and speaker. I explore this world and share my insights. I enjoy new opportunities and solving complex problems.