Part 1: High-quality android code with analysis tools : CheckStyle

Thientvse
tvsoft
Published in
3 min readSep 7, 2017

Today we’ll learn about how to improve high-quality Android code in our projects using some code analysis tools for Java. Example as : CheckStyle, FindBugs, PMD and Android Studio Lint — all of them free and open source.

What are code analysis tools?

There are tools that parse and analyze your soure code without execute it. It can help you to find potential bug, security error. You can create a set of rules and add to your project, if your source code don’t follow these rules, it will alert for you and you can fix it.

Benefits

  • Improve quality source code
  • Defines project rule.
  • Improve knowledge of a language
  • Detect potential bugs.

Setup

You create a folder code_quality_tools, it contains the XML file for the code analysis tools, and you create quality.gradle file:

Then you open your build.gradle in the app module folder and include this line at the end of the file:

apply from: '/code_quality_tools/quality.gradle'CheckStyle
You create a XML file and name it checkstyle.xml, it will contain a set of rule about conventions or coding standards. You can custom
a checkstyle file as you needs. For example, CheckStyle can check the constant name (final, static or both). If contain name don't follow
these rules, the problem will be flagged in the report.
// incorrect
private final static String myConstant = "myConstant";

// correct
private final static String MY_CONSTANT = "myConstant";

You open checkstyle.xml file:

<?xml version="1.0"?><!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="FileTabCharacter"/>
<module name="TreeWalker">

<!-- Checks for Naming Conventions -->
<!-- See http://checkstyle.sourceforge.net/config_naming.html -->
<module name="MethodName"/>
<module name="ConstantName"/>

<!-- Checks for Imports -->
<!-- See http://checkstyle.sourceforge.net/config_imports.html-->
<module name="AvoidStarImport"/>
<module name="UnusedImports"/>

<!-- Checks for Size -->
<!-- See http://checkstyle.sourceforge.net/config_sizes -->


<!-- other rules ignored for brevity -->
</module>

<module name="CatchParameterName">
<property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
</module>

<module name="ConstantName">
<property name="format"
value="^log(ger)?|[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
</module>
You can refer the rule at the website :

http://checkstyle.sourceforge.net

Then, you open quality.gradle:

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle, dependsOn: "assembleDebug") {
description 'Checks if the code meets standards'
group 'verification'

configFile file('./code_quality_tools/googlecheckstyle.xml')
source 'src'
include '**/*.java'
exclude '**/gen/**'

classpath = files()
ignoreFailures = false
}

Finally, you run gradle script:

After the task has finished running, a report will be generated, which is available at app module > build > reports > checkstyle. You can open checkstyle.html to view the report.

In the next post, we will learn about other analysis tools such as : PMD, FindBus, Lint.

Thanks for reading this article. Be sure to recommend as much as you can and also share with your friends. It means a lot to me. For more about programming, follow me and thientvse.com , so you’ll get notified when we write new posts. References:

Originally published at Code for fun.

--

--