Search code examples
androidgradlektlint

How do I add KtLint style to Android Studio using Gradle?


I want to add the style ktlint uses to Android Studio so that when I apply formatting myself it uses the ktlint style.

Based on the documentation, I installed the ktlint CLI

brew install ktlint 

I then navigated to the root of my project and executed

ktlint --android applyToIDEAProject

The style now appears in my preferences for use.

The issue is that the style applied through CLI uses what I think is the latest version of ktlint since my manual formatting is different from the formatting Gradle task.I would prefer if it used the style from the Gradle plugin so the style applied to the project is the same one the Gradle task uses when formatting.

Finally, I would like it to be a Gradle task so that other developers can import and apply the same style as I could create a hook.

Below is my 'ktlint' Gradle file

dependencies {
    ktlint "com.pinterest:ktlint:0.34.2"
}


task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "src/**/*.kt"
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "-F", "src/**/*.kt"
}

UPDATE 1

I looked into one of the Gradle plugins available and saw this file. To me it looks like a wrapper around the main library so it must be possible without using the 3rd party plugin. https://github.com/JLLeitschuh/ktlint-gradle/blob/master/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintApplyToIdeaTask.kt

The version I was using is 0.34.2 and comparing that to the latest, it looks like support for those commands were added later as seen here https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt#L49

From this information, I have added this Gradle task which has progressed me but still fails.

task addKtLintStyle(type: JavaExec, group: "formatting") {
    description = "yep"
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "--android", "applyToIDEA"
}

It fails with this error

.idea directory not found. Are you sure you are inside project root directory? 

Which is thrown here

https://github.com/pinterest/ktlint/blob/master/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEACommandHelper.kt#L27

My gradle file is in the root of my project so I am not sure what to do next at this point.


Solution

  • I managed to get this to work. This was the final task definition

    task addKtLintStyle(type: JavaExec, group: "formatting") {
        description = "Adds The KtLint Style To Your IDE"
        classpath = configurations.ktlint
        main = "com.pinterest.ktlint.Main"
        args "--android", "applyToIDEA", "-y"
        //Point to the root directory because this task needs access to the .idea directory
        workingDir(getRootDir())
    }
    

    Note the line

        //Point to the root directory because this task needs access to the .idea directory
        workingDir(getRootDir())
    

    and also

        args "--android", "applyToIDEA", "-y"
    

    The -y just accepts the applied style.

    After restarting Android Studio, I am able to see the ktlint style