# Configuring JaCoCo coverage limits in Android Gradle projects

DevFeed: [Configuring JaCoCo coverage limits in Android Gradle projects](<https://devfeed.tech/articles/note-to-future-me-add-jacoco-and-coverage-limits-32071.md>)

Original publisher: [Read original article](<https://www.maiatoday.net/p/note-to-future-me-add-jacoco-and-coverage-limits/>)

Published: 2018-10-13T13:55:25Z

Content type: tutorial

Language: en

Sources: [maiatoday](<https://devfeed.tech/sources/maiatoday.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [unit tests](<https://devfeed.tech/topics/unit-tests.md>), [Java](<https://devfeed.tech/topics/java.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [jacoco](<https://devfeed.tech/tags/jacoco.md>), [java-8](<https://devfeed.tech/tags/java-8.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [test-coverage](<https://devfeed.tech/tags/test-coverage.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

A practical Android Gradle snippet that adds JaCoCo test coverage, Kotlin support, and coverage limits for selected important classes or packages. It explains how to add the configuration, run coverage verification, and find the generated HTML report. It also warns that a Robolectric-related configuration line can crash unit tests on Java versions newer than 8.

## Source excerpt

I have found myself adding jacoco and coverage limits to more than one Android project. Enough times for me to extract the gradle bits in to a little nugget of grooviness to just drop into a project as needed. For a while now I have been reading about test coverage numbers and working with my intuition about what is the most valuable thing for me to spend my time on when building something. Also Kotlin. In the light of this my gradle build file snippet contains the following: Jacoco for test coverage Kotlin support A way to choose important classes and only set coverage limits on those Do this, Put the gist in a file jacoco.gradle Modify the jacoco.gradle file to add limits for your important packages. Add this line to your build.gradle apply from: 'jacoco.gradle' And then run the command ./gradlew tasks to see what new tasks are there to run. e.g. in the reporting section find something similar to ./gradlew/testDevDebugUnitTestCoverageVerification which will run the tests, make coverage reports and apply limits. Find the reports in a path similar to this ~/your\_project/your\_module/build/reports/jacoco/testDevDebugUnitTestCoverage/html/index.html Without further ado, the gist: The code in the gist was taken from so many blogposts. I can't even remember them all. Thanks to the original authors. Also note there is a small piece of cargo cult code that kept sneaking in from sundry blogposts that I don't need because I don't have Robolectric tests. As it turns out this code crashes unit tests on Java > 8. So remove this code if you have it and if it fits your usecase. tasks.withType(Test) { jacoco.includeNoLocationClasses = true }