title: Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1
body: <p>I recently started integrating <code>android-gradle-plugin</code> 1.1.0 in one of my projects. The project uses <code>robolectric 2.4</code> to run unit tests.</p>
  
  <p>It's a multi module project with very complex dependencies (Some modules depend on other modules). Something like that:</p>
  
  <pre><code>--&gt; application-module (dependsOn: module1, module2, module-core)
      --&gt; module1 (dependsOn: module-core)
      --&gt; module2 (dependsOn: module-core)
      --&gt; module-core (dependsOn: module3, module4)
          --&gt; module3 (library dependencies)
          --&gt; module4 (library dependencies)
  </code></pre>
  
  <p>For a more cleared picture please see <a href="https://github.com/serj-lotutovici/jacoco-example">jacoco-example</a> project.</p>
  
  <p>I tried to integrate JaCoCo to generate reports for the unit tests, but it seems to me that it runs only <code>androidTests</code> which are basically instrumentation tests.</p>
  
  <p>After some google'ing I've come across a few projects on GitHub and other articles, but they mainly are focused on previous versions of the <code>android-gradle-plugin</code> or are using other third party plugins like <code>android-unit-test</code> <a href="https://github.com/nenick/android-gradle-template">for example here</a>.</p>
  
  <p>May be I've lost my ability to google. But can somebody point me in a direction where I can find some documentations regarding the new stuff in android gradle plugin and how to run the jacoco task only for unit tests?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Adopted the script from <a href="https://github.com/nenick/android-gradle-template">nenick's example</a>:</p>
  
  <pre><code>apply plugin: "jacoco"
  
  configurations {
      jacocoReport
  }
  
  task jacocoReport(dependsOn: 'testDebug') &lt;&lt; {
      ant {
          taskdef(name:'jacocoreport',
                  classname: 'org.jacoco.ant.ReportTask',
                  classpath: configurations.jacocoReport.asPath)
  
          mkdir dir: "${buildDir}/test-coverage-report"
          mkdir dir: "${buildDir}/reports/jacoco/test/"
  
          jacocoreport {
              executiondata = files("${buildDir}/jacoco/testDebug.exec")
  
              structure(name: "${rootProject.name}") {
                  classfiles {
                      fileset (dir: "${buildDir}/intermediates/classes/debug") {
                          //exclude(name: '**/*_*.class')
                          exclude(name: '**/R.class')
                          exclude(name: '**/R$*.class')
                          exclude(name: '**/BuildConfig.class')
                      }
                  }
  
                  sourcefiles {
                      fileset dir: "src/main/java"
                      fileset dir: "${buildDir}/generated/source/buildConfig/debug"
                      fileset dir: "${buildDir}/generated/source/r/debug"
                  }
              }
  
              xml destfile: "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
              html destdir: "${buildDir}/test-coverage-report/"
          }
      }
  }
  
  dependencies {
      jacocoReport 'org.jacoco:org.jacoco.ant:0.7.2.201409121644'
  }
  </code></pre>
  
  <p>After that the <code>./gradlew jacocoReport</code> executes and generates the report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.</p>
  
  <p><strong>UPDATE_2</strong></p>
  
  <p>Tried out this <a href="http://raptordigital.blogspot.de/2014/08/code-coverage-reports-using-robolectric.html">example</a>. Adding the next task to one of my gradle build files:</p>
  
  <pre><code>task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
      group = "Reporting"
      description = "Generate Jacoco coverage reports"
  
      classDirectories = fileTree(
              dir: "${buildDir}/intermediates/classes/debug",
              excludes: ['**/R.class',
                         '**/R$*.class',
                         '**/*$ViewInjector*.*',
                         '**/BuildConfig.*',
                         '**/Manifest*.*']
      )
  
      sourceDirectories = files("${buildDir.parent}/src/main/java")
      additionalSourceDirs = files([
              "${buildDir}/generated/source/buildConfig/debug",
              "${buildDir}/generated/source/r/debug"
      ])
      executionData = files("${buildDir}/jacoco/testDebug.exec")
  
      reports {
          xml.enabled = true
          html.enabled = true
      }
  }
  </code></pre>
  
  <p>Same issue, the reports are generated, but the code coverage is still zero.</p>
  
  <p><strong>UPDATE_3</strong></p>
  
  <p>It seams that the task from UPDATE_2 worked but only for the module with <code>apply plugin: 'com.android.application'</code> (The reports a generated correctly). But for modules that are android libraries (<code>apply plugin: 'com.android.library'</code>) the reports show zero coverage, although the modules contain more tests then the application module.</p>
  
  <p><strong>UPDATE_4</strong></p>
  
  <p>Created a simple example project that demonstrates my issue. Currently if you run <code>./gradlew jacocoReport</code> the report is generated, but no test coverage is displayed for the module projects. See this <a href="https://github.com/serj-lotutovici/jacoco-example">link</a> </p>
  
  <p><strong>Short note:</strong> When the tests were AndroidUnitTests (whiteout JUnit 4 and Robolectric) JaCoCo reports showed coverage for all the modules.</p>
  
  <p>Any ideas?</p>