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
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 && chmod a+x detekt
./detekt --help
You can find other ways to install detekt here
Gradle 5.0+ is required:
buildscript {
repositories {
jcenter()
}
// or
mavenCentral()
jcenter {
content {
// just allow to include kotlinx projects
// detekt needs 'kotlinx-html' for the html report
includeGroup "org.jetbrains.kotlinx"
}
}
}
plugins {
id("io.gitlab.arturbosch.detekt").version("[version]")
}
detekt {
failFast = true // fail build on any finding
buildUponDefaultConfig = true // preconfigure defaults
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
}
}
// Groovy dsl
tasks.detekt.jvmTarget = "1.8"
// or
// Kotlin dsl
tasks {
withType<Detekt> {
// Target version of the generated JVM bytecode. It is used for type resolution.
this.jvmTarget = "1.8"
}
}
See bintray for releases and artifactory for snapshots.
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
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
- GitHub Action: detekt
- 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
- detekt-hint is a plugin to detekt that provides detection of design principle violations through integration with Danger
- JetBrains - Creating IntelliJ + Kotlin
- PMD & Checkstyle & ktlint - Ideas for threshold values and style rules