This documentation provides basic knowledge on what Ktlint is and how it works.
Ktlint serves the following purposes in the project:
-
Code Consistency: Enforces consistent coding styles across the project, which makes the codebase more readable.
-
Automated Code Review: Facilitates automated code review by highlighting style violations, making it easier to catch and address issues early in the development process.
-
Team Collaboration: Establishes a shared code style among team members, reducing friction and enhancing collaboration.
-
Build Process Integration: Seamlessly integrates into your project's build process, allowing for automated code style checks and formatting each time you build and run the application.
Since Ktlint is already implemented, we will go over how it works.
-
The Ktlint plugin inside project level build.gradle
-
Include the Ktlint Gradle plugin in your project's
build.gradle
file:plugins { id 'org.jlleitschuh.gradle.ktlint' version '11.3.1' }
-
Replace "11.3.1" with another version of the Ktlint Gradle plugin in the future if needed.
-
-
The Ktlint plugin inside module (app) level build.grade file
-
Include the Ktlint Gradle plugin to your Kotlin modules:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'org.jlleitschuh.gradle.ktlint' version '11.3.1' }
-
-
The Ktlint configuration
-
The Ktlint configuration can be seen down below and can be modified according to your code current code standard. It is however not advised to remove anything from disabledRules since Ktlint cannot auto-correct those errors.
ktlint { android = true ignoreFailures = false disabledRules = [ 'final-newline', 'no-wildcard-imports', 'max-line-length' ] reporters { reporter 'plain' reporter 'checkstyle' reporter 'sarif' } }
-
Auto-checks and Auto-formatting the code
Ktlint is currently set-up in a way such that it will Auto-check and Auto-format all the existing code when you either build or run the application. If you however want to manually do checks or format you can follow these steps:
-
Check for Violations:
To automatically check for code style violations:
- Open the terminal inside Android Studio
- Run the following command:
./gradlew ktlintCheck
This task will report any style violations found in the project.
-
Auto-formatting Code:
To automatically format the code according to the defined style rules:
- Open the terminal inside Android Studio
- Run the following command:
./gradlew ktlintFormat
This task modifies the code in-place to comply with the specified code style.
- The preBuild task
The line tasks.getByPath('preBuild').dependsOn('ktlintFormat')
inside the Module (app) level build.gradle
file specifies that Ktlint will check and format all the code when you build or run the application.
If any major code standard violations are present your build will fail.
Ktlint uses sensible default rules, but you can customize them as needed. To view the default rules, refer to the official documentation.
- Modify rules by configuring
build.gradle
at both Project and Module (app) levels:
It is not advised to remove any of the current rules since they cannot be auto-formatted. But if you want to enforce the rules anyway you can remove them from the 'disabledRules' array.
ktlint {
disabledRules = [
'final-newline',
'no-wildcard-imports',
'max-line-length'
]
}
This documentation covers the essential aspects of integrating and using Ktlint in your project. If you encounter any issues or have additional questions, refer to the official Ktlint GitHub repository or seek assistance from your project's development community.