I'd like to set up our project in a way that ensure that coding style and being warning and lint-free is enforced for checkins. I'd also like to make it super easy for developers to see when they're not in compliance.
Ideally this would be presented from within Android Studio (all our devs are using the same IDE) when you edit a file or run a build. Additionally, it would be nice to have this enforcement "just work" when a developer clones the repo, rather than requiring any additional manual setup.
What's the cleanest way to do this?
Static code analysers like Checkstyle, FindBugs and PMD may help you.
They can be configured to use with Gradle and Android Studio with help of these scripts. If somethings wrong according to analysers' configs build will fail.
As these scrips are integrated with Gradle, thay can be commited to repo and will work when developer clones it.
Unfortunately generated reports are not integrated with Android Studio, so better way to build and run checks from console using gradle wrapper. Static analysers' tasks are configured to depend on check
task, so ./gradlew check
command will run them all.
Reports are located in $project.buildDir/reports
directory, but it can be changed as well.
Checkstyle configurations are very flexible. They can be configured to any code style you prefer. For example, Google Java Code Checkstyle config.
PMD has ruleset to run checks over your code. It also has android rules. Here is a guide how to make a ruleset configuration for PMD.
FindBugs helps you find common bugs in Java code. E.g. it detects multiple strings concatenation using +
and suggests to use StringBuilder. It can be configured with filter files.
Also do not turn off lint check entirely like this:
lintOptions {
abortOnError false
}
A better way is to disable specific checks if you don't like them, e.g.:
lintOptions {
disable 'InvalidPackage'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With