JaCoCo doesn't work with Robolectric tests

It is known issue with the possible workaround - https://github.com/jacoco/jacoco/pull/288

Or downgrade jacoco version to 0.7.1.201405082137

UPDATE

The workaround is not needed anymore. You must use gradle version 2.13 and jacoco version 0.7.6.201602180812.

Update root build.gradle:

buildscript {
    dependencies {
        classpath 'org.jacoco:org.jacoco.core:0.7.6.201602180812'
    }
}

task wrapper( type: Wrapper ) {
  gradleVersion = '2.13'
}

Run ./gradlew wrapper

Update project build.gradle:

apply plugin: 'jacoco'

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

I was facing the same issue but now it is resolved for me by following this link,

issue link: https://github.com/robolectric/robolectric/issues/2230

Solution for this problem is mentioned here:

https://github.com/dampcake/Robolectric-JaCoCo-Sample/commit/f9884b96ba5e456cddb3d4d2df277065bb26f1d3


The accepted answer is a bit dated. Here is a similar fix we just implemented. In the module (i.e. app) build.gradle add:

apply plugin: 'jacoco'

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

This does require JaCoCo 7.6+, but you are likely using it already.

Notes for Studio:

  1. This only fixes the CLI. If you run coverage from Studio using JaCoCo, the Robolectric coverage is still not reported. The default IntelliJ Coverage Runner seems to work fine.
  2. The test were crashing intermittently in Studio unless I added -noverify to the Android JUnit -> VM Options