Consistent coding style using Checkstyle

Rahul Rengaramanujam
3xplore
Published in
2 min readDec 25, 2016

Enabling Checkstyle for your Android project

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

Below are the steps to enable Checkstyle for your android app using gradle which would help your team to follow a consistent coding style.

  1. Add checkstyle plugin to your gradle file under app folder:

apply plugin: ‘checkstyle’

2. Create a task in gradle to customize some Checkstyle attributes

task checkstyle(type: Checkstyle) {
source 'src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'

def configProps = ['proj.module.dir': projectDir.absolutePath]
configProperties configProps

// empty classpath
classpath = files()

showViolations true

reports {
xml.enabled false
html.enabled true
}
}

3. Add your checkstyle.xml as per your coding standards, the default location is

PROJECT_DIR/app/config/checkstyle/checkstyle.xml

4. Add the Checkstyle style to your gradle build task to run with every build.

preBuild.dependsOn('checkstyle')

When run successfully, you should be able to see the reports under the build folder.

Sample Project

References

  1. https://medium.com/@LiudasSurvila/writing-checkstyle-rules-with-android-studio-b1d31b30ca3a#.59ldfwwy1
  2. http://vincentbrison.com/2014/07/19/how-to-improve-quality-and-syntax-of-your-android-code/

--

--