Detekt for Kotlin: Complete Guide with Examples
\n\n1. What It Is and Why It's Needed
\n\nDetekt 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\nWhy 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\nDetekt 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\n2. Installation
\n\nDetekt 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\nInstallation via Gradle (Plugin)
\nAdd the following to the build.gradle.kts (or build.gradle) file:
// 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\nAfter adding the plugin, run the command:
\n./gradlew detekt\n\n\nInstallation via CLI (Homebrew for macOS/Linux)
\nbrew install detekt\n\n\nInstallation via SDKMAN
\nsdk install detekt\n\n\nInstallation via JAR (universal method)
\ncurl -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\n3. Quick Start — Minimal Working Example
\n\nCreate a simple Kotlin file src/main/kotlin/example.kt:
// 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}"