5
votes
  1. I have written some test cases under androidTest directory.
  2. I am using androidx (testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner")
  3. added dependencies:

    androidTestImplementation 'androidx.test:runner:1.1.0'

    androidTestImplementation 'androidx.test:rules:1.1.0'

  4. added JaCoCo as dependency, as well.

  5. When I run createDebugAndroidTestCoverageReport Gradle task, all the test cases run successfully but coverage is 0% in report

I am guessing that is due to this error:

V/InstrumentationResultParser: Error: Failed to generate Emma/JaCoCo coverage. V/InstrumentationResultParser: INSTRUMENTATION_CODE: -1

1
you might not have defined the correct classDirectories; verify this once. - Martin Zeitler
I have also define dir. def fileFilter = ['*/R.class', '**/R$.class', '*/BuildConfig.', '*/Manifest.*', '*/*Test.*', 'android/**/*.*'] def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter) def mainSrc = "${project.projectDir}/src/main/java" sourceDirectories = files([mainSrc]) classDirectories = files([debugTree]) - Subhan

1 Answers

4
votes

Google just redirected me to my own answer ...

As it turned out, for Android projects one (meanwhile) has to use a path alike this:

"${project.buildDir}/intermediates/javac/debug/classes"

In case you may wonder... JaCoCo only works for @RunWith(JUnit4.class) Java unit tests in test (but not @RunWith(AndroidJUnit4.class) Android integration tests in androidTest).