4

I'm using Android Studio 1.2.2 and the Gradle plugin 1.2.3.

I'm trying to generate a code coverage report without running gradlew connectedCheck, just gradlew test. I want to avoid the need to have a connected device or emulator, so I can speed up the builds on the Jenkins server.

The best I've been able to do so far is to include unit tests execution data in the report, as described here: Jacoco Code Coverage in android studio. This is useful for displaying the results for all the tests, but at the moment I want to run only unit tests (Junit4 and Robolectric).

Is there a way to edit the createDebugCoverageReport gradle task so that it doesn't run connectedAndroidTest?

Thank you.

Community
  • 1
  • 1
Corneliu Dascălu
  • 3,900
  • 1
  • 28
  • 38

2 Answers2

3

I managed to find a workaround. Namely, disabling the connectedAndroidTest task.

project.afterEvaluate {
    def append = "append=true"
    def destFile = "destfile=$buildDir/outputs/code-coverage/connected/coverage.ec"
    testDebug.jvmArgs "-javaagent:$buildDir/intermediates/jacoco/jacocoagent.jar=$append,$destFile"

    createDebugCoverageReport.dependsOn testDebug
    connectedAndroidTestDebug.enabled = false
}

So, starting from the solution proposed in this answer, which adds unit test coverage to the instrumentation test coverage, I've disabled the instrumentation test task. This allows me to run only the unit tests, without needing a connected device, by executing the createDebugCoverageReport task.

Community
  • 1
  • 1
Corneliu Dascălu
  • 3,900
  • 1
  • 28
  • 38
1

There is an open source plugin for doing exactly this: jacoco-android-gradle-plugin.

It basically runs the unit tests for each variant and generates coverage reports. The usage is documented in the project README.

mitja
  • 61
  • 8