Rahul RV
Rahul RV
Dec 25, 2016 · 2 min read

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/

3xplore

About Travel, Android Programming and my life experience in general

Rahul RV

Written by

Rahul RV

3xplore

3xplore

About Travel, Android Programming and my life experience in general

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade