I’ve been seeing this spotless in lots of open-source codes, and it seems it’s a fairly widespread coding model plugin for the Gradle construct system utilized in Android growth.
So I gave it a attempt to use it in my mission. Listed below are the steps I took so as to add this spotless plugin to my current initiatives, which encompass each Kotlin script (KTS) and Groovy construct Gradle recordsdata.
1. Add com.diffplug.spotless Plugin
Add com.diffplug.spotless
within the project-level construct.gradle/.kts.
KTS
plugins {
id("com.diffplug.spotless") model "6.19.0" apply false
}
Groovy
plugins {
id 'com.diffplug.spotless' model '6.19.0' apply false
}
2. Configure the Spotless
Add the next on the finish of your project-level construct.gradle/.kts.
KTS
subprojects {
apply(plugin = "com.diffplug.spotless")
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
goal("**/*.kt")
targetExclude("$buildDir/**/*.kt")
ktlint()
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
}
kotlinGradle {
goal("*.gradle.kts")
ktlint()
}
}
}
Groovy
subprojects {
apply plugin: 'com.diffplug.spotless'
spotless {
kotlin {
goal '**/*.kt'
targetExclude("$buildDir/**/*.kt")
ktlint()
licenseHeaderFile rootProject.file('spotless/copyright.kt')
}
groovyGradle {
goal '*.gradle'
greclipse()
}
}
}
Few notes:
-
goal("**/*.kt")
is required to incorporate all of the Kotlin recordsdata within the subdirectories.**
signifies the subdirectories. -
licenseHeaderFile
provides the copyright textual content header in each Kotlin file. Instance ofspotless/copyright.kt
file might be discovered right here. -
Needn’t explicitly use
**
ingoal("*.gradle.kts")
, it contains the subdirectories already.
3. Run the Spotless Code Formatter
Run the next instructions in your Android Studio terminal.
To examine with out making modifications:
.gradlew spotlessCheck
To examine and apply the modifications:
.gradlew spotlessApply
spotlessCheck is a bit ineffective to me. I normally simply run the spotlessApply straight.
It will not apply the change straight if it encounters errors. Among the errors I get are
Wildcard Imports is Used
rule: customary:no-wildcard-imports
Wildcard import
java.lang.AssertionError: Error on line: 5, column: 1
rule: customary:no-wildcard-imports
Wildcard import
No Whitespace Begin of Operate Physique
rule: customary:function-start-of-body-spacing
Anticipated a single white area earlier than begin of perform physique
java.lang.AssertionError: Error on line: 7, column: 57
rule: customary:function-start-of-body-spacing
Anticipated a single white area earlier than begin of perform physique
After you repair all of the errors, it applies the copyright header and default coding model to your recordsdata.
4. Automate spotlessApply in Construct
In the event you’re lazy to run .gradlew spotlessApply each time, you may automate it everytime you construct an Android mission.
Add this in your project-level construct.gradle/.kts file to run the spotlessApply activity earlier than the preBuild activity.
KTS / Groovy
subprojects {
afterEvaluate {
duties.named("preBuild") {
dependsOn("spotlessApply")
}
}
}
Points Confronted with Spotless
-
Official doc sucks – I observe the doc right here. No, it does not work!
-
One error at a time – No kidding, I’ve many wildcard imports in my mission. I’ve to repair every error and rerun the spotlessApply to get to the subsequent errors. Fortunately, I can search the file content material as a substitute of counting on the spotless.
-
Cannot disable the rule – I attempted, however I did not get it working. So I don’t know whether or not that is possible. That is no documentation on this in any respect.
Instance
GitHub Repository: Demo_CleanEmptyCompose