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.
The sample file tree is simple:
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
The library1 build.gradle looks like this. Neither of the includes or excludes work below.
library1/build.gradle
apply plugin: 'com.android.library'
android {
sourceSets {
main.jni.srcDirs = []
main.jniLibs.srcDir 'src/main/libs'
main.java {
srcDir "../../java/src"
exclude { FileTreeElement elem ->
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')
}
And library2 build.gradle. Surprisingly, the uncommented exclude below does work.
library2/build.gradle
apply plugin: 'com.android.library'
android {
sourceSets {
main.jni.srcDirs = []
main.jniLibs.srcDir 'src/main/libs'
main.java {
srcDir "../../java/src"
//exclude { FileTreeElement elem -> ((elem.path.contains('/libraryoneonly/')) ) }
exclude ('com/company/product/pkg1/libraryoneonly/Library1Source.java')
}
}
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.
If you'd like to pull the sample project and try it out, here is a link: https://github.com/gbak/gradleTest
I finally figured out the way to include a single file, without modifying the srcDir
path:
main.java {
srcDir "../../java/src"
setIncludes(new HashSet(['com/company/product/pkg1/libraryoneonly/*.java']))
}
The path '**/*.java'
is automatically included as an include, adding any more includes, like a direct include to the source file does nothing, since it is already included path anyway.
The setIncludes()
call replaces any existing includes, including the '**/*.java'
and sets the provided path[s] as the only include path.
Reading over the docs and adding the suggested printf statements after the main.java helped.
main.java.getIncludes().each { println "Added include: $it" }
main.java.sourceFiles.each { println "File in source set: " + it }
Thanks to all who have helped out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With