Setup Jacoco & Sonarqube in multimodule multiflavor Kotlin Android project

Photo by Marcin Jozwiak on Unsplash

Prelude

The day has come at last! The day, when you are going to setup SonarQube in your Android project. And again, you are relying on google to lend you a helping hand, but this time it might not come that easy, because there is no copy-paste solution to take from the beloved stackoverflow.

  • Few examples for Android projects and if you find some, they would mostly be related to Java or would be using SonarQube properties that are already obsolete (e.g. sonar.jacoco.reportPaths)
  • If you are lucky to find examples on Kotlin, then certainly they are solutions for single module projects
  • or maybe I’m extra bad at googling 🤷‍♂️

Agenda

We’ll first setup JaCoCo — a tool, which will help us generate coverage report for the codebase. This report will contain detailed information as to which classes/paths have been covered by tests, and which are not covered.

Setting up JaCoCo

In topmost build.gradle add JaCoCo to classpath:

buildscript {
repositories { ... }
dependencies {
...
classpath "org.jacoco:org.jacoco.core:0.8.6"
}
}
apply from: "$project.rootDir/jacoco.gradle"
./gradlew clean testDevDebugUnitTestCoverage

Setting up SonarQube

You have to run SonarQube locally or on a remote machine. Setting that up is out of the scope of this post (that’s something that Google can certainly help with). For simplicity we’ll assume that we have Sonar running remotely on some host.

buildscript {
repositories { ... }
dependencies {
...
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0"
}
}
apply from: "$project.rootDir/sonar.gradle"
subprojects {
apply plugin: "org.sonarqube"
sonarqube {
properties {
property "sonar.coverage.jacoco.xmlReportPaths", "$projectDir.parentFile.path/app/build/reports/jacoco/testDevDebugUnitTestCoverage/testDevDebugUnitTestCoverage.xml"
}
}
}
Each clap counts!

Android enthusiast, Google Certified Android developer, Stackoverflower

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