Skip to main content
The project enforces consistent Kotlin code style using KtLint with automated formatting and validation integrated into the build process.

KtLint configuration

Plugin setup

KtLint is configured at the project level in build.gradle.kts:1 and applied to all modules:
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType

plugins {
    alias(libs.plugins.ktlint) apply false
}

allprojects {
    apply(plugin = "org.jlleitschuh.gradle.ktlint")
    extensions.configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
        android.set(true)
        ignoreFailures.set(false)

        reporters {
            reporter(ReporterType.PLAIN)
            reporter(ReporterType.CHECKSTYLE)
            reporter(ReporterType.SARIF)
        }
    }
}

Plugin version

The project uses KtLint plugin version 13.1.0 (gradle/libs.versions.toml:44):
[versions]
ktlint = "13.1.0"

[plugins]
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
KtLint is configured with ignoreFailures = false, meaning builds will fail if style violations are detected.

EditorConfig

The project uses .editorconfig to customize KtLint rules and IDE behavior.

Current rules

The configuration in .editorconfig:1 sets the following rules:
[*.{kt,kts}]
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true

ktlint_standard_trailing-comma-on-call-site = disabled
ktlint_standard_trailing-comma-on-declaration-site = disabled
ktlint_standard_max-line-length = disabled
ktlint_standard_import-ordering = disabled
ktlint_function_naming_ignore_when_annotated_with = Composable

Rule explanations

Disabled rules:
  • ktlint_standard_trailing-comma-on-call-site
  • ktlint_standard_trailing-comma-on-declaration-site
Trailing commas are allowed but not enforced, giving developers flexibility:
// Both styles are acceptable
val list = listOf(
    "item1",
    "item2",
    "item3",
)

val list2 = listOf(
    "item1",
    "item2",
    "item3"
)
Disabled rule: ktlint_standard_max-line-lengthMaximum line length is not enforced, allowing longer lines when necessary for readability:
// Long lines are acceptable when they improve readability
val navigationItem = NavigationItem(title = "Home", icon = Icons.Default.Home, route = "home_screen")
While not enforced, aim to keep lines under 120 characters when practical.
Disabled rule: ktlint_standard_import-orderingImport ordering is not enforced by KtLint. The IDE handles import organization based on Kotlin official style.
Custom rule: ktlint_function_naming_ignore_when_annotated_with = ComposableComposable functions can use PascalCase naming (capitalized), which is standard for Compose:
@Composable
fun HomeScreen() {  // PascalCase is allowed for @Composable functions
    // Composable content
}

fun processData() {  // camelCase required for regular functions
    // Regular function
}

Code formatting

Automatic formatting

Format all code automatically:
./gradlew ktlintFormat
The ktlintFormat task automatically fixes most style violations.

Check code style

Validate code style without making changes:
./gradlew ktlintCheck

Output reports

KtLint generates three types of reports:
  1. PLAIN - Human-readable console output
  2. CHECKSTYLE - XML format for CI tools
  3. SARIF - Static analysis format for GitHub, Azure DevOps
Reports are located at:
module/build/reports/ktlint/
├── ktlintMainSourceSetCheck.txt
├── ktlintMainSourceSetCheck.xml
└── ktlintMainSourceSetCheck.sarif

Pre-commit hooks

Automatic enforcement

KtLint checks are automatically run before every build via pre-build hooks in app/build.gradle.kts:48:
tasks.named("preBuild").configure {
    dependsOn("ktlintFormat")
    dependsOn("ktlintCheck")
}
This ensures:
1

Format code

ktlintFormat automatically fixes style violations
2

Validate style

ktlintCheck verifies all code meets style requirements
3

Compile code

Build proceeds only if style checks pass
Because ignoreFailures = false, builds will fail if KtLint finds unfixable style violations. Fix these issues before committing.

Manual hooks

You can also run checks manually before committing:
./gradlew ktlintFormat ktlintCheck
git add .
git commit -m "Your commit message"

IDE integration

Android Studio / IntelliJ IDEA

Configure your IDE to use the project’s code style:
1

Apply EditorConfig

The IDE automatically detects .editorconfig and applies settings to Kotlin files.
2

Set Kotlin code style

The project uses official Kotlin style (gradle.properties:19):
kotlin.code.style=official
This is automatically applied to all modules.
3

Format on save (optional)

Enable automatic formatting in IDE settings:Settings → Tools → Actions on Save
  • ☑ Reformat code
  • ☑ Optimize imports

KtLint IDE plugin

Install the KtLint plugin for real-time feedback:
  1. Open Settings → Plugins
  2. Search for “ktlint”
  3. Install ktlint (unofficial) plugin
  4. Restart IDE
The plugin highlights style violations as you type.

Code style guidelines

Kotlin official style

The project follows the Kotlin official coding conventions:
// Classes: PascalCase
class ArticleRepository

// Functions: camelCase (except @Composable)
fun fetchArticles()

// Composables: PascalCase
@Composable
fun HomeScreen()

// Constants: UPPER_SNAKE_CASE
const val MAX_RETRY_COUNT = 3

// Properties: camelCase
val userName: String
// Use 4 spaces for indentation
class Example {
    fun method() {
        if (condition) {
            doSomething()
        }
    }
}

// Spaces around operators
val sum = a + b

// No space before colon in type declarations
val name: String

// Space after colon in type declarations
fun method(): String
// Short functions on single line
fun double(x: Int): Int = x * 2

// Multiple parameters: wrap after opening paren
fun longFunction(
    parameterOne: String,
    parameterTwo: Int,
    parameterThree: Boolean,
) {
    // Function body
}
// Composables use PascalCase
@Composable
fun ArticleCard(
    article: Article,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,  // Modifier last with default
) {
    // Composable body
}

// Remember followed by mutable state
val expanded by remember { mutableStateOf(false) }

Troubleshooting

Build fails due to KtLint

If the build fails with KtLint violations:
  1. Run auto-format:
    ./gradlew ktlintFormat
    
  2. Check remaining issues:
    ./gradlew ktlintCheck
    
  3. Review the report:
    app/build/reports/ktlint/ktlintMainSourceSetCheck.txt
    
  4. Fix manual issues that auto-format couldn’t resolve

Disable specific rules

To disable additional rules, add them to .editorconfig:
[*.{kt,kts}]
ktlint_standard_<rule-name> = disabled
Only disable rules when absolutely necessary. Consistent style improves code maintainability.

Ignore specific violations

Use suppression annotations for exceptional cases:
@Suppress("FunctionName")
fun `test with spaces in name`() {
    // Test implementation
}

Clear KtLint cache

If KtLint behaves unexpectedly:
./gradlew cleanKtlint
./gradlew ktlintCheck

Continuous integration

CI pipeline integration

KtLint checks run automatically in CI via the pre-build hooks. For explicit CI validation:
# GitHub Actions example
- name: Run KtLint
  run: ./gradlew ktlintCheck

- name: Upload reports
  uses: github/codeql-action/upload-sarif@v2
  with:
    sarif_file: app/build/reports/ktlint/ktlintMainSourceSetCheck.sarif

PR validation

Add KtLint checks to pull request workflows:
- name: Format code
  run: ./gradlew ktlintFormat

- name: Check for changes
  run: |
    git diff --exit-code || (
      echo "Code is not formatted. Run ./gradlew ktlintFormat"
      exit 1
    )

Best practices

Format before committing

Always format code before committing:
./gradlew ktlintFormat
git add .
git commit -m "feat: add new feature"

Review auto-formatting

Review changes made by ktlintFormat before committing to ensure they’re correct.

Team consistency

Ensure all team members:
  • Use the same IDE settings
  • Have EditorConfig support enabled
  • Run KtLint before pushing code

Update KtLint regularly

Keep KtLint up to date for the latest rules and fixes:
# gradle/libs.versions.toml
[versions]
ktlint = "13.1.0"  # Update version here

Next steps

Building

Learn about build configuration and Gradle tasks

Testing

Write unit and instrumented tests

Build docs developers (and LLMs) love