How do I run ktlintFormat for every build I make through Android Studio

See original GitHub issue

First of all, thank you for making this amazing tool. I really appreciate the time you take to sharpen this tool to its full potential.

So here’s what I need.

Whenever I press the image or build my project, I want to run ktlintFormat and if it fails, I want to fail the build process also.

Can you help me configure it ?

NOTE: Currently ./gradlew ktlintFormat works perfectly fine.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
kiranshaw-qicommented, Jan 8, 2022

@kiranshaw-qi - this worked for me in app/build.gradle inside the dependencies block (note the last two lines) - you should be able to adapt this for your purpose, but in our case, we always wanted the build to fail if there were ktlint errors:

dependencies {
.
.
.
    ktlint("com.pinterest:ktlint:0.42.1") {
        attributes {
            attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
        }
    }

    task ktlint(type: JavaExec, group: "verification") {
        description = "Check Kotlin code style."
        main = "com.pinterest.ktlint.Main"
        classpath = configurations.ktlint
        args = [
                "--reporter=plain,output=${project.buildDir}/ktlint/report.txt",
                "--reporter=checkstyle,output=${project.buildDir}/ktlint/report.xml",
                "--verbose"
        ]
    }

    task ktlintFormat(type: JavaExec, group: "formatting") {
        description = "Fix Kotlin code style deviations."
        main = "com.pinterest.ktlint.Main"
        classpath = configurations.ktlint
        args = [
                "-F",
                "--reporter=plain,output=${project.buildDir}/ktlint/report.txt",
                "--reporter=checkstyle,output=${project.buildDir}/ktlint/report.xml",
                "--verbose"
        ]
    }

    ktlint.dependsOn(ktlintFormat)
    preBuild.dependsOn(ktlint)
}

As you can see, on pre-build, we run ktlintFormat first, then ktlint afterward. This will ensure that the auto-format occurs, if possible, if not, the build will fail and generate an error report.

thanks @h-bomb it worked.

0reactions
h-bombcommented, Jan 8, 2022

@kiranshaw-qi - this worked for me in app/build.gradle inside the dependencies block (note the last two lines) - you should be able to adapt this for your purpose, but in our case, we always wanted the build to fail if there were ktlint errors:

dependencies {
.
.
.
    ktlint("com.pinterest:ktlint:0.42.1") {
        attributes {
            attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
        }
    }

    task ktlint(type: JavaExec, group: "verification") {
        description = "Check Kotlin code style."
        main = "com.pinterest.ktlint.Main"
        classpath = configurations.ktlint
        args = [
                "--reporter=plain,output=${project.buildDir}/ktlint/report.txt",
                "--reporter=checkstyle,output=${project.buildDir}/ktlint/report.xml",
                "--verbose"
        ]
    }

    task ktlintFormat(type: JavaExec, group: "formatting") {
        description = "Fix Kotlin code style deviations."
        main = "com.pinterest.ktlint.Main"
        classpath = configurations.ktlint
        args = [
                "-F",
                "--reporter=plain,output=${project.buildDir}/ktlint/report.txt",
                "--reporter=checkstyle,output=${project.buildDir}/ktlint/report.xml",
                "--verbose"
        ]
    }

    ktlint.dependsOn(ktlintFormat)
    preBuild.dependsOn(ktlint)
}

As you can see, on pre-build, we run ktlintFormat first, then ktlint afterwards. This will ensure that the auto-format occurs, if possible, if not, the build will fail and generate an error report.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring and running Ktlin on Android Studio
First of all, you need to configure the classpath in top-level build.gradle file to use the plugin jlleitschuh/ktlint-gradle. classpath "org.jlleitschuh.gradle: ...
Read more >
Code Formatting in Kotlin using ktlint - GeeksforGeeks
run ./gradlew tasks from the command line and look for any ktlint tasks; try to run ... How to create project in Android...
Read more >
Adding ktlint to Your Kotlin Project - goobar
An introduction to formatting Kotlin code with ktlint. This post specifically focuses on adding ktlint to your Kotlin project.
Read more >
How to add ktlint in Android Project - Iqbal Ahmed
Use ktlint task in Android Studio. Open Run->Edit Configuration; Select your app -> Before launch; Add Run Gradle Task by pressing + button....
Read more >
Code Formatting in Kotlin using ktlint - MindOrks
In this blog, we will learn how to format your Kotlin code using ktlint ... So, you can write better and faster Android...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found