Everything about Jococo and Sonarqube in Android

Hey there everyone, Long time no see.

First things first, In this blog, we will cover.

1) About Jacoco and SonarQube.

2) What are code coverage reports?

3) How can we easily integrate these into our android app?

About Jacoco and SonarQube

Jacoco is an open-source and free code coverage library which generates the report for both unit and instrumental tests. It covers all the project code base.

SonarQube is an open-source platform for automatically code reviewing with static analysis of code to detect bugs, code smells and security hotspots for more than 17 programming languages.

What are code coverage reports?

Code and test coverage reports are the key metrics in software testing and reviewing strategies. These metrics can help in deciding to check whether the developed software application is fulfilling the requirements or not.

Integration in Android App

Let’s start integrating Jococo into Android Project,

In the build.gradle root module directory,

build.gradle (top-level)

Don’t forget to add these to the dependencies in the top level as above. For the sonarqube properties, you can create them according to your Project. You can make the access token (sonar.login) in sonarqube after installing it in your system. For installation, you can follow the below-mentioned steps.

classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0"
classpath 'nl.neotech.plugin:android-root-coverage-plugin:1.5.3'

For the Jococo code coverage report generation, you can use the plugin used above. Setting up the configuration for Jococo for multi-module is not easy. Configuring Jacoco for such a case is not always straightforward. So we used this Android root coverage report library. This library works like a charm on AGP version 7.2.2 and can save a lot of time.

In the build.gradle (:app) module,

build.gradle (:app)

We have halfway reached. Hit the Sync with gradle, Once completed. Open up the terminal in Android Studio and execute this command

./gradlew :rootCoverageReport

Note: If the gradle wrapper is not there. You need to install gradle. You can download it from an package management system like Homebrew in MacOS or Chocolatey in Windows.

If installed, please install gradle by entering this command given below.

brew install gradle || choco install gradle

After the installation, in the Android Studio terminal:

gradle wrapper

It will create a wrapper file in the root of the project, Now you can again execute the command ./gradlew :rootCoverageReport in the terminal to generate Jococo report. Also, try to comment down any flavours you have added, It will show an error otherwise and ask for only debug build variant.

After the build is successful in the terminal. You will see something in the Project directory.

Jococo Report

We also need to install SonarQube in our system to be able to see the generated reports.

brew install sonarqube 
brew install sonar-scanner

After installation of the sonarqube you need to start the services. Please enter the command brew services start sonarqube in the terminal. You can create your credentials by hitting this URL: http://localhost:9000/ and proceed with creating a password.

Once you log into Sonarqube, Open Android Studio, hit this command in terminal

./gradlew sonarqube

It would successfully create the sonarqube report.

References

  1. SonarQube
  2. Jacoco
  3. Android Root Coverage Plugin.

Please feel free to reach out to me on LinkedIn and Twitter

Thanks for reading this. Happy Code Reviewing! :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store