Format and Check Code Style
I want to match the coding style with Google Java Style Guide. I like to press Reformat Code button to auto format the current java file. And I need the CheckStyle plugin to help me check the style.
import XML file of Google Java style
- download the XML from
styleguide/intellij-java-google-style.xml
of Google's GitHub - go to Preferences
- go to Editor
- go to Code Style
- click the Manage button (which is right of Scheme text)
- click the Import button
- chose import from Intelljj IDEA code style XML
- select the
intellij-java-google-style.xml
file - click the Apply button
Now, I can open any Java class file, and press Reformat Code (Alt + Command + L) to auto format it to Google Java style.
install CheckStyle-IDEA plugin
- go to Preferences
- go to Plugins
- type CheckStyle-IDEA into search field
- press Browse repositories
- click the Install button of CheckStyle-IDEA plugin (version 5.10.1)
- restart Android Studio
- go to Other Settings in Preferences
- At Checkstyle page, set Google Checks bundled to be active
- Select the Scan Scope to be All files in project
Now, I can open Java class file, and press Check Current File button to analyse coding style.
Checkstyle found 165 item(s) in 1 file(s)
create Gradle task for check style
- download the
google_checks.xml
to${rootProject}/qualityTools/checkStyle.xml
- create
${rootProject}/qualityTools/quality.gradle
file
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
description 'check code standard'
group 'verification'
configFile rootProject.file('qualityTools/checkStyle.xml')
source 'src/main/java'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
classpath = files()
}
checkstyle {
toolVersion '8.2'
ignoreFailures = false
showViolations = true
}
- apply Gradle config to root
build.gradle
apply from: 'config/dependencies.gradle'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}
allprojects {
repositories {
jcenter()
}
ext {
compatibility = JavaVersion.VERSION_1_8
...
}
apply from: rootProject.file('qualityTools/quality.gradle')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Now, I can find the checkstyle task on each module's verification tasks group. I run the checkstyle task on root module, then I can find the report HTML and XML files in build/reports
folder of each modules.
/Users/HSY/Desktop/${projectName}/app/build/reports/checkstyle/checkstyle.html
CheckStyle Audit
Designed for use with CheckStyle and Ant.
------------------------------------------
Summary
Files Errors
116 9465
CheckStyle and Google style XML version is not matched issue
:app:checkstyle FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkstyle'.
> Unable to create a Checker: Property 'fileExtensions' in module Checker does not exist, please check the documentation
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1.911 secs
The CheckStyle version is not matched. I have to specified its version to the latest one.
checkstyle {
toolVersion '8.2'
}
Property does not exist error in checkstyle configuration file of Stack Overflow