Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding detekt static code analysis to kotlin projects

You can add static code analysis to kotlin using detekt. Below is the basic configuration, for more details about configuration and rules go to project page: https://arturbosch.github.io/detekt/.

//add to top-level gradle file
buildscript {
    ext {
        detektVersion = '1.0.0.RC7'
    }

    repositories {
        classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion"
}

//add to module gradle file
apply plugin: 'io.gitlab.arturbosch.detekt'
detekt {
    version = detektVersion
    profile("main") {
        output = "$project.projectDir/build/reports/detekt"
        config = "$projectDir/scripts/detekt-config.yml"
        parallel = true
    }
}

Gradle step for checking is detektCheck. If you want to enable it on an already existing projects, there will probably be some problems which you might not want to fix immediately - in that case, you can create a baseline file first, based on which only new problems will be reported.

First add the baseline file setup to your detekt setup in the module gradle file:

detekt {
    profile("main") {
        baseline = file("../scripts/detekt-baseline.xml")
    }
}

Then create the baseline file to be used with detektBaseline gradle step. Further detektCheck calls won't fail the build and will report only new problems.

//detekt-config.yml example
failFast: true //this fails the build immediately (on any problem); it can be set to false and fail the build depending on the score
autoCorrect: true
style:
MaxLineLength:
maxLineLength: 150
NewLineAtEndOfFile:
active: true
UnusedPrivateMember:
active: false
complexity:
TooManyFunctions:
thresholdInClasses: 30 #using rule of 30
comments:
UndocumentedPublicClass:
active: false
UndocumentedPublicFunction:
active: false
potential-bugs:
LateinitUsage:
excludeAnnotatedProperties: "inject"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment