Guarantee Android Code High quality: Use ktlint in your Kotlin Tasks | by Paul Knulst | Feb, 2023 | Videogame Tech

roughly Guarantee Android Code High quality: Use ktlint in your Kotlin Tasks | by Paul Knulst | Feb, 2023 will cowl the newest and most present instruction re the world. retrieve slowly because of this you comprehend with out issue and appropriately. will addition your data cleverly and reliably


Ensure Android code quality: use ktlint in your Kotlin projects
Picture by Kevin Ku/Pexels

Kotlin is the first language advisable by Google for Android utility improvement. In comparison with Java, it’s going to make life simpler for Android app builders, however if you’re new to this subject, it’s possible you’ll not know find out how to format your code appropriately.

As an alternative of beginning to analysis and study particular guidelines for writing clear code utilizing Kotlin, you need to use ktlintwhich was developed for this explicit goal:

ktlint is a Kotlin anti-bikeshedding linter with a built-in format

💡 Be aware: ktlint is an open supply mission maintained and utilized by Pinterest that may be discovered and downloaded on GitHub:

Which means it checks the type of the code and helps to format the code by making computerized adjustments. This can end in extra readable and comprehensible code.

Advantages of utilizing ktlint:

  • ktlint can save time
  • ktlint will save vitality by avoiding checking code type manually
  • ktlint simplifies the formatting course of

ktlint It may be divided into two important duties:

  1. ktlint is a lint instrument: Fraying carried out by ktlint is predicated on the official Kotlin normal type information. Which means it’s going to validate the code and examine if it adheres to the official Kotlin type guides.
  2. ktlint is a formatter: If issues are discovered within the code utilizing ktlintit’s potential to run the ktlint format to robotically repair these issues.

Additionally, ktlint It may be put in and utilized in two other ways:

  1. As a gradle job utilizing ktlint-gradle. This job can be utilized in CI/CD pipelines or pre-commit/pre-insert hooks to robotically examine and repair your code.
  2. As an Android Studio plugin to permit computerized integration of the IDE into your improvement machine.

To implement ktlint in any Android app, will probably be added to the mission utilizing the ktlint-gradle plug. It is a separate third get together plugin along with the essential one ktlint instrument. It would will let you work with ktlint by offering Gradle duties that internally execute ktlint instructions.

To permit ktlint in an android mission, the gradle plugin must be added to the basis degree construct.gradle archive:

id("org.jlleitschuh.gradle.ktlint") model "11.1.0"

// Required to obtain KtLint

If the Android app makes use of the DSL plugin, the Gradle plugin will probably be added otherwise:



// Required to obtain KtLint

apply(plugin = "org.jlleitschuh.gradle.ktlint")

💡 Be aware: Please notice that this tutorial is written with the present model 11.1.0 of the ktlint-gradle plugin!

For those who work with a number of submodules inside an Android mission, the ktlint-gradle the plugin must be added to the allprojects... root degree block construct.gradle archive:

// root-level construct.gradle
apply plugin: "org.jlleitschuh.gradle.ktlint"

After setting the ktlint-gradle plugin, format and lint duties at the moment are obtainable within the Android app.

There are two methods to seek out out which duties will be run within the app:

  • Run ./gradlew duties within the CLI and discover any ktlint job that may be executed
  • Use the Gradle instrument in Android Studio to listing all obtainable duties

To examine the code with ktlintthe next command will be run within the CLI or chosen from the Gradle instrument:

./gradle ktlintCheck

This command will undergo the mission and report any errors discovered utilizing the default worth ktlint-gradle setting.

Automated correction of any errors reported by ktlintCheck It may be achieved by operating the next:

./gradle ktlintFormat

This command will try to repair all errors and report another errors that would not be fastened robotically.

After operating each instructions, your code must be optimized when it comes to the Kotlin type information.

Though there are numerous normal guidelines utilized by default by ktlintit’s potential to switch or specify guidelines to adapt to any want:

  • don’t have any new line on the finish of a file
  • indent dimension of two (as an alternative of 4)
  • disable any rule

To use these guidelines, create a brand new .editorconfig file within the root listing of the mission and specify every little thing. ktlint-gradle it’s going to use this setting each time it runs a code examine.

The subsequent .editorconfig applies beforehand outlined guidelines and specifies that ktlint ought to ignore the no-wildcard-imports ruler:

indent_size = 2
indent_style = house
insert_final_newline = true


As an alternative of manually putting in ktlint-gradle and utilizing a Gradle job to run it, you need to use Android Studio to put in an unofficial plugin from the JetBrains market:

This plugin will absolutely combine into your IDE and internally use ktlint guidelines for formatting your code.

Set up will be achieved utilizing the Android Studio plugin supervisor. Open the Plugins menu by deciding on File -> Settings -> Plugins and seek for ktlint:

Ensure Android code quality: use ktlint in your Kotlin projects
Picture of Android Studio plugins part used to put in Ktlint

Then, set up the plugin, restart your IDE and open File -> Settings -> Instruments -> ktlint, and modify the settings to your wants:

Ensure Android code quality: use ktlint in your Kotlin projects
Picture of Android Studio Instruments part used to configure Ktlint

now for those who press CTRL+ALT+L will robotically format your code and apply all ktlint guidelines.

I hope this text has helped present you find out how to implement ktlint in your Android initiatives utilizing the Android Studio plugin or the Gradle job and taking full benefit of its options.

Implementing it in Android Studio is simple and must be achieved by each Android app developer to optimize the standard of their code. Additionally, utilizing ktlint-gradle As a part of your CI/CD pipeline, it ensures that your code stays compliant with Kotlin’s coding requirements at the same time as your group grows and evolves.

Carrying ktlint It can save you time and assets by stopping potential bugs and lowering the time spent on code assessment. Additionally, general code high quality will probably be optimized.

To additional optimize the general Android code high quality and velocity up the app improvement course of, you’ll be able to consult with the next two tutorials:

  1. Enhancing the code high quality of Android purposes with SonarQube and Gradle Detekt
  2. Streamline Android app improvement with Docker, SonarQube, Detekt and MobSF

Lastly, what do you concentrate on ktlint and use it to optimize code high quality? Are you keen to use it to your mission? Additionally, do you’ve got any questions on find out how to implement it? I might love to listen to your ideas and reply your questions. Please share every little thing within the feedback.

Be at liberty to attach with me on my weblog, LinkedIn, Twitterand GitHub.

Thanks for studying, and glad coding!

I want the article not fairly Guarantee Android Code High quality: Use ktlint in your Kotlin Tasks | by Paul Knulst | Feb, 2023 provides perspicacity to you and is beneficial for including collectively to your data


Ensure Android Code Quality: Use ktlint in your Kotlin Projects | by Paul Knulst | Feb, 2023