Android and SonarQube with code coverage

With this post we are going to walkthrough how to setup SonarQube with code coverage for a Android project.

During this walkthrough we make use of the following system and software:

  • Android Studio, version 4.1.1
  • Gradle, version 6.5
  • JDK / java, java 15.0.1 2020-10-20
  • JaCoCo, version 0.8.6
  • SonarQube, version 8.5.1
  • SonarQube scanner, version 2.7.1

Example Android project

For this walkthrough there is an example project based on the template Navigation Drawer Activity where we added a data class Car.java with some unit tests in CarTest.java. This project is available on GitHub project with all code already inside!


All important code changes can be found in below gradle files:

  • Project build.gradle: Added dependencies for both org.jacoco:org.jacoco.core and org.sonarsource.scanner.gradle:sonarqube-gradle-plugin
  • Module build.gradle: Applied to both jacoco.gradle and sonarqube.gradle
  • jacoco.gradle: Create this file to handle most JaCoCo configurations
  • sonarqube.gradle: Create this file to handle most SonarQube configurations

Android unit test with code coverage

For this example we have added Car.java and CarTest.java in the package com.example.android_sample_app.data.
To enable code coverage for any project execute the following steps;

  • Project build.gradle: Added dependencies for both classpath "org.jacoco:org.jacoco.core:0.8.6".
  • jacoco.gradle:Add this file into the project.
  • Module build.gradle: Apply jacoco.gradle and set android > buildTypes > debug > testCoverageEnabled on true.
    See below code sample.

When all above steps are executed sync the gradle project and run below command. This will run all Unit tests with code coverage, to see the coverage of the current project go to [project location]/android-sample-app/app/build/reports/jacocoTestReport/html/index.html

gradlew clean testDebugUnitTest

What is SonarQube?

If you have not already setup SonarQube you can follow those instructions at this post. We are running SonarQube at http://localhost:9000/. if you are running SonarQube on a different url, you can change the path in sonarqube.gradle.

Why should we use SonarQube?
Every developer should execute a code review, but what if we also run an automated code review? Checking for bugs, vulnerabilities, code smells and all of these issues can be found with a manual code review, but would it not be great if that's already covered by an automated system? See it as an extra layer of quality to write the best code possible.

If you feel you don't need it, I would challenge you to run SonarQube on a recent project to see it for your self!

Depending on the SonarQube setup, it can even fail a CI (Continuous Integration) job, upon an increasement of quality issues or decrease code coverage.

How to configure SonarQube?

We need to execute 2 different steps to add our Android project to SonarQube.

  • #1: Create a new project in SonarQube
  • #2: Configure SonarQube inside the Android project

For the first step we need to navigate to SonarQube inside a browser. In our case we navigate to SonarQube Projects page .

  • Enter in field Project key the project key, in this example android-sample-app
  • Enter in field Display name the project name, in this example android-sample-app
  • Click on Set up
  • SonarQube will provide a generated Token, this token is used as sonar.login value in the second step
  • Click on Java
  • Click on Gradle
  • SonarQube displays how to configure SonarQube within the Java Gradle project.
  • Keep this browser page open, to have a details available

As second step we need to update the Android project.

  • Project build.gradle: Added dependencies for both classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7.1".
  • sonarqube.gradle:Add this file into the project.
  • Module build.gradle: Apply sonarqube.gradle.
  • sonarqube.gradle: Update the property sonar.login with the generated token.
  • Optional update also the properties sonar.host.url, sonar.projectKey and/or sonar.projectName depending on youre setup

Below is the sonarqube.gradle listed. When you at the properties sonar.coverage.jacoco.xmlReportPaths has the value "**/jacocoTestReport/jacocoTestReport.xml". This connects the generated JaCoCo test reports with SonarQube, so that they also become available inside SonarQube.

Execute gradle with SonarQube

When all steps are executed, sync the gradle project and run below command.

As all parameters are defined within sonarqube.gradle, we can just call gradlew sonarqube to execute the SonarQube logic. To chain all events we can directly call gradlew testDebugUnitTest sonarqube to execute all unit tests with code coverag and execute SonarQube on the local code base.

if you have any issues you can always add --info, for logging information printed to the console.

# Clean, run all unit test and execute SonarQube
gradlew clean testDebugUnitTest sonarqube

# With extra logging
gradlew clean testDebugUnitTest sonarqube --info
        

Verify SonarQube

At this stage you have used SonarQube to analyze the first project! Go directly to http://localhost:9000/projects and see the results of the project. Click through the environment of SonarQube to understand the different features.

In the post we have mentioned code coverage. To verify that feature is function, open the SonarQube project and navigate to the Code tab. Navigate to src/main/java/com/example/android_sample_app, data and Car.java to view the code coverage for the file Car.java.

Links

With this guide you have configured SonarQube with code coverage for an Android project. If you have any questions or feedback, we like to hear it in the comments!

Thank you for reading this article, If you liked the article, help others find this article by sharing it.

Reacties

  1. Hi, after running your sample code, I got: com.android.build.gradle.internal.testing.ConnectedDevice > No tests found.
    Same am my own project after configurations.
    Any help on what might be wrong ?

    BeantwoordenVerwijderen

Een reactie posten

Populaire posts van deze blog

Android-x86 virtual machine configuration tips

Setup SonarQube with Docker