Spotless Linting Setup
This project uses Spotless for code formatting and license header management. Spotless is configured to work with Kotlin, Groovy, Gradle KTS files, and XML files.
Features
The Spotless configuration includes:
- Kotlin formatting using ktlint
- Customizable formatting using
ktlint
and the.editorconfig
file - Custom Compose UI rules for better Jetpack Compose code formatting
- License header management for all supported file types
- Gradle and XML file formatting
Using Spotless
IntelliJ/Android Studio Run Configurations
The project includes pre-configured run configurations in the .run
directory:
-
Spotless Check: Verifies if all files conform to the formatting rules
- Run using:
./gradlew spotlessCheck
- Or use the "Spotless Check" run configuration in Android Studio
- Run using:
-
Spotless Apply: Automatically formats all files according to the rules
- Run using:
./gradlew spotlessApply
- Or use the "Spotless Apply" run configuration in Android Studio
- Run using:
Tip
Always run Spotless Apply before committing your changes to ensure consistent code formatting.
CI/CD Integration
The project's GitHub Actions workflow automatically runs spotlessCheck
on pull requests. Your PR
will fail if there are any formatting issues.
Customizing Copyright Headers
The project includes default copyright headers in the spotless
directory:
copyright.kt
- For Kotlin filescopyright.gradle
- For Gradle filescopyright.kts
- For Kotlin Script filescopyright.xml
- For XML files
To use your own copyright headers:
- Navigate to the
spotless
directory - Replace the content of the copyright files with your own headers
- Maintain the proper file format for each type
Example copyright header for Kotlin files:
Note
Make sure to update all copyright files to maintain consistency across different file types.
Configuration Details
The Spotless configuration is defined in gradle/init.gradle.kts
and includes:
Kotlin Configuration
kotlin {
target("**/*.kt")
targetExclude("**/build/**/*.kt")
ktlint(ktlintVersion).editorConfigOverride(
mapOf(
"android" to "true",
),
).customRuleSets(
listOf(
"io.nlopez.compose.rules:ktlint:0.4.16",
),
)
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
}
Other File Types
The configuration includes similar setups for:
- Groovy (Gradle) files
- Kotlin Script (
.kts
) files - XML files
Important
The build task will fail if any files don't match the formatting rules. Always run spotlessApply
if spotlessCheck
fails.
Best Practices
-
Pre-commit Hook: Consider setting up a pre-commit hook to run
spotlessApply
: -
IDE Integration: Use the provided run configurations in Android Studio for easy access to Spotless commands.
-
Regular Checks: Run Spotless Check periodically during development, not just before commits.
-
Copyright Updates: Remember to update copyright years and organization information in the header files when necessary.
Tip
If you're using Android Studio, you can bind the Spotless Apply run configuration to a keyboard shortcut for quick formatting.
Further Reading
- Useful Tips & Tricks: Get useful tips for development and debugging
- CI/CD Setup: Set up continuous integration and deployment for the project