Android Gradle script in multi-module project 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 &gt; 0
            }
        }

        jacoco {
            version = '0.6.2.201302030002'
        }

        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
           }
       }
 }
Let's see what we have here:

sourceSets.androidTest.allJava.each {
    ...
}



sourceSets leads to SourceSetContainer
.androidTest leads to a single SourceSet
.allJava leads to the allJava property on a SourceSet which leads to a SourceDirectorySet


Now, while there is not a .size() method or anything like it in SourceDirectorySet, or any of the super interfaces of that, there is some Groovy magic that provides a .size() method for all Iterables.

This means that you should be able to do the following:

def androidTestJavaSourceFileCount = sourceSets.androidTest.allJava.size()


Don't be fooled by this one-liner though, the source of the Iterable.size() method is:

public static int size(Iterable self) {
    return size(self.iterator());
}


which calls:

public static int size(Iterator self) {
    int count = 0;
    while (self.hasNext()) {
        self.next();
        count++;
    }
    return count;
}


So it will do the same thing as your previous code did, but it will clean up your part of the code.