Detekt Static Analyzer for Kotlin

It's time to love static analyzers! Meet Detekt at Kotlin


If you already know what will be discussed and you are only interested in implementing the project, proceed immediately to the installation point.

What is it?


image

Software analysis performed (as opposed to dynamic analysis) without real execution of the programs under study. Wikipedia

And in your own words, it’s a handy tool to help you follow the rules of good code. Allowing very flexible customization of the filters by the definition of bad code. Which at each launch will generate a certain report, including statistical data on detected conflicts, information about them, which allows to determine where the conflict occurred and its type.

Static analyzers facilitate the work of the reviewer and generally save the development team time.

Suppose we have a good PR, however, the author gave names that are too long for the variables, wrote code that can be easily simplified or violated one of the many set rules in the Detekt configuration file, the rules are not complicated, but code-review will not pass such a PR.
The author will again have to switch between projects, as well as the reviewers, I am sure that repeated review will bring little pleasure.
Automate if it's fast and not expensive. (C) Common sense

What are static analyzers?


For java:


For Kotlin:


I answer the question that arose in your head, Spot-bugs on Kotlin does not work.

Introducing Detekt into the project


Detect can be installed in several ways:

  1. Plugin for AndroidStudio
  2. Gradle

I think the configuration through Gradle is more flexible and I’m talking about it.

On the official website there are several ways to install Detekt in the project, depending on the version of Gradle, whether it is an Android project. But, in my experience of implementing an android project, some instructions do not work . At the moment, the project is very close to 1.0 release. The latest version at the time of writing is release candidate 1.0.0-RC14

. So.

We go to the official Gradle website and see the installation instructions.

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
 }
     dependencies {
         classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0-RC14"
     }
}

It is this decision that I advise to adhere to for the Android project.

You probably noticed that I removed the apply: plugin line, as I advise you to separate Detekt into a separate file and apply apply in it already.

Stage 1:


So, copy this code to the application level build.gradle.

There, in the allprojects block , we should write a line for applying our file containing detekt settings.

allprojects {
    apply from: "$rootDir/detekt.gradle"
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
}

Stage 2:


Then you need to create the detekt.gradle file

apply plugin: "io.gitlab.arturbosch.detekt"
detekt {
    toolVersion = "1.0.0-RC14"                              // Version of the Detekt CLI that will be used. When unspecified the latest detekt version found will be used. Override to stay on the same version.
    input = files("src/main/java")                          // The directories where detekt looks for input files. Defaults to `files("src/main/java", "src/main/kotlin")`.
    parallel = true                                         // Builds the AST in parallel. Rules are always executed in parallel. Can lead to speedups in larger projects. `false` by default.
    filters = ".*build.*,.*/resources/.*,.*/tmp/.*"         // Regular expression of paths that should be excluded separated by `;` or `,`.
    config = files("$rootDir/detekt-config.yml")            // Define the detekt configuration(s) you want to use. Defaults to the default detekt configuration.
    reports {
        xml {
            enabled = true                                  // Enable/Disable XML report (default: true)
            destination = file("build/reports/detekt.xml")  // Path where XML report will be stored (default: `build/reports/detekt/detekt.xml`)
        }
        html {
            enabled = true                                  // Enable/Disable HTML report (default: true)
            destination = file("build/reports/detekt.html") // Path where HTML report will be stored (default: `build/reports/detekt/detekt.html`)
        }
    }
}

Stage 3:


Now you need to create the configuration file detekt-config.yml

This is the standard configuration file from the official site .

Stage 4:


Open a console and run the command: gradlew detekt

All!


Now you have ready statistics for your project. They are displayed in the console, and you can also find them along the path: {u_project} \ app \ build \ reports \ detekt

Finally


It is very convenient to use Detekt when assembling in Jenkins.
The execution time of a task in a project with more than 2000 classes is 4-7 seconds *.

I want to say that using a static analyzer simplifies our work a little. Accelerates the development process and saves business money.

Links:

Detekt github
Set up Detekt
default-detekt-config.yml

Also popular now: