Meet detekt, a static code analysis tool for the Kotlin programming language. It operates on the abstract syntax tree provided by the Kotlin compiler.
- Code smell analysis for your Kotlin projects
- Complexity reports based on lines of code, cyclomatic complexity and amount of code smells
- Highly configurable rule sets
- Suppression of findings with Kotlin's
@Suppress
and Java's@SuppressWarnings
annotations - Specification of quality gates which will break your build
- Code Smell baseline and suppression for legacy projects
- Gradle plugin for code analysis via Gradle builds
- SonarQube integration
- Extensibility by enabling incorporation of personal rule sets,
FileProcessListener's
andOutputReport's
- IntelliJ integration
- Third party integrations for Maven, Bazel and Github Actions (Docker based and Javascript based)
Visit the project website for installation guides, release notes, migration guides, rule descriptions and configuration options.
- Changelog and migration guides
- Available CLI options
- Rule set and rule descriptions
- Writing custom rules and extending detekt
- Suppressing issues in code
- Suppressing issues via baseline file
- Configuring detekt
- Sample Gradle integrations examples:
curl -sSLO https://github.com/detekt/detekt/releases/download/v[version]/detekt-cli-[version]-all.jar
java -jar detekt-cli-[version]-all.jar --help
You can find other ways to install detekt here
plugins {
id("io.gitlab.arturbosch.detekt").version("[version]")
}
repositories {
mavenCentral()
}
detekt {
buildUponDefaultConfig = true // preconfigure defaults
allRules = false // activate all available (even unstable) rules.
config = files("$projectDir/config/detekt.yml") // point to your custom config defining rules to run, overwriting default behavior
baseline = file("$projectDir/config/baseline.xml") // a way of suppressing issues before introducing detekt
reports {
html.enabled = true // observe findings in your browser with structure and code snippets
xml.enabled = true // checkstyle like format mainly for integrations like Jenkins
txt.enabled = true // similar to the console output, contains issue signature to manually edit baseline files
sarif.enabled = true // standardized SARIF format (https://sarifweb.azurewebsites.net/) to support integrations with Github Code Scanning
}
}
// Groovy DSL
tasks.withType(Detekt).configureEach {
jvmTarget = "1.8"
}
// or
// Kotlin DSL
tasks.withType<Detekt>().configureEach {
// Target version of the generated JVM bytecode. It is used for type resolution.
jvmTarget = "1.8"
}
See maven central for releases and sonatype for snapshots.
If you want to use a SNAPSHOT version, you can find more info on this documentation page.
Gradle 6.1+ is the minimum requirement. However, the recommended versions together with the other tools recommended versions are:
Detekt Version | Gradle | Kotlin | AGP | Java Target Level | JDK Max Version |
---|---|---|---|---|---|
1.17.0 |
7.0.1 |
1.4.32 |
4.2.0 |
1.8 |
15 |
The list of recommended versions for previous detekt version is listed here.
detekt itself provides a wrapper over ktlint as a formatting
rule set
which can be easily added to the Gradle configuration:
dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:[version]")
}
Likewise custom extensions can be added to detekt.
If you contributed to detekt but your name is not in the list, please feel free to add yourself to it!
- Artur Bosch - Maintainer
- Marvin Ramin - Collaborator, Bunch of rules, Active on Issues, refactorings, MultiRule
- schalks - Collaborator, Active on Issues, Bunch of rules, Project metrics
- Niklas Baudy - Active on Issues, Bunch of rules, Bug fixes
- lummax - Cli enhancements
- Svyatoslav Chatchenko - Active on Issues, NamingConventions and UnusedImport fixes
- Sean Flanigan - Config from classpath resource
- Sebastian Schuberth - Active on Issues, Windows support
- Olivier Lemasle - NP-Bugfix, rules fixes, Gradle plugin improvement
- Marc Prengemann - Support for custom output formats, prototyped Rule-Context-Issue separation
- Sebastiano Poggi - Build tooling improvements, rules improvements and fixes, docs fixes, Gradle plugin improvements
- Ilya Tretyakov - Sonar runs should not auto correct formatting.
- Andrey T - Readme fix
- Ivan Balaksha - Rules: UnsafeCast, SpreadOperator, UnsafeCallOnNullableType, LabeledExpression
- Anna Y - Readme fix
- Karol Wrótniak - Treat comments as not empty blocks
- Radim Vaculik - VariableMaxLength - bugfix
- Martin Nonnenmacher - UndocumentedPublicClass - enum support
- Dmytro Troynikov - Updated Magic Number rule to ignore Named Arguments
- Andrew Ochsner - Updated Readme for
failFast
option - Paul Merlin - Gradle build improvements
- Konstantin Aksenov - Coding improvement
- Matthew Haughton - Added type resolution, Dependency updates, Coding + Documentation improvements
- Janusz Bagiński - Fixed line number reporting for MaxLineLengthRule
- Mike Kobit - Gradle build improvements
- Philipp Hofmann - Readme improvements
- Olivier PEREZ - Fixed Typo in Readme
- Sebastian Kaspari - Html-Output-Format, Documentation fix
- Ilya Zorin - Rule improvement: UnnecessaryAbstractClass
- Gesh Markov - Improve error message for incorrect configuration file
- Patrick Pilch - Rule improvement: ReturnCount
- Serj Lotutovici - Rule improvement: LongParameterList
- Dmitry Primshyts - Rule improvement: MagicNumber
- Egor Neliuba - Rule improvement: EmptyFunctionBlock, EmptyClassBlock
- Said Tahsin Dane - Gradle plugin improvements
- Misa Torres - Added: TrailingWhitespace and NoTabs rules
- R.A. Porter - Updated Readme links to RuleSets
- Robbin Voortman - Rule improvement: MaxLineLength
- Mike Gorunov — Rule improvement: UndocumentedPublicFunction
- Joey Kaan - New rule: MandatoryBracesIfStatements
- Dmitriy Samaryan - Rule fix: SerialVersionUIDInSerializableClass
- Mariano Simone - Rule improvement: UnusedPrivateMember. New Rules: UnusedPrivateClass, VarCouldBeVal
- Shunsuke Maeda - Fix: to work on multi module project using maven plugin
- Mikhail Levchenko - New rules: Unnecessary let, ExplicitItLambdaParameter
- Scott Kennedy - Minor fixes
- Mickele Moriconi - Added: ConstructorParameterNaming and FunctionParameterNaming rules
- Lukasz Jazgar - Fixed configuring formatting rules
- Pavlos-Petros Tournaris - Lazy evaluation of Regex in Rules
- Erhard Pointl - Kotlin DSL and Gradle enhancements
- Tyler Thrailkill - FunctionNaming rule enhancements
- Tarek Belkahia - TooManyFunctions rule options
- Bournane Abdelkrim - Fix typos
- Rafael Toledo - Fix Gradle plugin badge
- Alberto Ballano - ExceptionRaisedInUnexpectedLocation rule improvements
- Guido Pio Mariotti - Documentation improvement
- Mygod - UnusedImports rule improvement
- Andreas Volkmann - yaml code comments
- glammers - Documentation improvement
- Ahmad El-Melegy - yaml syntax fix
- Arjan Kleene - Add unnecessary apply rule
- Paweł Gajda - Rule improvement: FunctionParameterNaming
- Alistair Sykes - Doc improvement
- Andrew Arnott - UnusedPrivateMember improvement
- Tyler Wong - UnderscoresInNumericLiterals rule
- Daniele Conti - ObjectPropertyNaming improvement
- Nicola Corti - Fixed Suppress of MaxLineLenght
- Michael Lotkowski - Rule improvement: False positive UnusedImport for componentN
- Nuno Caro - Adds TXT report support on Gradle plugin
- Minsuk Eom - Rule fix: PackageNaming
- Jonas Alves - Rule fix: MagicNumber with ignoreNamedArgument and a negative value
- Natig Babayev - Readme improvements
- David Phillips - New rule: MandatoryBracesLoops
- Volkan Şahin - Documentation improvement
- Remco Mokveld - Rename Blacklist/Whitelist to more meaningful names
- Zachary Moore - Rule, cli, gradle plugin, and config improvements
- Veyndan Stuart - New rule: UseEmptyCounterpart; Rule improvement: UselessCallOnNotNull
- Parimatch Tech - New rule: LibraryEntitiesShouldNotBePublic, UnnecessaryFilter
- Chao Zhang - SARIF report format; Rule improvements
- Marcelo Hernandez - New rule: SuspendFunWithFlowReturnType, ObjectExtendsThrowable
- Harold Martin - Rule improvement: ClassOrdering
- Roman Ivanov - Rule improvement: ReturnFromFinally
- Severn Everett - New rule: SleepInsteadOfDelay
- Adam Kobor - New rule: MultilineLambdaItParameter
- Slawomir Czerwinski - Rule improvement: FunctionOnlyReturningConstant
- Ivo Smid - Fix Local development on Windows
- Krzysztof Kruczynski - Rule fix: ThrowingExceptionInMain, ExitOutsideMain
- Paya Do - Designer for Detekt's logo
- zmunm - New rule: ObjectLiteralToLambda
- Vinicius Montes Munhoz - Documentation improvement
- Eliezer Graber - Rule fix: ModifierOrder
- Dominik Labuda - Gradle plugin improvement
As mentioned in...
- KotlinConf 2018 - Safe(r) Kotlin Code - Static Analysis Tools for Kotlin by Marvin Ramin
- droidcon NYC 2018 - Static Code Analysis For Kotlin
- Kotlin on Code Quality Tools - by @vanniktech Slides Presentation
- @medium/acerezoluna/static-code-analysis-tools-for-kotlin-in-android
- @medium/annayan/writing-custom-lint-rules-for-your-kotlin-project-with-detekt
- Free Continuous Integration for modern Android apps with CircleCI
- Static code analysis for Kotlin in Android
- The Art of Android DevOps
- Android Basics: Continuous Integration
- Kotlin Static Analysis — why and how?
- Check the quality of Kotlin code
- Kotlin Static Analysis Tools
- Speeding up the detekt task in a multi-project Gradle build (for detekt < 1.7.0)
- SBCARS '18 - Are you still smelling it?: A comparative study between Java and Kotlin language by Flauzino et al.
- Preventing software antipatterns with Detekt
Integrations:
- SonarKotlin
- Codacy
- Gradle plugin that generates ErrorProne, Findbugs, Checkstyle, PMD, CPD, Lint, Detekt & Ktlint Tasks for every subproject
- Java library for parsing report files from static code analysis
- sputnik is a free tool for static code review and provides support for detekt
- Novoda Gradle Static Analysis plugin
- Maven plugin that wraps the Detekt CLI
- Bazel plugin that wraps the Detekt CLI
- Gradle plugin that helps facilitate GitHub PR checking and automatic commenting of violations
- Codefactor
- GitHub Action: Detekt All
- IntelliJ Platform Plugin Template
- MuseDev
Custom rules and reports from 3rd parties:
- cph-cachet/detekt-verify-implementation
- detekt-hint is a plugin to detekt that provides detection of design principle violations through integration with Danger
- GitLab report format
- JetBrains - Creating IntelliJ + Kotlin
- PMD & Checkstyle & ktlint - Ideas for threshold values and style rules