Measuring unit test coverage in multi-module Android projects using Jacoco: Part 1

Photo by Christian Kaindl on Unsplash
  • Part 1: test reports generation [this article]
  • Part2: coverage verification & enforcement

Not so long time ago our Android team decided to push our processes related to unit testing to the next level. We’ve already had some verbal agreements but most of the time everything was a bit spontaneous (not to say chaotic). We didn’t have any formal rules and metrics and as some famous man said — “you can’t improve something if you cannot measure it”. So I started searching for a way to automate our processes related to writing tests. Quick Googling led me to Jacoco: there is a lot of articles and blog posts on this topic but most of them appeared to be not quite compatible with Android specifics and especially with multi-module setup. So I decided to sum up all of my findings and share the results with the community in this blog post. Welcome!

Let’s start by adding Jacoco plugin to your project if you haven’t done it yet. To begin with, create a new jacoco.gradle file in the root project folder, it will contain most of the jacoco-related logic once we’re done:

Now we’re ready to start filling it with useful stuff. Essentially, Jacoco provides two main and quite independent features:

  1. Test reports generation (HTML, XML, etc.). jacocoTestReport Gradle task is responsible for that. This feature is pretty straightforward — it simply generates human-readable reports for all Kotlin classes so you can use them to get an idea on your current coverage (measured in %) in a particular file, folder, package or module. That’s what we’re going on to discuss in this first article.
  2. Test coverage verification. This one is more interesting: it allows you to define some coverage threshold (like 80%) and to execute a check against your code which will say whether this threshold is reached or not. Depending on your needs, you can make your remote pipeline fail in Gitlab/GitHub or even prevent local builds from compilation if this check fails. This is what Jacoco’s jacocoTestCoverageVerification Gradle task is for. I am going to discuss this topic in the next article.

In most cases, feature #1 should look like a reasonable thing to begin with, so let’s get into more details on how to set up test reports generation using Jacoco in your Android project.

Nowadays, many multi-module projects contain both traditional Android (the ones which have either or plugins applied in their gradle file) and pure Java/Kotlin (java-library plugin) modules. Our project is no exception here. From unit testing and Jacoco standpoints, these modules are a little bit different and require separate set up strategies. So it would be great to have a way to differentiate all of that at the Gradle level. Let’s add the following Groovy function to the jacoco.gradle we’ve created earlier:

Now we’re ready to setup Jacoco’s Gradle tasks depending on the current module type:

“Kotlin reporting” is fairly easy to set up:

Android reporting is more complicated:

A couple of notes:

  • We don’t write a lot of Android Tests (the ones which are located in/androidTest directory instead of usual /test) and we don’t really want such tests to affect our unit test coverage so I’ve excluded them from this setup. They can be added to the generation/verification process fairly easy though, the main change would be to enable testCoverageEnabled flag somewhere inside the setupAndroidReporting + you will also need to add one more dependency to dependsOn section:
  • You may want to customize defFilter so it works best for your particular project. For instance, I don’t want UI things like Fragments and Activities to affect overall coverage so I added them to exclusions. By the way, you may also want to have the same set of exclusions for both Android and Kotlin modules. In our case, we don’t need any exceptions for Java/Kotlin modules.
  1. Java or mixed Java/Kotlin projects might need to tweak and extend source directories from the above. Pure Kotlin projects (like ours) should work fine with the setup from above in most of the cases.

We’re done with the basic setup for jacoco.gradle file. Let’s make its features available to actual modules. The simplest solution would be to add the following line to every module’s build.gradle file:

But this approach is not flexible enough as it leads to copy-pasting. And what’s more important — it’s super easy to forget to add this line when you’re creating a new module. If you’re not happy with these limitations you can try adding the following code to the top-level gradle file of your project:

This will automatically apply all the Jacoco stuff from jacoco.gradle file to all Gradle submodules. You can further extend this approach by adding extra checks inside afterEvaluate block if you need to, here’s a simple example:

That’s basically it, now we’re ready to generate HTML reports for all of our modules. Just open Terminal tab in your Android Studio and execute the following command:

Alternatively, you can generate reports for individual modules by explicitly specifying module name:

Your reports will be generated in <module>/build/coverage-report/ directory, they basically look like this:

Generated HTML report example

That’s basically it. Now you have everything needed to estimate and measure unit test coverage across all modules of your project. jacocoTestReport could also help you to find weak spots which require some extra attention from its maintainers.

But this is only the first step on the way to better unit testing. In the next article, I’d like to keep extending our jacoco.gradle to also includejacocoTestCoverageVerification feature: I’ll show how I made it work with our multi-module setup + how it can be integrated into your CI/CD pipelines. Stay tuned :-)

Bonus: ignore list. Let’s say you have some modules you’d like to exclude from all the measurements (to save time & resources for instance). Here’s how it can be done:

  1. Create a newjacoco-ignore-list.gradle file next to to the jacoco.gradle(it will evolve into a separate Jacoco config in the second article + it might be more convenient for you and other devs from your team to keep all configurable stuff independent from Jacoco implementation/integration details):

2. Extendjacoco.gradle a little bit:

3. Enjoy!

Android enthusiast

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