Problem running app with testCoverage enabled and jacoco

Viewed 472

I have an Android project which is a library and this library comes with its own demo project, as usual. The library is not developed by me, but I have the code. If I compile the demo project, with the build.gradle of the library with:

debug {
    testCoverageEnabled true
}

Everything works perfectly, the demo project runs and the library works normally, but if I change to:

debug {
    testCoverageEnabled false
}

When running the demo project I get the following error:

 java.lang.NoClassDefFoundError: Failed resolution of: lorg/jacoco/agent/rt/internal_8ff85ea/Offline;

My problem is that I integrate this library (aar) in my own project and when I execute it, I always get the same error that the library gives when indicating testCoverageEnabled false.

I have tried to create the aar with testCoverageEnabled false even if it does not compile, I have changed the testCoverageEnabled in my project, etc... but I always get the same error.

It is important to say that jacoco is not implemented neither in the library nor in my project... No implementations, no plugin… nothing, I think this is very strange.

Updating gradle to the latest versions I don't get positive results.

How can I fix this?

I hope I have explained myself well.

Thanks in advance.

1 Answers

Have you tried adding jacoco to your project? It may just be that this library is looking for it, and is erroring out when it can't find it.

To add jacoco to your project, you can do the following:

  1. in your app-level build.gradle add the following to the top:

    apply plugin: 'jacoco'
    apply from: '../app/jacoco.gradle' // the path to your jacoco.gradle file
    
  2. Create a jacoco.gradle file. This is what mine looks like, update yours as needed

apply plugin: 'jacoco'
jacoco {
 toolVersion = "0.8.6"
 reportsDir = file("$buildDir/reports")
}

task jacocoTestReportDebug(type: JacocoReport, dependsOn: ['testDevDebugUnitTest']) {
 // "debug" build type for test coverage
 group = "reporting"
 description = "Generate unified Jacoco code coverage report"

 reports {
     xml.enabled false
     csv.enabled false
 }

 // update this to include any areas we don't need or cannot test
 def fileFilter = []

 def javaDebugTree = fileTree(dir: "${buildDir}/intermediates/javac/devDebug", excludes: fileFilter)
 def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/devDebug", excludes: fileFilter)
 def mainSrc = "${project.projectDir}/src/main/java"

 sourceDirectories.from = files([mainSrc])
 classDirectories.from = files([javaDebugTree, kotlinDebugTree])
 
 // Target both java and kotlin build folder
 executionData.from = fileTree(dir: "$buildDir", includes: [
         "jacoco/testDevDebugUnitTest.exec",
         "outputs/code-coverage/connected/*coverage.ec"
 ])
}

Hopefully this solves the issue! Otherwise, please post the full stack trace to help us debug further.

Related