Detekt for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Detekt for Kotlin: Complete Guide with Examples

\n\n

1. What It Is and Why It's Needed

\n\n

Detekt is a powerful static code analysis tool for the Kotlin language, created by Arturbosch and maintained by the community. It is designed to automatically detect potential problems, code smells, violations of style conventions, and vulnerabilities in Kotlin projects. Detekt works at the level of the abstract syntax tree (AST), allowing it to analyze code more deeply than simple text linters. The tool integrates with build systems such as Gradle, Maven, and other CI/CD tools, making it indispensable for maintaining code quality in team development.

\n\n

Why is Detekt needed? First, it automates code checking for compliance with Kotlin Coding Conventions, which is especially important in large projects with multiple developers. Second, Detekt helps identify potential bugs at early stages: for example, unused variables, possible NullPointerExceptions, complex conditional constructs, and other issues that can lead to runtime errors. Third, the tool facilitates refactoring: it highlights code sections with high cyclomatic complexity, long functions, redundant Elvis operators, and other patterns that impair code readability and maintainability.

\n\n

Detekt provides over 150 built-in rules, divided into categories: potential bugs, code style, performance, complexity, exceptions, and others. The tool is fully customizable: you can enable/disable individual rules, change their threshold values, and even write your own rules in Kotlin. Detekt supports report generation in HTML, XML, SARIF, and text formats, allowing integration with any code analysis systems.

\n\n

2. Installation

\n\n

Detekt is installed as a plugin for the Gradle build system (recommended method) or as a standalone CLI tool. The exact installation commands are provided below.

\n\n

Installation via Gradle (Plugin)

\n

Add the following to the build.gradle.kts (or build.gradle) file:

\n\n
// build.gradle.kts\nplugins {\n    id("io.gitlab.arturbosch.detekt") version "1.23.6" // current version at the time of writing\n}\n\n// Detekt configuration (optional)\ndetekt {\n    buildUponDefaultConfig = true // use default rules + custom ones\n    allRules = false // do not enable all rules (only recommended ones)\n    config.setFrom("$projectDir/config/detekt/detekt.yml") // path to config\n    baseline = file("$projectDir/config/detekt/baseline.xml") // baseline for ignoring errors\n    reports {\n        html.required.set(true) // generate HTML report\n        xml.required.set(true) // generate XML report\n        txt.required.set(true) // generate text report\n    }\n}\n
\n\n

After adding the plugin, run the command:

\n
./gradlew detekt\n
\n\n

Installation via CLI (Homebrew for macOS/Linux)

\n
brew install detekt\n
\n\n

Installation via SDKMAN

\n
sdk install detekt\n
\n\n

Installation via JAR (universal method)

\n
curl -sSLO https://github.com/detekt/detekt/releases/download/v1.23.6/detekt-cli-1.23.6-all.jar\nalias detekt='java -jar detekt-cli-1.23.6-all.jar'\n
\n\n

3. Quick Start — Minimal Working Example

\n\n

Create a simple Kotlin file src/main/kotlin/example.kt:

\n\n
// example.kt\nfun main() {\n    val x = 1\n    val y = 2\n    if (x > 0) {\n        println("Positive")\n    } else {\n        println("Non-positive")\n    }\n    println("Sum: ${x + y}"                    

Recommendations