Android Spotless Easy Setup

Setup spotless
in Android in 3 easy steps.
Step 1
Add ktlint
and spotless
plugin in the app build.gradle
file.
plugins {
id "com.android.application"
id "kotlin-android"
id "org.jlleitschuh.gradle.ktlint" version "<latest_version>"
id "com.diffplug.spotless" version "<latest_version>"
// Other plugins
}
You can find latest version of klint
and spotless
here,
Step 2
Create a new Gradle file at the project root level. It can have any name.
spotless.gradle
spotless {
// optional: limit format enforcement to just the files changed by this feature branch
ratchetFrom 'origin/main'
format 'misc', {
// define the files to apply `misc` to
target '*.gradle', '*.md', '.gitignore'
// define the steps to apply to those files
trimTrailingWhitespace()
indentWithSpaces() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
}
java {
// don't need to set target, it is inferred from java
// apply a specific flavor of google-java-format
googleJavaFormat('1.8').aosp().reflowLongStrings()
// make sure every file has the following copyright header.
// optionally, Spotless can set copyright years by digging
// through git history (see "license" section below)
licenseHeader '/* (C)$YEAR */'
}
}
Step 3
Use this new file in the app build.gradle
like this,
apply from: "$project.rootDir/spotless.gradle"
To run spotless
To check for issues
./gradlew spotlessCheck
To automatically fix issues
./gradlew spotlessApply