Check Android code style and code smell using ktlint and detekt

Tram Ho

Have you ever been commented on just because your code format is different from the others? You have to edit changes code settings in Android Studio ? import other people’s settings into AS and do this again and again on different projects ? Really annoying!

In this article, I will introduce to you 2 tools that I often use in android code are ktlint and detekt to check code style , format code and code smell . The configuration is done directly on the code base and the results are exactly the same on different machines so it is very convenient. Posts mainly share code , config steps if there are any questions, please comment or contact , I will support.

Prepare

Things to keep in mind when getting started.

  1. The Android gradle project uses Gradle’s Kotlin DSL . If you use Groovy , do your own research and convert , I think it’s not too complicated.
  2. If your project has many module (remember to distinguish them from package ) then you need to implement them on all modules .

Install ktlint

Ktlint is Kotlin linter opensource to check and format Kotlin code style, convention . Purpose I used ktlint in the project is to help you help to config android studio code style settings dragged on just as before, and so simple, I almost used to format the standard pinterest launched, people can custom and it’s not too hard. Importantly code style rules should be done immediately at the init or start projects (probably Maintain source or new source).

No more rambling, let’s get started:

1. Go to github of ktlint and select the latest version

Declare in your dependency file (in my project is Configs.kt ), if you do not have this file , skip this step for now.

2. Create a source ktlint management file , temporarily called ktlint.gradle.kts

A few points to note in this pile of code :

  • I created 1 configurations to declare dependency named ktlint (like implementation , testImplementation or kapt , …)
  • Create 2 gradle task :
    • ktlintCheck : used to check Kotlin code style and convention . Here I just do the simple check and create the html report in the path app/build/reports/ktlint/ktlint.html
    • ktlintFormat : used to format the code automatically.
  • If you skip step 1, you can replace the dependencies as follows:

3. Go to the app/build.gradle.kts file to complete the final steps

  • Add references to the file ktlint.gradle.kts , note from , here I am to level with file build.gradle.kts .

4. Custom a few rules

If you feel it is necessary to fit your project or code style with you and your team members .

  • Create an .editorconfig file located in the outermost directory ( rootDir ) of the project. Edit the necessary config , see full instructions here . For my project, currently I only setup a little bit:

5. Test results

  • Run the command ./gradlew ktlintFormat after you finish coding to automatically format the code .
  • After running the command, usually the auto format will succeed. If there is a problem, you can go to app/build/reports/ktlint to view the report and fix it manually. According to my experience, 99.69% must be successful, the remaining percentage is due to the automatic format algorithm and there are a few minor errors and need to fix with rice (yaoming).

Report html format:

Install detekt

Meet detekt, a static code analysis tool for the Kotlin programming language. It operates on the abstract syntax tree provided by the Kotlin compiler.

Basically this is a linter , the analyzer is used to check code smell , create a report of code complexity like LOCs , cyclomatic complexity and number of code smell

detekt is also a wrapper based on ktlint so it can format code , but I used ktlint separately (always the latest) so I don’t want to use detekt for auto format .

And similar ktlint, I just apply at a fast, compact as possible, limiting change too much of detekt config available. You can change it to suit your project.

Let’s get started!

1. Setup dependency

Declaring dependency

In the app/build.gradle.kts file :

Note :

  • Detekt runs need jvmTarget 1.8
  • Note the input source directory of your project is src/main/java , your could be src/main/kotlin or something.

2. Generate default detekt config

Run the command ./gradlew detektGenerateConfig , this task will automatically generate the detekt.yml file containing all the default config rules .

3. Test results

  • Run the command ./gradlew detekt .
  • I think the project has implemented a long time ( maintain ), it will fail quite a bit. I recommend right now, do the following steps in the correct order:
    • Go to the report folder to see all the errors.
    • Fix heavy bugs about code smell , as much as possible because it’s not good for the project in nature.
    • Rerun and check the report to fix further if there are still many serious errors.
  • After fixing all the errors that should be fixed , if unable to pass , try editing some rules to suit the project – step 4.

4. Custom config rules

Important Note : Since these are standard and suitable configurations for most kotlin projects, please think before you change.

Ok, the truth is that if you apply detekt on a project for too long, it really is very difficult to do notes on, and another fact is some config in android other than Kotlin net, even so but Custom work should be kept as low as possible. The rule to change is:

Check for errors and make sure to fail in the report that it should be used in the project.

Open config/detekt.yml file and make changes. Here are the parts I have modified , you can refer to:

Conclustion

Above are all the basic steps to help the android ( kotlin ) project to increase a little quality in the implementation code stage of convention , code style , quality … Hope everyone applies successfully!

Source code reference:

Happy Coding!

Share the news now

Source : Viblo