Android Studio's built-in "Reformat Code" formatter does not fully respect ktlint rules, even with .editorconfig settings. This causes issues like:
- Method chain dots moving to the beginning of lines instead of the end
- Incorrect blank line insertion (e.g., in
whenblocks) - Other subtle formatting differences that cause ktlint checks to fail
Android Studio uses IntelliJ's Kotlin formatter, which has its own opinions about code style. While .editorconfig can influence some settings, IntelliJ's formatter has limitations and cannot be fully aligned with ktlint, particularly for:
- Method call chain wrapping
- Blank line rules
- Some indentation edge cases
When working on a single file:
- Open the file in the editor
- Press
Cmd+Alt+L(Mac) orCtrl+Alt+L(Windows/Linux) - The ktlint plugin will assist with formatting
- This works better than bulk operations
For formatting multiple files or the entire project:
# Format all Kotlin files according to ktlint rules
./gradlew ktlintFormat
# Check formatting without making changes
./gradlew ktlintCheckIf you have the ktlint plugin enabled:
- Right-click on a file/folder in the Project view
- Select "Apply ktlint Format"
- Note: The plugin mode is set to MANUAL in
.idea/ktlint-plugin.xml
DO NOT select a folder and use "Code → Reformat Code"
- This uses IntelliJ's formatter, which conflicts with ktlint
- Will cause ktlint checks to fail
- Creates unnecessary changes that need to be reverted
In the commit dialog, DO NOT check "Reformat code"
- Go to Settings → Version Control → Commit
- Ensure "Reformat code" is unchecked
- Or if checked, understand it will use IntelliJ's formatter, not ktlint
- Work on your code normally
- Use
Cmd+Alt+Lto format individual files as you work - Before committing, run
./gradlew ktlintCheckto verify - If ktlint reports issues, run
./gradlew ktlintFormatto fix them
# Check if your code meets ktlint standards
./gradlew ktlintCheck
# If there are issues, auto-fix them
./gradlew ktlintFormat
# Then stage and commit your changes
git add .
git commit -m "Your message"A git pre-commit hook has been set up in .githooks/pre-commit that automatically runs ktlint before each commit:
- Runs
./gradlew ktlintCheck --daemonbefore allowing commits - If ktlint finds issues, the commit is blocked
- Provides instructions to fix issues with
./gradlew ktlintFormat - Can be bypassed (not recommended) with
git commit --no-verify
This ensures you never accidentally commit code that violates ktlint rules.
Setup:
The hook is configured via .githooks/setup.sh. If you cloned the repo and haven't set up hooks yet, run:
./.githooks/setup.shSee GITHOOKS.md for more details about all available git hooks.
The .editorconfig file has been updated with settings that attempt to align Android Studio's formatter with ktlint. However, these are best-effort only and cannot guarantee full compatibility.
The most important settings:
# Method call chain wrapping - keep dot at the end of the line
ij_kotlin_method_call_chain_wrap = off
# Blank lines in when blocks
ij_kotlin_blank_lines_around_block_when_branches = 0| Action | Tool to Use | ✅/❌ |
|---|---|---|
| Format single file | Cmd+Alt+L in editor |
✅ OK |
| Format multiple files | ./gradlew ktlintFormat |
✅ Recommended |
| Format entire project | ./gradlew ktlintFormat |
✅ Recommended |
| Reformat folder in IDE | Folder → Reformat Code | ❌ Avoid |
| Commit with "Reformat code" | Commit dialog checkbox | ❌ Avoid |
| Check formatting | ./gradlew ktlintCheck |
✅ Recommended |
# Check formatting (doesn't modify files)
./gradlew ktlintCheck
# Auto-fix formatting issues
./gradlew ktlintFormat
# Format only changed files (faster)
./gradlew ktlintFormat -PinternalKtlintGitFilter=trueIf you accidentally formatted with Android Studio's formatter:
- Revert the changes:
git checkout -- . - Run ktlint format:
./gradlew ktlintFormat - Review the proper changes:
git diff - Stage only the desired changes