Getting Jacoco to work with Dagger

January 16, 2015

Writing tests for your code provides you with a number of development benefits, from proving that your code meets set requirements to ensuring that future code changes don’t introduce bugs or break existing functionality. It’s also important to know how much of your code is covered by your tests and what areas in your code aren’t yet covered.

Jacoco is a framework that creates code coverage reports that show you how much of your code has or has not been touched in your tests. This post is an excellent guide to setting up your Android Gradle project to use Jacoco.

To generate the Jacoco test coverage report, execute the following in the commandline:

./gradlew cleanTest test jacocoTestReport

If you’re using Dagger in your Android Gradle project, you might encounter the following error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:jacocoTestReport'.
> Error while creating report

To dig deeper into this error, execute the same command with the stacktrace option:

./gradlew cleanTest test jacocoTestReport --stacktrace

In the stack trace, you’ll see an entry similar to this:

Caused by: java.io.FileNotFoundException: /app/build/intermediates/classes/debug/com/yourproject/AndroidModule$ModuleAdapter$ProvideAccountManagerProvidesAdapter.class (No such file or directory)
	at org.apache.tools.ant.types.resources.FileResource.getInputStream(FileResource.java:217)
	at org.jacoco.ant.ReportTask.createBundle(ReportTask.java:568)
	at org.jacoco.ant.ReportTask.createReport(ReportTask.java:547)
	at org.jacoco.ant.ReportTask.execute(ReportTask.java:494)
	... 67 more

Jacoco is expecting to find generated files with a single “$” in their name, but Dagger generates files with “$$” in their name. However, we don’t need to check test coverage in these generated classes, so we can tell Jacoco to ignore these files by adding the following to the “excludes” array (in addition to any existing entries) in the fileTree passed to “classDirectories”, like so:

classDirectories = fileTree(
 dir: '../app/build/intermediates/classes/debug',
 excludes: [
 '**/*InjectAdapter*.*',
 '**/*StaticInjection*.*',
 '**/*ModuleAdapter*.*'
 ]
 )
Joseph Kreiser
Joseph Kreiser
Software Developer

Stay in the loop with our latest content!

Select the topics you’re interested to receive our new relevant content in your inbox. Don’t worry, we won’t spam you.

MichiganLabs among INC. MAGAZINE’S BEST WORKPLACES 2020
Business Team Press Release

MichiganLabs among INC. MAGAZINE’S BEST WORKPLACES 2020

April 30, 2020

Michigan Software Labs is one of the highest-scoring businesses, with standout employee engagement.

Read more
How to Build a Better App UI Architecture - Part 2
Development

How to Build a Better App UI Architecture - Part 2

November 19, 2019

Following up on the importance of consistency and reusability in Part 1, let's look at how to optimize the UI system for better consistency and reusability. We’ll begin by breaking down an iOS app, then look at how to build an interfaces-based system.

Read more
Iulia Rontu to join leadership team at MichiganLabs
Team

Iulia Rontu to join leadership team at MichiganLabs

February 4, 2021

Iulia will be joining the leadership team at MichiganLabs effective Monday, February 8, 2021. Iulia has been with MichiganLabs for 2 years in May of this year. She is currently the UX Practice Lead where she helps define our user experience practice to help transform and delight our clients.

Read more
View more articles