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

Looking for more like this?

Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.

Chicago Roboto 2022 Retrospective
Android Development

Chicago Roboto 2022 Retrospective

August 11, 2022

Scott Schmitz shares some notes of interest from talks at Chicago Roboto 2022, an Android community conference, that took place August 1-2.

Read more
A 3-part framework for getting your software project approved internally

A 3-part framework for getting your software project approved internally

September 25, 2024

Explore this strategic approach to securing internal buy-in for your custom software projects. The framework emphasizes starting with a lean business case, engaging key stakeholders across the organization to align economic, operational, and technical considerations, and embracing an iterative learning process to make informed decisions.

Read more
How our Associates are using AI tools: Advice for early-career developers
Development

How our Associates are using AI tools: Advice for early-career developers

August 13, 2024

Our 2024 Associates at Michigan Labs share their experiences using AI tools like GitHub Copilot and ChatGPT in software development. They discuss how these tools have enhanced their productivity, the challenges they've faced, and provide advice for using AI effectively.

Read more
View more articles