Consistent coding style using Checkstyle
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.
- 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.
References