detekt: Static Analysis for Kotlin in CI
detekt analyzes Kotlin source for code smells and complexity, exiting non-zero when issues exceed the configured threshold.
detekt is the standard Kotlin static analyzer. It runs as a Gradle task or CLI, reads a YAML config, and can baseline existing issues.
What it does
detekt parses Kotlin files under --input, applies its rule sets, and writes reports (txt, html, xml/checkstyle, sarif, md). It exits 0 when clean and a non-zero code when issues are found (or weighted issues pass maxIssues). The Gradle plugin exposes the same as the detekt task.
Common usage
# Gradle task form (most common)
./gradlew detekt
# CLI form
detekt --input src/main/kotlin --config detekt.yml \
--report sarif:detekt.sarifFlags
| Flag | What it does |
|---|---|
| --input <paths> | Source files or directories to analyze |
| --config <file> | Path to the detekt YAML config |
| --baseline <file> | Ignore issues recorded in the baseline |
| --report <id:path> | Write a report (txt, html, xml, sarif, md) |
| --build-upon-default-config | Layer your config over the defaults |
| --auto-correct | Apply formatting autocorrections (with formatting rules) |
In CI
Run detekt as a Gradle task so it uses the same plugin version as the build. Emit a SARIF report and upload it to code scanning, or checkstyle XML for CI plugins, so findings appear in the PR rather than only the console.
Common errors in CI
"Property 'X' is misspelled or does not exist" in the config means a rule or rule-set name changed between detekt versions; align the config with the plugin version. "Analysis failed with N weighted issues" is the normal failure when issues exceed maxIssues. A missing type-resolution warning appears for rules that need a classpath; configure detektMain with type resolution.