13

I'm using Robolectric and JaCoCo together. My code coverage reports do not work without the following lines of code in gradle script:

 testOptions {
    unitTests.all {
        jacoco {
            includeNoLocationClasses = true
        }
    }
 }

But in the recent version of Gradle the JaCoCo extension, that I use here, is marked as deprecated. I could not find any replacement for it. So, where should I apply the includeNoLocationClasses = true option?

Lingviston
  • 5,479
  • 5
  • 35
  • 67

2 Answers2

10

Using the Gradle Kotlin DSL with Gradle 5.5.1 and Kotlin 1.3.31 this works:

tasks {
    withType<Test> {
        configure<JacocoTaskExtension> {
            isIncludeNoLocationClasses = true
        }
    }
}
Tom Hanley
  • 1,252
  • 11
  • 21
  • I needed `excludes = listOf("jdk.internal.*")` as well as `isIncludeNoLocationClasses = true`. Without the `excludes` I got a `NoClassDefFoundError` with `GeneratedSerializationConstructorAccessor1`. – Edmund Johnson Feb 16 '23 at 17:26
2

I found a solution. JaCoCo automatically adds jacoco extension to all tasks of test type. So, all that I had to do was adding the following lines into build script:

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

It doesn't look like an official solution, but it allows the custom JacocoReport implementation to work correctly.

Lingviston
  • 5,479
  • 5
  • 35
  • 67
  • 1
    Doesnt' work for me (Gradle 5.1.1 / Android Gradle Plugin 3.3.1): `Could not set unknown property 'includeNoLocationClasses' for object of type org.gradle.testing.jacoco.plugins.JacocoPluginExtension. ` – Alix Feb 14 '19 at 11:40
  • I also get this error: Could not set unknown property 'includeNoLocationClasses' for object of type org.gradle.testing.jacoco.plugins.JacocoPluginExtension. – fobbymaster Mar 05 '19 at 20:03
  • My answer is outdated. See the recent solution above. – Lingviston Nov 04 '21 at 11:58