skip to content

detekt

detekt finds code smells and complexity problems, configured by a rules file and tamed with a baseline for existing violations. The baseline concept — freeze current debt, block new debt — is the part worth explaining.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

6

What is detekt and what does running the `detekt` Gradle task do for a Kotlin project?

level: juniorimportance: must knowfreq 62%

answer

  1. Static analysis = reads code, doesn't run it
  2. Gradle plugin id io.gitlab.arturbosch.detekt adds `detekt` task
  3. Parses .kt into PSI/AST, runs rule sets
  4. Reports: HTML/XML/SARIF/TXT under build/reports/detekt
  5. Smells + complexity (detekt) vs formatting (ktlint)

basics

~10 s

detekt is a static analysis tool for Kotlin. It scans your source code without running it and reports code smells, overly complex functions, and style problems so you can fix them.

solid answer

~40 s

detekt is a static code analyzer for Kotlin that inspects source files at compile-time (no execution) for code smells, complexity, naming, and potential bugs. You apply the `io.gitlab.arturbosch.detekt` Gradle plugin, which adds a `detekt` task. Running `./gradlew detekt` parses each `.kt` file into an AST/PSI tree, runs enabled rules, and emits findings as console output plus reports (HTML, XML/Checkstyle, SARIF, plain TXT) under `build/reports/detekt`. By default it fails the build when findings exceed the configured `maxIssues` threshold (or any if zero), making it a quality gate. Rules are organized into rule sets (complexity, style, potential-bugs, etc.) and configured via `detekt.yml`. It complements ktlint, which focuses purely on formatting, whereas detekt targets smells and complexity.

code

kotlin · 9 lines
kotlin
plugins {
    id("io.gitlab.arturbosch.detekt") version "1.23.7"
}

detekt {
    buildUponDefaultConfig = true
    config.setFrom(files("$rootDir/detekt.yml"))
}
// ./gradlew detekt  -> findings + build/reports/detekt/detekt.html

go deeper

for a junior

Knows detekt is a Kotlin static analyzer that flags smells/complexity and runs via a Gradle task.

for a middle

Can name rule sets and report formats, and explains how it acts as a build-failing quality gate.

for a senior

Distinguishes detekt from ktlint by responsibility, knows PSI-based analysis and threshold configuration.

for a principal

Frames detekt within an overall quality-gate strategy across modules/CI and weighs its value vs false positives.

## What detekt is **detekt** is a *static analysis* tool for Kotlin. "Static" means it analyzes your source code **without running it** — it reads the text, parses it into a syntax tree, and applies rules. Contrast this with *dynamic* analysis (running tests, profilers) which needs the program to execute. It finds **code smells** (patterns that hint at deeper problems, e.g. a function that's too long), **complexity** issues (deeply nested code, too many branches), naming violations, and likely bugs (e.g. an empty `catch` block). ## How you run it You apply the Gradle plugin: ```kotlin plugins { id("io.gitlab.arturbosch.detekt") version "1.23.7" } detekt { config.setFrom("$rootDir/detekt.yml") buildUponDefaultConfig = true } ``` This registers a `detekt` task. Running `./gradlew detekt`: 1. Collects `.kt` source files. 2. Parses each into a **PSI** tree (Program Structure Interface — the Kotlin/IntelliJ AST representation). 3. Runs every **enabled rule** against the tree. 4. Reports each **finding** (rule id, file, line, message). 5. **Fails the build** if findings exceed the threshold — turning it into a *quality gate*. ## Rule sets Rules are grouped into **rule sets**: `complexity`, `style`, `potential-bugs`, `coroutines`, `naming`, `exceptions`, `performance`, and more. Each rule has a unique id (e.g. `LongMethod`, `MagicNumber`, `TooGenericExceptionCaught`). ## Reports Reports land under `build/reports/detekt/`: HTML (human-friendly), XML (Checkstyle format for CI), SARIF (for GitHub code scanning), and TXT. ## detekt vs ktlint - **ktlint** = pure formatting/style (indentation, import order). It can auto-format. - **detekt** = code smells and complexity. It mostly *reports* (limited autocorrect for some rules via `autoCorrect`). Many projects run both. detekt is the deeper "is this code healthy?" check.

  • Does detekt run your tests or execute the code?
    No. It is purely static — it parses and analyzes source text only, so it is fast and needs no running application.
  • Where do you see the results?
    In the console and as report files under build/reports/detekt (HTML, XML, SARIF, TXT).

detekt is a spell-and-grammar checker for code health: it reads your draft and flags awkward, risky, or overly tangled passages without ever 'running' the story.

saying these in an interview costs you the question

  • Saying detekt runs the program or executes tests
  • Confusing detekt with ktlint by claiming its main job is formatting
  • Not knowing it is applied as a Gradle (or Maven/CLI) plugin
  • Thinking findings never affect the build outcome

context

open as a page

How does `detekt.yml` work, and what do `buildUponDefaultConfig` and `--build-upon-default-config` mean when configuring rules?

level: middleimportance: must knowfreq 55%

basics

~10 s

detekt.yml is a YAML file where you turn rules on or off and set their thresholds. With buildUponDefaultConfig, your file only overrides the defaults instead of replacing them entirely.

open as a page

What is a detekt baseline file, how do you generate it, and how should it be used when adopting detekt on a legacy codebase?

level: middleimportance: should knowfreq 48%

basics

~10 s

A baseline is a file that records all existing detekt findings so they get ignored from now on. It lets you adopt detekt without fixing everything first, while still catching new problems.

open as a page

How do you configure detekt reports (SARIF, XML, HTML) and integrate detekt into CI so it gates merges and surfaces findings in code review?

level: middleimportance: should knowfreq 38%

basics

~20 s

detekt can output reports in several formats. SARIF is a standard JSON format that GitHub understands, so you upload it to show findings as annotations on pull requests. In CI you run the detekt task and fail the build on findings.

open as a page

What is type resolution in detekt, why do some rules require it, and how do you enable it (and how does it differ from a plain `detekt` run)?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Some detekt rules need to know the real types of expressions, not just the text. That extra information is type resolution. You enable it by giving detekt the compiled classpath, which makes those advanced rules work.

open as a page

How do you write a custom detekt rule and register it as a `RuleSetProvider`, and what core APIs are involved?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

You write a class that extends detekt's Rule, override the visit method for the code you care about, and report findings. Then you bundle it in a RuleSetProvider and put that module on detekt's plugin classpath.

open as a page