I have a multi-module Android project built by Gradle with the new Android-plugin 0.10+.

All modules are mainly configured from the root project.

I've enable the new code coverage feature. The major problem is that only some modules contain test code and when I enable code coverage on modules that don't contain test code, it fails.

I would like to avoid enabling code-coverage in each module `build.gradle` file so that when someone add test code in a module that wasn't tested, there is no need to modify the `build.gradle`.

I ended up with the following code where code coverage is enabled only when the `androidTest` `sourceSet` contains at least one .java source file. It works as expected, but I'm wondering if there is a nicer way of doing this.

Here is the relevant portion of the root project `build.gradle` script:

 configure(subprojects.findAll()) {
 if(it.name.equals('MyApp')){
 apply plugin: 'android'
 }else{
 apply plugin: 'android-library'
 }

 android {
 ...
 //my question is really about the follong 4 lines :
 //is there a simpler way to get java source file count in the androidTest sourceSet ?
 def androidTestJavaSourceFileCount = 0
 sourceSets.androidTest.allJava.each {
 androidTestJavaSourceFileCount ++
 }
 println("androidTestJavaSourceFileCount in androidTest for module $project.name : $androidTestJavaSourceFileCount")
 buildTypes {
 debug {
 testCoverageEnabled = androidTestJavaSourceFileCount > 0
 }
 }
 
 jacoco {
 version = '0.6.2.201302030002'
 }

 sourceSets {
 main {
 manifest.srcFile 'AndroidManifest.xml'
 res.srcDirs = ['res']
 assets.srcDirs = ['assets']
 }
 }
 }