Gradle exclude file from sourceSet not working
<p>I've got a project where I'm trying to include a single file from a tree of java source files into a library project.  Whatever I do I am not able to include only that file.  I have had success in another project excluding all files except the one, but that only worked when running gradle from the library project only.  When it was run from the application directory, all files were included.  While trying to create a simple project that reproduces the issue, that same exclude all does not work either.</p>

<p>The sample file tree is simple:</p>

<pre><code>application - android app that includes the two libraries
library1/
  build.gradle
  src/main/AndroidManifest.xml
library2/
  build.gradle
  src/main/AndroidManifest.xml
java/src/com/company/product/
  /pkg1
    Source1.java, Source2.java, Source3.java
    libraryoneonly/Library1Source.java
  /pkg2
    Source1.java, Source2.java, Source3.java
</code></pre>

<p>The library1 build.gradle looks like this.  Neither of the includes or excludes work below.</p>

<p><strong>library1/build.gradle</strong></p>

<pre><code>apply plugin: 'com.android.library'
android {
    sourceSets {
        main.jni.srcDirs = []
        main.jniLibs.srcDir 'src/main/libs'
        main.java {
            srcDir "../../java/src"
            exclude  {  FileTreeElement elem -&gt;
                println "Searching for files, found: " + elem.relativePath
                !(elem.isDirectory() || elem.name.equals('Library1Source.java'))
            }
            //include ("**/libraryoneonly/**")
            //include ('com/company/product/pkg1/libraryoneonly/Library1Source.java')
        }
    }
dependencies {
    compile project(':library2')
}
</code></pre>

<p>And library2 build.gradle.  Surprisingly, the uncommented exclude below does work.</p>

<p><strong>library2/build.gradle</strong></p>

<pre><code>apply plugin: 'com.android.library'

android {
        sourceSets {
            main.jni.srcDirs = []
            main.jniLibs.srcDir 'src/main/libs'
            main.java {
                srcDir "../../java/src"
                //exclude  {  FileTreeElement elem -&gt; ((elem.path.contains('/libraryoneonly/')) ) }
                exclude ('com/company/product/pkg1/libraryoneonly/Library1Source.java')
            }
        }
</code></pre>

<p>Just to note, I never see the "Searching for files, found" text when library1 is building.  I'm not sure why.  I do see it in the larger project this test was modeled from, depending on where the build was run from.</p>

<p>If you'd like to pull the sample project and try it out, here is a link:  <a href="https://github.com/gbak/gradleTest">https://github.com/gbak/gradleTest</a></p>