Jacoco-report-aggregation fails with "Can't add different class with same name"

Hello,

I have a multi module project where multiple of these sub projects generate code from the same protobuf dependency. So you end up with generated code in more than one project with the same generated code. This builds and works fine and does what we need it to do.

When enabling jacoco coverage aggregation, the reporting fails with the following:

* What went wrong:
Execution failed for task ':test-results:testCodeCoverageReport'.
> Error while creating report

Caused by: java.lang.IllegalStateException: Can't add different class with same name:

Partial stack trace is below. I’m looking for a way around this or to have the jacoco report aggregation to not fail on it, maybe warn? Open to thoughts here.


org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':test-results:testCodeCoverageReport'.
...
Caused by: : Error while creating report
        at org.jacoco.ant.ReportTask.execute(ReportTask.java:502)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:299)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...
Caused by: java.io.IOException: Error while analyzing myapplication-broker-17.7.1-SNAPSHOT.jar@eadp/myapplication/experimental/MyApplicationEventsGrpcKt$MyApplicationEventsCoroutineImpl
Base.class with JaCoCo 0.8.8.202204050719/5dcf34a.
        at org.jacoco.core.analysis.Analyzer.analyzerError(Analyzer.java:163)
        at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:135)
        at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:158)
        at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:195)
        at org.jacoco.core.analysis.Analyzer.analyzeZip(Analyzer.java:267)
        at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:198)
        at org.jacoco.ant.ReportTask.createBundle(ReportTask.java:573)
        at org.jacoco.ant.ReportTask.createReport(ReportTask.java:545)
        at org.jacoco.ant.ReportTask.execute(ReportTask.java:496)
        ... 147 more
Caused by: java.lang.IllegalStateException: Can't add different class with same name: eadp/myapplication/experimental/MyApplicationGrpcKt$MyApplicationEventsCoroutineImplBase   
        at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)
        at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:100)
        at org.objectweb.asm.ClassVisitor.visitEnd(ClassVisitor.java:377)
        at org.jacoco.core.internal.flow.ClassProbesAdapter.visitEnd(ClassProbesAdapter.java:100)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:748)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:424)
        at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:117)
        at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:133)
        ... 154 more

It would probably the easiest if you add one “common” project that does the generation of those classes onto which the other projects that need those classes depend on. This way you only need to do the generation once and also need to do the compilation of those classes once and if you use JPMS in the future will not have the problem of split-packages you would have right now. And then this error probably also goes away.

Another option would probably be to generate to different packages in the different projects.

I don’t know whether there would also be a hack-around to keep it like you currently have it.