JacocoReportTask extends DefaultTask { @TaskAction public void generateReport() throws IOException { File coverageFile = getCoverageFile(); File coverageDirectory = getCoverageDirectory(); List<File> coverageFiles = Lists.newArrayList(); if (coverageFile != null) { coverageFiles.add(coverageFile); } if (coverageDirectory != null) { Files.fileTreeTraverser().breadthFirstTraversal(coverageDirectory) .filter(File::isFile).copyInto(coverageFiles); } if (coverageFiles.isEmpty()) { if (coverageDirectory == null) { throw new IOException("No input file or directory specified."); } else { throw new IOException(String.format( "No coverage data to process in directory '%1$s'", coverageDirectory)); } } generateReport( coverageFiles, getReportDir(), getClassDir(), getSourceDir(), getTabWidth(), getReportName(), getLogger()); }  @InputFile @Optional @Nullable File getCoverageFile(); void setCoverageFile(File coverageFile); @InputDirectory @Optional @Nullable File getCoverageDirectory(); void setCoverageDirectory(File coverageDirectory); @OutputDirectory File getReportDir(); void setReportDir(File reportDir); @InputDirectory File getClassDir(); void setClassDir(File classDir); @InputFiles List<File> getSourceDir(); void setSourceDir(List<File> sourceDir); String getReportName(); @Input void setReportName(String reportName); @InputFiles FileCollection getJacocoClasspath(); void setJacocoClasspath(FileCollection jacocoClasspath); @Input int getTabWidth(); void setTabWidth(int tabWidth); @TaskAction void generateReport();  }
@Test public void sanityCheckReport() throws IOException, URISyntaxException { File coverageFile = copyResourceToFolder( "jacocoReport/com/android/tools/build/tests/myapplication/coverage.ec", mTemporaryFolder.newFolder()); File sourceRoot = setUpSourceDirectory(); File classDir = setUpClassDirectory(); File reportDir = mTemporaryFolder.newFolder(); JacocoReportTask.generateReport( ImmutableList.of(coverageFile), reportDir, classDir, ImmutableList.of(sourceRoot), 4, "debug", Logging.getLogger(JacocoReportTaskTest.class)); File indexHtml = new File(reportDir, "index.html"); Document document = Jsoup.parse(indexHtml, Charsets.UTF_8.name(), indexHtml.getParentFile().toURI().toString()); Elements totals = document.select("td:contains(142 of 147)"); expect.that(totals).named("Total coverage table cell").hasSize(1); document = navigateTo( getLinkWithText(document, "com.android.tools.build.tests.myapplication")); getLinkWithText(document, "MainActivity.new View.OnClickListener() {...}"); expect.that(document.text()).doesNotContain("BuildConfig"); document = navigateTo(getLinkWithText(document, "MainActivity")); document = navigateTo(getLinkWithText(document, "doA()")); Elements covered = document.select("span.fc"); assertThat(covered).isNotEmpty(); assertThat(covered.get(0).text()).contains("Log.d(\"Test1\", \"Do a\");"); }