title: Test coverage with Jacoco plugin for Gradle in offline instrumentation mode for Android
body: <p>I'm trying to get code coverage for my PowerMockito unit tests with Jacoco Gradle plugin. I have to use offline instrumentation because PowerMockito doesn't support online mode. </p>
  
  <p>For Jacoco you have to create Ant task which will make code coverage, but all examples of offline instrumentation use <strong>'Java'</strong> plugin which is not compatible with <strong>'com.android.library'</strong> plugin.</p>
  
  <p>Workable solutions which use 'Java' plugin:</p>
  
  <pre><code>apply plugin: 'java'
  apply plugin: 'jacoco'
  
  //Additional SourceSets can be added to the jacocoOfflineSourceSets as needed by 
  project.ext.jacocoOfflineSourceSets = [ 'main' ]
  task doJacocoOfflineInstrumentation(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
          jacocoOfflineSourceSets.each { sourceSetName -&gt;
              if (file(sourceSets[sourceSetName].output.classesDir).exists()) {
                  def instrumentedClassedDir = "${outputDir}/${sourceSetName}"
                  ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
                      fileset(dir: sourceSets[sourceSetName].output.classesDir, includes: '**/*.class')
                  }
                  //Replace the classes dir in the test classpath with the instrumented one
                  sourceSets.test.runtimeClasspath -= files(sourceSets[sourceSetName].output.classesDir)
                  sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
                  instrumented = true
              }
          }
          if (instrumented) {
              //Disable class verification based on https://github.com/jayway/powermock/issues/375
              test.jvmArgs += '-noverify'
          }
      }
  }
  test.dependsOn doJacocoOfflineInstrumentation
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/41370815/jacoco-offline-instrumentation-gradle-script">Jacoco offline instrumentation Gradle script</a></p>
  
  <p><a href="https://stackoverflow.com/questions/31720139/jacoco-code-coverage-report-generator-showing-error-classes-in-bundle-code-c">jacoco code coverage report generator showing error : &quot;Classes in bundle &#39;Code Coverage Report&#39; do no match with execution data&quot;</a></p>
  
  <p>For Android I use</p>
  
  <pre><code>apply plugin: 'com.android.library'
  </code></pre>
  
  <p>And this plugin doesn't have <strong>'classes'</strong> task, <strong>'jacocoTestReport'</strong> task and have different SourceSet implementation. This error message appears when I try to use both plugins:</p>
  
  <pre><code>The 'java' plugin has been applied, but it is not compatible with the Android plugins
  </code></pre>
  
  <p>So, my question is: How to use Jacoco offline instrumentation for Gradle in case of Android platform.</p>