Detekt : A static code analysis tool for Kotlin

Posted By : Bipin Tiwari | 29-Oct-2021

If you have developed in Kotlin you must have came across with klint or lint. If you don't know what that is then here is the definition according to wikipedia :

 

    "lint, or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs."

 

When it comes to code analysis tool, in Java there are  plenty tools like pmd, findbugs, checkstyle, SonarQube. But when it comes to Kotlin we have only few proper option like Detekt and Klint.

 

With Klint the rules can't be modified and the gradle plugins are not easy to integrate.

 

Detekt is a Kotlin specific linting tool that allow us to enforce codebase documentation, formatting standards, and trace bugs before they do any harm.

 

Detekt provide a number a features like  

  1. It highly configurable (rule set or rule level).
  2. You can specify code smell thresholds to break your build or print a warning.
  3. Gradle plugin for code analysis via Gradle builds
  4. Complexity report based on logical lines of code, McCabe complexity and amount of code smells
  5. Extensible by own rule sets and FileProcessListener's

 

Setup Detekt :
 

1. Add the classpath dependency to your existing dependencies in your root build.gradle

          dependencies {

                     classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.10.0"
           }

 

Note : You can always find the latest version here : 
https://plugins.gradle.org/plugin/io.gitlab.arturbosch.detekt

 

2. Now create your Detekt rules configuration file, which specify which rules Detekt should follow when anlayzing for code smell and leaks. Its simple just follow the steps provide on the official documentation page of Detekt: https://arturbosch.github.io/detekt/configurations.html

You can specify the name to detekt.yml and put in root project path.

 

3. Create the Detekt configuration file : detekt.gradle and put it on the same path as rules configuration file.

 

apply plugin: 'io.gitlab.arturbosch.detekt'

detekt {
  profile("main") {
    input = "$projectDir"
    config = "$project.rootDir/detekt-ruleset.yml"
    filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    output = "${project.buildDir}/reports/detekt.xml"
    parallel = true
   }
}

 

4 . Final Step : Add the below line in every kotlin module's build.gradle : 

 

apply from: '../detekt.gradle'

Note : The path for detekt.gradle is where you put it in 3rd step.

And its Done!

 

Now just run below command for code analysis :

 

./gradlew detektCheck

 

and you will have the full report for code smell.

About Author

Author Image
Bipin Tiwari

Bipin is a highly skilled Android developer with extensive professional experience in creating innovative and efficient mobile applications. He possesses proficiency in programming languages such as Java and Kotlin, along with a deep understanding of Android SDK, design patterns, and best coding practices. Bipin is committed to delivering high-quality code and ensuring exceptional user experiences. With excellent problem-solving skills and the ability to work collaboratively in a team environment, he has made valuable contributions to both client and internal projects, including the development of the Blackbook Travels App and Corniz App.

Request for Proposal

Name is required

Comment is required

Sending message..