Thursday, 11 December 2014

Configure Jenkins for Android - CheckStyles

Google has a very nice coding style guide published in the web page. It is called "Code Style Guidelines for Contributors" and it could be found here.

Jenkins could be configured to do automatic coding style check on each run. Here is a short guide about how to do it.

1. Download and get the jar
1.1 Go to the web page of Check style in SourceForge:


1.2 Download the jar folder and remember where is.

2. Configure the Jenkins for CheckStyle plugin
2.1 Opens Jenkins
2.2 Go to Manage Jenkins -> Manage plugins
2.3 From the Available tab, look for a plugin called "CheckStyle Plugin". Download and install it. Make sure that another plugin called "Static Analysis Utilities" is also installed.

3. Configure the project
3.1 From the test project, add the follow line in the ant.properties

checkstyle.jar=/usr/lib/checkstyle/checkstyle-6.1.1-all.jar

In my case the plugin is stored in the /usr/lib/checkstyle folder. Change it to the folder where your jar file is.

3.2 Download the google_check.xml from the follow url and place it in the root folder of the test project.


3.3 Add the follow line at the end of the build.xml in the test project, just before the line which ends with </project>

    <taskdef classpath="${checkstyle.jar}" resource="checkstyletask.properties" />
    <target name="checkstyle">
        <checkstyle config="google_checks.xml">
           <formatter type="xml" tofile="checkstyle-result.xml"/>
           <fileset dir="${tested.project.dir}/src" includes="**/*.java"/>
       </checkstyle>
    </target>

The code above creates a new target called checkstyle, which when runs, it gets the file called "google_checks.xml", which should be in the same folder as the build.xml is, and check all the java files in the source folder of the project to be test. Make sure you have set correctly the variable for "test.project.dir" in the ant.properties file.

4. Config the job
4.1 On Jenkins, go to the configuration of the project
4.2. On the Build section, add the target "checkstyle" in the Invoke Ant part. The mine looks like this:

uninstall clean emma debug checkstyle install test

*If you are not using emma, just remove the target emma.

4.3 On the Post-build Actions section, add a new action called "Publish CheckStyle analysis results"
4.4 Enter the path for the checkstyle-result.xml. It should be something like:

TestProject/checkstyle-result.xml

Where "TestProject" is the name of the testing project, not the source project.

Finally, to check the result, on the left side of the project, along with "Build", "Configure" and other options, there should be a new option called "Checkstyle Warnings", where you can see all the warnings about the coding style.

Happy coding!

Source
Jenkins - Building an Android app and test project
Github - CheckStyle

No comments:

Post a Comment