I recently started integrating android-gradle-plugin 1.1.0 into one of my projects. The project uses robolectric 2.4 to run unit tests.
This is a project with several modules with very complex dependencies (some modules depend on other modules). Something like that:
--> application-module (dependsOn: module1, module2, module-core) --> module1 (dependsOn: module-core) --> module2 (dependsOn: module-core) --> module-core (dependsOn: module3, module4) --> module3 (library dependencies) --> module4 (library dependencies)
For a cleaner image, see the jacoco-example project.
I tried to integrate JaCoCo to create reports for unit tests, but it seems to me that it only works androidTests , which are mostly instrumental tests.
After some google'ing, I came across several projects on GitHub and other articles, but they mainly focus on previous versions of android-gradle-plugin or use other third-party plugins such as android-unit-test for example here .
I may have lost the ability of Google. But can someone point me in the direction where I can find the documentation regarding the new material in the android gradle plugin and how to run the jacoco task only for unit tests?
UPDATE
Took a script from nenick example :
apply plugin: "jacoco" configurations { jacocoReport } task jacocoReport(dependsOn: 'testDebug') << { 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' }
After that ./gradlew jacocoReport executes and generates a report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.
UPDATE_2
Tried this example . Adding the following task to one of my gradle build files:
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 } }
The same problem, reports are generated, but the coverage of the code is still zero.
UPDATE_3
This means that the task from UPDATE_2 worked, but only for the module with apply plugin: 'com.android.application' (reports are generated correctly). But for modules that are Android libraries ( apply plugin: 'com.android.library' ), the reports show zero coverage, although the modules contain more tests than the software module.
UPDATE_4
Created a simple example demonstrating my problem. Currently, if you run ./gradlew jacocoReport , a report is generated, but test coverage is not displayed for module projects. See Link
Brief note:. When the tests were AndroidUnitTests (whiteout JUnit 4 and Robolectric), JaCoCo reports showed coverage for all modules.
Any ideas?