How to configure ktlint gradle plugin in a continuous mode?

780 views Asked by At

In my Android Studio app project, I've integrated ktlint gradle.

Is it possible to configure the ktlint gradle plugin, so that it runs continuously on the fly (while coding on the keyboard)?? Like ESlint is used for JS, it warns me immediately if any lint warnings occur..

3

There are 3 answers

0
Xid On BEST ANSWER
1
Shubham Kumar Gupta On

After lot of googling and exploring about ktlint we can setup like this

module(ex: app) level gradle

apply plugin: 'org.jlleitschuh.gradle.ktlint'

dependencies {}

ktlint {
    version = "0.48.2"
    debug = true
    verbose = true
    android = true
    outputToConsole = true
    outputColorName = "RED"
    ignoreFailures = true
    enableExperimentalRules = true
    additionalEditorconfig = [ // not supported until ktlint 0.49
                               "max_line_length": "20"
    ]
    disabledRules = ["final-newline"] // not supported with ktlint 0.48+
    baseline = file("${projectDir}/config/ktlint/baseline.xml")
    reporters {
        reporter "plain"
        reporter "checkstyle"
        reporter "sarif"
    }
    kotlinScriptAdditionalPaths {
        include fileTree("scripts/")
    }
    filter {
        exclude("**/generated/**")
        include("**/kotlin/**")
    }
}

project level build

plugins {
    id("org.jlleitschuh.gradle.ktlint") version "12.1.0" apply false
}

allprojects {
    task copyGitHooks(type: Copy) {
        description = "Copies the git hooks from /git-hooks to the .git folder."
        group = "git hooks"
        from "$rootDir/scripts/pre-commit"
        into "$rootDir/.git/hooks/"
    }

    task installGitHooks(type: Exec) {
        description = "Installs the pre-commit git hooks from /git-hooks."
        group = "git hooks"
        workingDir rootDir
        commandLine "chmod", "-R", "+x", ".git/hooks/"
        dependsOn copyGitHooks
        doLast {
            logger.info("Git hook installed successfully.")
        }
    }
    afterEvaluate { project ->
        if (project.tasks.findByName("preBuild") != null) {
            project.tasks.named("preBuild") { dependsOn "installGitHooks" }
        }
    }
}

Create a pre-commit(no extension) file inside a scripts folder [project top level]

#!/bin/bash

echo "............Starting ktlint check......................................"

echo "Started Formitting...."

./gradlew ktlintFormat

#
#echo ".....Staging all file changed during ktlint formatting................."
#
#git add .

echo "Completed.............................................................."

To enable git hook you need to run git config core.hooksPath .git/hooks

You need to create ktlint baseline for each module

Steps:

  • Sync
  • Gradle Tasks
  • App Module Tasks
  • help
  • ktlintGenerateBaseline

or from terminal you can run ./gradlew :app:ktlintGenerateBaseline

Using extension you can setup this by installing

https://plugins.jetbrains.com/files/15057/287040/ktlint-0.12.0.zip?updateId=287040&pluginId=15057&family=INTELLIJ

enter image description here

0
amram99 On

This is possible in Intellij / Android Studio if you install the ktlint IDEA plugin.

It's also configurable so you can represent findings as warnings or errors.