Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Latest commit

 

History

History
75 lines (59 loc) · 4.06 KB

File metadata and controls

75 lines (59 loc) · 4.06 KB

Detekt

Detekt is a static analysis tool that looks for potential bugs and style violations in Kotlin code. It does not support Java. It can be used in both pure Kotlin, and Android Kotlin projects. It then only makes sense to have Detekt enabled if you have Kotlin code in your project. The plugin only runs Detekt on projects that contain the Kotlin or the Kotlin-Android plugin.

Supported Detekt Gradle Plugin version: 1.0.0.RC9.2 and above Recommended Detekt Gradle Plugin version: 1.0.0-RC14 and above

Table of contents


IMPORTANT: setup Detekt

Unlike the other tools, the plugin won't automatically add Detekt to your project. If you forget to do it, the plugin will fail the build with an error.

In order to use Detekt, you need to manually add it to all your Kotlin projects. You can refer to the official documentation for further details. Note that you should not add the detekt closure to your build.gradles, unlike what the official documentation says. The detekt closure in the staticAnalysis configuration gets applied to all Kotlin modules automatically.

In most common cases, adding Detekt to a project boils down to three simple steps:

  1. Add this statement to your root build.gradle project (change the version according to your needs):
    plugins {
        id 'io.gitlab.arturbosch.detekt' version '1.0.0-RC14'
        // ...
    }
  2. Add this statement to each Kotlin project's build.gradles:
    plugins {
        id 'io.gitlab.arturbosch.detekt'
        // ...
    }

Configure Detekt

Enabling and configuring Detekt for a project is done through the detekt closure. The closure behaves exactly like the standard Detekt plugin does in Gradle, which is to say, quite differently from how the other tools' configurations closures work. For example:

detekt {
    toolVersion = "[version]" // custom toolVersion defined. By default, it is the Gradle plugin version
    input = files("src/main/kotlin") // Optional: files representing project's Kotlin sources
    filters = ".*/resources/.*,.*/build/.*" // A comma-separated list of regex exclusions
    baseline = file("my-detekt-baseline.xml") // Optional: Just if you want to create a baseline file.
}

(assuming you're using the Novoda scaffolding system, see Example configurations for more details)

You need to provide at a minimum the config and output values. It's important that you do not specify a warningThreshold nor a failThreshold in the Detekt configuration file as it will interfere with the functioning of the Static Analysis plugin's threshold counting. For the same reason, make sure that failFast is set to false in the Detekt configuration.

For more information about Detekt rules, refer to the official website.

Exclude files from Detekt analysis

In order to exclude files from Detekt analysis, you have to use the facilities provided by the Detekt plugin in the detekt configuration closure. This means, you have to provide a value to the filters property that contains the exclusion pattern(s) you wish Detekt to ignore.

The filters property expects a string consisting in a comma-separated list of regular expression patterns, e.g., '.*test.*,.*/resources/.*,.*/tmp/.*' (which would exclude any path containing the word test, or any path containing a directory called resources or tmp).

Detekt in mixed-language projects

If your project mixes Java and Kotlin code, you don't need to have an exclusion in place for all *.java files. Detekt itself only looks for *.kt files, so no further configuration is required.