https://kotlinlang.org logo
#ktlint
Title
c

Colton Idle

03/19/2020, 3:28 AM
Trying to use ktlint in my Android app. Just found out about it recently and I read through the github.io page. It says "ktlint is a single binary with both linter & formatter included", but what I'm finding hard to understand is how to add it to my android project? I see that in getting started it has command line or gradle. I'm assuming since I have a default android project, I can just use gradle, but I'm still confused on why there are 3 plugins shown, and then a no plugin option. I'm not really sure what I'm looking for here, but maybe because I'm new it's going over my head. In my head, all I want is to run a linter during CI that gives me a report of all the broken rules + I want the IDE to tell me when I broke one of these (like android lint) or at least auto reformat code to these rules (cmd + shift + L in IntelliJ)
s

Sha Sha Chu

03/19/2020, 3:35 AM
Hi Colton! There's no single right way to use ktlint, although most people do use one of the Gradle plugins listed in the readme. Those will generate the gradle tasks you can invoke locally or on CI to lint (and optionally format) your code.
ktlint doesn't have a strong IDE integration, although you can sync some of the ktlint rule configs with IntelliJ configs
c

Colton Idle

03/19/2020, 3:40 AM
Thanks for clarifying. I'm trying to use https://github.com/jlleitschuh/ktlint-gradle but the docs don't mention whether to put the plugin in my root build.gradle or my app build.gradle. Any help on that end?
s

Sha Sha Chu

03/19/2020, 3:43 AM
I'm not actually sure (I develop in ktlint but don't have experience with the plugins.) @tapchicoma is one of the main contributors for that plugin, though, and tends to be quite responsive in this Slack.
c

Colton Idle

03/19/2020, 3:44 AM
Thanks. I'll wait for them to respond. Appreciate the help @Sha Sha Chu
s

Sha Sha Chu

03/19/2020, 3:45 AM
👍
b

Brais Gabin

03/19/2020, 8:00 AM
I have this in my root project and it works
Copy code
subprojects { 
  apply plugin: "org.jlleitschuh.gradle.ktlint"

  ktlint {
    version = "0.36.0"
    ignoreFailures = true

    reporters {
      reporter "checkstyle"
    }
  }
}
But if you don’t find the documentation of ktlint-gradle clear enough you should open an issue in the repo. The feedback from people that just arrived to the project is very valuable.
👍 1
t

tapchicoma

03/19/2020, 8:31 AM
usually you want to apply
ktlint-gradle
to root project to have additional helper tasks like git hook integration or IDEA codestyle integration. Other then that what @Brais Gabin wrote is correct (maybe except
ignoreFailures
🙂 )
b

Brais Gabin

03/19/2020, 8:33 AM
That’s true! I’m using ignoreFailures because I use QualityGate to check the reports later. But usually you don’t want
ignoreFailures
.
And I’ll try it in the root project. Thanks!
t

tapchicoma

03/19/2020, 8:35 AM
I would say git format hook is very convenient - saves some time on fixing codestyle after it failed on your CI. IDEA integration is not working so good though, would advice to generate
.editorconfig
with kotlin styling
c

Colton Idle

03/19/2020, 2:55 PM
Following the readme I ended up with this in the root of my android project:
Copy code
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.3.70"
    repositories {
        google()
        jcenter()
        maven {
            url "<https://plugins.gradle.org/m2/>"
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0-beta02"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

apply plugin: "org.jlleitschuh.gradle.ktlint"
But when I run
./gradlew ktlintCheck
I don't get any report.
t

tapchicoma

03/19/2020, 2:56 PM
you've applied plugin only to the root project. You need to add:
Copy code
subprojects { 
  apply plugin: "org.jlleitschuh.gradle.ktlint"
}
c

Colton Idle

03/19/2020, 2:57 PM
Awesome. That worked! Thank you @tapchicoma!
Three small questions. I can post in general instead. 1. I'm looking at "IntelliJ Idea Only Plugin" and
ktlintApplyToIdea
. Which one gives me the "cmd + shift + L" to reformat code as per the style in the IDE? 2. "Unexpected indentation (2) (it should be 4) " Why can't this be auto corrected? 3. Why don't comments that start at the beginning of the file, get indented?
t

tapchicoma

03/19/2020, 3:06 PM
1 It create custom codestyle scheme for kotlin. You could see it via Settings -> Editor -> CodeStyle -> Kotlin in Scheme dropdown. Though it has quite limited kotlin support. 2 -> ktlint rule issue 3 -> ktlint rule issue
c

Colton Idle

03/19/2020, 3:07 PM
For 2 and 3 (sorry, noob here with linters and stuff) does this mean I can just change the rule in the config?
t

tapchicoma

03/19/2020, 3:09 PM
2 -> rule implementation could not figure out how to autoformat properly. Could not recall rule name right now. 3 -> rule is missing for this. You could write your custom rule
c

Colton Idle

03/19/2020, 3:11 PM
Thank you Yahor!
s

Sha Sha Chu

03/19/2020, 4:27 PM
@Colton Idle re #2: confusingly, there are actually 2 different versions of the indentation rule. there is one that’s in the standard ruleset that simply checks indentation but does not autoformat, and there is one in the experimental ruleset that can reformat, but it’s actually a pretty difficult thing to do correctly, which is why it’s still in experimental. on command line you enable the experimental rules via the
--experimental
flag, but i’m not 100% how to enable them via the plugin. #3 is likely a bug - feel free to file on github
c

Colton Idle

03/19/2020, 6:14 PM
I filed two bugs. Hopefully they are valid and simple enough to highlight whether it's just user error. Again, I'm very new to adding linters and when they can be used, so I'm really trying hard to make sense of all of this. In the end... all I want is my team to all reformat their kotlin code all the same way via IntelliJ and I want to be able to see a result of all of the "issues" in my CI. Hope that ktlint is the solution (although the intelliJ capabilities are maybe missing?)
82 Views