Easy methods to Upload Spotless Code Formatter on your Android Undertaking?

I’ve been seeing this spotless in lots of open-source codes, and it seems this can be a lovely well-liked coding taste plugin for the Gradle construct machine utilized in Android building.

So I gave it a try to use it in my assignment. Listed below are the stairs I took so as to add this spotless plugin to my current tasks, which include each Kotlin script (KTS) and Groovy construct Gradle information.

1. Upload com.diffplug.spotless Plugin

Upload com.diffplug.spotless within the project-level construct.gradle/.kts.

KTS

plugins {
    
    identification("com.diffplug.spotless") model "6.19.0" observe false
}

Groovy

plugins {
    
    identification 'com.diffplug.spotless' model '6.19.0' observe false
}

2. Configure the Spotless

Upload the next on the finish of your project-level construct.gradle/.kts.

KTS

subprojects {
    observe(plugin = "com.diffplug.spotless")
    configure<com.diffplug.gradle.spotless.SpotlessExtension> {
        kotlin {
            goal("**/*.kt")
            targetExclude("$buildDir/**/*.kt")

            ktlint()
            licenseHeaderFile(rootProject.record("spotless/copyright.kt"))
        }

        kotlinGradle {
            goal("*.gradle.kts")
            ktlint()
        }
    }
}

Groovy

subprojects {
    observe plugin: 'com.diffplug.spotless'
    spotless {
        kotlin {
            goal '**/*.kt'
            targetExclude("$buildDir/**/*.kt")

            ktlint()
            licenseHeaderFile rootProject.record('spotless/copyright.kt')
        }

        groovyGradle {
            goal '*.gradle'
            greclipse()
        }
    }
}

Few notes:

  • goal("**/*.kt") is had to come with all of the Kotlin information within the subdirectories. ** signifies the subdirectories.

  • licenseHeaderFile provides the copyright textual content header in each and every Kotlin record. An instance of the spotless/copyright.kt record will also be discovered right here.

  • Do not want to explicitly use ** in goal("*.gradle.kts"), it contains the subdirectories already.

3. Run the Spotless Code Formatter

Run the next instructions on your Android Studio terminal.

To test with out making adjustments:

 .gradlew spotlessCheck

To test and observe the adjustments:

 .gradlew spotlessApply

spotlessCheck is somewhat pointless to me. I in most cases simply run the spotlessApply at once.

It would possibly not observe the trade at once if it encounters mistakes. One of the most mistakes I am getting are

Wildcard Imports is Used

rule: same old:no-wildcard-imports

Wildcard import

java.lang.AssertionError: Error on line: 5, column: 1

rule: same old:no-wildcard-imports

Wildcard import

No Whitespace Get started of Serve as Frame

rule: same old:function-start-of-body-spacing

Anticipated a unmarried white area earlier than beginning of operate physique

java.lang.AssertionError: Error on line: 7, column: 57

rule: same old:function-start-of-body-spacing

Anticipated a unmarried white area earlier than beginning of operate physique

After you repair all of the mistakes, it applies the copyright header and default coding taste on your information.

4. Automate spotlessApply in Construct

In case you are lazy to run .gradlew spotlessApply each and every time, you’ll automate it on every occasion you construct an Android assignment.

Upload this on your project-level construct.gradle/.kts record to run the spotlessApply process earlier than the preBuild process.

KTS / Groovy

subprojects {
    
    afterEvaluate {
        duties.named("preBuild") {
            dependsOn("spotlessApply")
        }
    }
}

Problems Confronted with Spotless

  • Reputable file sucks – I practice the document right here. No, it does not paintings!

  • One error at a time – No kidding, I’ve many wildcard imports in my assignment. I’ve to mend each and every error and rerun the spotlessApply to get to the following mistakes. Fortuitously, I will seek the record content material as a substitute of depending at the spotless.

  • Can not disable the rule of thumb – I attempted, however I did not get it running. So I do not know whether or not that is possible. That is no documentation in this in any respect.

Instance

GitHub Repository: Demo_CleanEmptyCompose

You May Also Like

More From Author

+ There are no comments

Add yours