Maintaining Code Style for Android App

Code Quality Sarcasm (Source:
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand”. — Martin Fowler.

If you have ever worked in a team on a project, you definitely know how important it is to understand the code of your teammates. Whenever you work on a project, it is very important that you and your team follow the same code style. Code Style (Programming Style) is a set of rules or guidelines used when writing the source code for program. It helps developers to read and understand code conforming to the style.

Learning to write code is easy but learning to write clean code is hard work. It requires more than just the knowledge of principles and patterns. You must sweat over it. You must practice it yourself, and watch yourself fail. You must watch others practice it and fail. You must see them stumble and retrace their steps. For more detailed explanation of “why code style is important”, you can read this article written by Cory Rylan.

However if you follow all the guidelines and set of rules to write your code, it is important to validate all the source code against the guidelines of your organisation/team before pushing the changes. As being an android developer, I will discuss how to do it in android project.

In this project, we will use Checkstyle tool which checks the java code automatically which makes it ideal for projects that uses a style guidelines.

How to use Checkstyle

First, you need to have a guidelines in xml format to use in Android Studio so that you can reformat your source code according to the guidelines. You can make your own set of rules for android studio but I’d like to prefer official android guidelines. There are other guidelines configuration file also available which you can download from the following links. Additional links related to the guide and tools are at the bottom of the articles.

  1. Google Java Style
  2. Android Style (I prefer this)
  3. Square Android Style

If you don’t know how to add style to your project in android studio, follow this link —How to Import The Official Android Code Style.

Based on the code style which you are following, create a checkstyle configuration file. The checkstyle configuration which I am using can be found at this github repo. You are free to modify the configuration of this file but make sure that it must be consistent with the code style which you or your organisation follow.

— In the root directory of your android project, create a folder named “config” . In this directory paste the “checkstyle-config.xml” which you have downloaded from above github link.

Now create a file named “style-script.gradle” in the same config directory and paste the code below-

I’m not going to explain how gradle tasks work because it is a beyond the scope of this article and surely I will write an article about Gradle also. After adding the style-script to your project, you need to apply that script to the build.gradle in app module. Add the following line in your app module build.gradle.

apply from: '../config/style-script.gradle'

Now you can build your project and run the command below in the terminal (make sure you are in root dir) to execute checkstyle task. (You can also run any task within android studio)

$ ./gradlew checkstyle

Note : Don’t forget to replace the ./gradlew with the gradle if you are using gradle instead of gradle-wrapper.

Upon execution of the above command, xml and html reports will be generated in the build directory of the app module.

root-project >> app >> build >> reports >> checkstyle >> checkstyle.html

Open the html file and you will see something similar to this.

Checkstyle html report generated after execution of checkstyle report

You can view the errors and change them accordingly. After following all the guidelines you will see no errors occurring and BUILD_SUCCESSFUL.

That’s all for this article. There are also some other tools which help to maintain the code style and quality throughout the life-cycle of your project. I will discuss all that tools in the upcoming articles.

Additional Links:

  1. Google Style Guide
  2. Android Code Style Guide
  3. Checkstyle Configuration Guide
  4. Checkstyle Gradle Plugin

I hope this article will help you to write a better code and make a good developer. Thanks for reading this article. Click ❤ below to recommend this article if you found helpful. It would let others to get this article in their feed. Also if you have any doubt or query, feel free to ask me.

For more articles about android and other tech, follow me, you will be notified.