9

I am using detekt with ktlint for formatting my code is like below.

detekt.gradle

ext{
    toolVersion = "1.0.0-RC16"
}
detekt {
    input = files(...)

    filters = ".*/resources/.*,.*/build/.*"
    baseline = file("${project.rootDir}/tools/detekt-baseline.xml")
    reports{
        html{
            enabled = true
        }
        xml{
            enabled = false
        }
    }
    config = files(file("$project.rootDir/tools/detekt.yml"))
}

dependencies {
    detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:$toolVersion"
}

detekt-baseline.yml

autoCorrect: true

build:
  maxIssues: 10
  weights:
  # complexity: 2
  # LongParameterList: 1
  # style: 1
  # comments: 1

Project level build.gradle

buildscript {
    ext{...}
    repositories {...}
    dependencies {...}
}

plugins{
    id "io.gitlab.arturbosch.detekt" version "1.0.0-RC16"
}

apply from: 'tools/detekt.gradle'
...


allprojects {
    repositories {...}
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

As you can see I have added detekt plugin for formatting in detekt.gradle. I have also enabled autoCorrect in detekt-baseline.yml. But code is not formatted when I ran ./gradlew detekt Generated html report shows no findings, but the metrics are shown.

When I ran ./gradlew detekt after commenting out following line in detekt.gradle.

//    config = files(file("$project.rootDir/tools/detekt.yml"))

It does shows me findings, including formatting issues (ex: needless blank lines)

How could I configure detekt to auto format the code according to the ktlint?


This is an android project.

user158
  • 12,852
  • 7
  • 62
  • 94

1 Answers1

5

Since detekt 1.1.0-RC15, you have to enable the autoCorrect in gradle:

detekt {
    autoCorrect = true
}

https://detekt.github.io/detekt/changelog-rc.html#rc15

caladeve
  • 456
  • 4
  • 12
Khongor Bayarsaikhan
  • 1,644
  • 1
  • 16
  • 19