Jacoco offline instrumentation Gradle script

前端 未结 4 872
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 06:18

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

4条回答
  •  盖世英雄少女心
    2020-12-06 06:36

    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.

提交回复
热议问题