Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco offline instrumentation Gradle script

I tried looking for Jacoco offline instrumentation gradle script snippets but couldn't find one. Is it possible to do Jacoco offline instrumentation through gradle scripts ? If yes...An example of it would be greats. Thanks.

like image 741
karthik Avatar asked Dec 28 '16 22:12

karthik


2 Answers

The answer https://stackoverflow.com/a/42238982/2689114 works for me(with some adapting to a newer gradle version).

If you have multi-module gradle project, then you should add some extra configuration to support cross-module code coverage.

For more details see Cross-module code coverage with Jacoco offline instrumentation in gradle mutlimodule project

Also, there is a working example: https://github.com/SurpSG/jacoco-offline-instrumentation

like image 35
SergiiGnatiuk Avatar answered Sep 29 '22 12:09

SergiiGnatiuk


classesDir is not available in Gradle 5 this offline instrumentation code worked for me on Gradle 5.1.1

 task instrument(dependsOn: [classes, project.configurations.jacocoAnt]) {

 inputs.files classes.outputs.files
 File outputDir = new File(project.buildDir, 'instrumentedClasses')
 outputs.dir outputDir
 doFirst {
     project.delete(outputDir)
     ant.taskdef(
             resource: 'org/jacoco/ant/antlib.xml',
             classpath: project.configurations.jacocoAnt.asPath,
             uri: 'jacoco'
     )
     def instrumented = false
     if (file(sourceSets.main.java.outputDir).exists()) {
         def instrumentedClassedDir = "${outputDir}/${sourceSets.main.java}"
         ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
             fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class')
         }
         //Replace the classes dir in the test classpath with the instrumented one
         sourceSets.test.runtimeClasspath -= files(sourceSets.main.java.outputDir)
         sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
         instrumented = true
     }
     if (instrumented) {
         test.jvmArgs += '-noverify'
     }
 }
}


test.dependsOn instrument

The above code is taken from the link https://github.com/esdk/g30l0/commit/82af4c9aad50aadc40d940471fe1b934473170c7 pease follow for more information.

like image 190
Pravanjan Avatar answered Sep 29 '22 12:09

Pravanjan