📜 ⬆️ ⬇️

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 implementation in the project, go directly to the installation point.

What is it?


image

Analysis of software produced (as opposed to dynamic analysis) without real execution of the studied programs. Wikipedia
')
And in your own words, this is a handy tool that helps in complying with the rules of good code. Allowing very flexible configuration of the filter to determine bad code. Which at each start will generate a certain report, including statistical data on detected conflicts, information about them, allowing 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, but the author gave too long names to variables, wrote code that can be easily simplified or violated one of the many rules that can be set in the Detekt configuration file, the rules are not complicated, but this PR will not work through code-review.
The author will again have to switch between projects, as well as reviewers, I am sure, a little pleasure will be delivered by a second review.
Automate if it is fast and not expensive. (C) Common sense

What are static analyzers?


For java:


For Kotlin:


I answer the question you have in my head, Spot-bugs on Kotlin does not work.

Implement Detekt in the project


Detekt can be installed in several ways:

  1. Plugin for AndroidStudio
  2. Gradle

I think the setting through Gradle is more flexible and I’ll tell you about it.

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

So.

Go to the official website Gradle and see the instructions for installation.

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

This is the solution that I advise you to stick with 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.

Stage 1:


So, we copy this code in the application level build.gradle.

There, in the allprojects block , we have to write a line for applying our detekt configuration file.

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

Stage 2:


Then you need to create the file detekt.gradle

 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 a configuration file detekt-config.yml

This is the standard configuration file from the official site .

Stage 4:


Open the console and run the command: gradlew detekt

Everything!


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

Finally


Detekt is very convenient to use when building in Jenkins.
The task execution time in the program with more than 2000 classes is 4-7 seconds *.

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

References:

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

Source: https://habr.com/ru/post/442500/


All Articles