skip to content

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