2

I've implemented jacoco to my project some times ago. The implementation is just like described here:

classpath "org.jacoco:org.jacoco.core:0.8.3"

and I have no problem on that. (Let's say this is project 1)

But the thing is whenever I'm exporting the project to .jar file. The second project (let's say project 2) that I use the .jar file gives that error on runtime:

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

I've tracked the error about the library but in reality I don't want to export anything related to jacoco in project 1, because it seems meaningless to me to have that inside the jar file.

How can I keep jacoco out of the jar file?

Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
Onur
  • 134
  • 2
  • 16

2 Answers2

4

Your error is specified in how to prevent jacoco instrumenting production code?. Basically it is explained that JaCoCo provides two ways of performing instrumentation: on the fly and offline. It also describes literally the root of your problem (in bold):

One of such specific scenarios - is execution of tests on Android, because there is no way to use Java agent on it. So AFAIK Android Plugin for Gradle, when instructed to measure coverage using JaCoCo, uses offline instrumentation and therefore requires two types of build - with coverage and without for release.

So you need to generate the jar for release. Actually in the example you used as baseline, it gives you the solution (with small corrections) to disable testCoverage for the release:

buildTypes {
   release {
     // Your release part, disable testCoverage
     testCoverageEnabled false
   }
   debug {
     //your debug part
     //add tests coverage using Jacoco
     testCoverageEnabled true
   }
 }

UPDATE: found this workaround for maven.

Instrumentation should not be done on every build. You do not want to release instrumented code, first because this is bad practice and second code will not run unless jacocoagent.jar is in the classpath.

Try to download and include jacocoagent.jar manually in the classpath (I did not have time to test it).

Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
  • Hi Carlos, Sorry that I forgot the mention that part. As you've mentioned we're also having a structure like that and production jars has no problem, but the problem exists in debug jars and to be honest we have to export debug jars too and that makes it more complicated. Well I think there is not much possible way to extract a debug .jar with no instance/dependency/whatever with jacoco when the testCoverageEnabled is true. If no real/closer solutions will be available I'll accept that 1-2 days. – Onur Mar 26 '19 at 07:51
  • I will update the response because there is a workaround to include the instrumentation jar manually. It is not recommened but I guess you can give a try. – Carlos Cavero Mar 26 '19 at 08:04
0

You can create a separate 'Java Library' module in your project and don't add 'Jacoco' dependencies in that module. You can just keep your core logic, which you want to be exported in that module. When you will build that project, a .jar file will get created in '{module_name}/build/' folder of your project.

You also need to add this new module dependencies in your main module 'app'. And add 'Jacoco' dependencies also in 'app' module.

Let me know, if you need further help.

Jatin
  • 491
  • 2
  • 12
  • Hi Jatin, Sorry for kinda late reply, but creating a separate module is not an option according to the project structure. Is there a possibility you see without doing it? – Onur Mar 25 '19 at 09:56
  • OK, I think we need to exclude dependencies while generating .jar file. You can refer this link for it. https://www.vogella.com/tutorials/Gradle/article.html#excluding_dependencies – Jatin Mar 25 '19 at 10:54
  • I am not sure, if excluding will help you. But you can give it a try. – Jatin Mar 25 '19 at 11:03
  • Not really, because that is excluding a part of library the dependency uses. In that case jacoco should be totally in the application but shouldn't be exporting any dependency while exporting as .jar . It's really weird because I don't understand noone wrote about using jacoco and exporting the project as a .jar. – Onur Mar 25 '19 at 12:55
  • May be Jacoco team may answer your question. https://www.jacoco.org/jacoco/trunk/doc/team.html – Jatin Mar 26 '19 at 09:23