How to use the Kotlin Formatter
Format Kotlin code in three steps:
1
Paste or upload Kotlin code
Paste Kotlin source code into the input panel or upload a .kt or .kts file. Android, Spring, or KMP Kotlin code is all accepted.
2
Choose indent size
Select 2 or 4 spaces. The Kotlin Coding Conventions and ktlint both recommend 4 spaces; choose what matches your project.
3
Format, copy, or download
Click 'Format Kotlin' to apply ktlint-style indentation and Kotlin syntax highlighting. Copy the result or download as a .kt file.
When to use this tool
Use the Kotlin formatter when Kotlin code needs clean formatting before review or documentation:
- →Formatting Kotlin code from Android documentation, Google Codelabs, or GitHub that lost indentation
- →Cleaning up Jetpack Compose UI code from AI assistants before integrating into an Android project
- →Preparing Kotlin examples for Android developer blog posts, tutorials, or README files
- →Quickly reformatting a Kotlin data class, sealed class, or coroutine flow for a code review
- →Formatting Kotlin Multiplatform (KMP) shared module code before publishing
- →Tidying Spring Boot Kotlin controller or service code for technical documentation
Frequently asked questions
Q:Does Kotlin use K&R or Allman brace style?
Kotlin uses K&R style — opening braces appear on the same line as the declaration. This is the style specified in the official Kotlin Coding Conventions and enforced by ktlint. Unlike Java's Google Style Guide, there are no common Kotlin style variants that place opening braces on new lines.
Q:Does it support Jetpack Compose @Composable functions and annotations?
Yes — @Composable and other Compose-specific annotations (@Preview, @HiltViewModel, @Inject) are recognised as Kotlin annotations and highlighted in orange. Composable function bodies, remember blocks, LaunchedEffect, and side effect APIs are indented as standard Kotlin code blocks.
Q:How does it handle Kotlin coroutines and Flow?
Suspend functions, launch and async coroutine builders, withContext, flow builder blocks, and collect terminals are indented correctly as standard Kotlin block syntax. Flow, StateFlow, SharedFlow, Channel, Deferred, and Job are recognised as Kotlin types and highlighted in blue. String templates ($var and ${expr}) are highlighted in green.
Q:Can it format Kotlin data classes, sealed classes, and when expressions?
Yes — data class bodies (which may have custom methods and init blocks), sealed class hierarchies with multiple subclasses, and when expression branches are all indented correctly. Destructuring in for loops and function parameters is preserved as-is at the correct indent level.
Q:Does it work with Kotlin Multiplatform (KMP) code and .kts build scripts?
Yes — KMP shared code uses standard Kotlin syntax with expect/actual declarations, which are treated as regular class and function declarations in the formatter. Kotlin script files (.kts), including Gradle build scripts (build.gradle.kts), are valid Kotlin and are formatted identically to application code.
Q:What is the difference between ktlint and Detekt for Kotlin?
ktlint is a formatting-focused linter that enforces the official Kotlin Coding Conventions style rules — indentation, spacing, braces, and naming. Detekt is a static analysis tool that detects code smells, complexity issues, potential bugs, and security problems. They are complementary: ktlint ensures consistent style; Detekt ensures code quality. This formatter applies ktlint-style formatting rules.