How to use source sets of one module in another?


(Arjun Patel) #1

I have JAVA library module along with sets of JUnit tests (This library are being used by other Android Application modules). I am able to run JUnit tests. However, in past we had situation where certain tests would behave differently when ran using Sun JVM than Android Dalvik VM. So I have created Android test project which includes JAVA library. However, I don’t want to copy JUnit tests from JAVA library into Android test library (mainly to avoid code duplication). How can I use source sets defined in JAVA library into Android test application ?

I have tried below but not success. androidTestCompile files(project(":javaLib").sourceSets.test.java.srcDirs)


(Peter Niederwieser) #2

Referencing another project’s object model (as in ‘project(":javaLib").sourceSets’) is problematic, and should be avoided. Instead, it’s best to explicitly configure the sources for the Android test application (in its own ‘sourceSets’ block), wherever they may come from. If you absolutely need to share configuration code between the two projects, configure them from a parent project (‘subprojects { sourceSets { … } }’), or have both projects apply the same script plugin (‘apply from: “$rootDir/gradle/testSources.gradle”’).


(Arjun Patel) #3

Hello Peter, Thanks for the tip, but JAVA and Android both requires different sourceSets (per naming conventions). Is it possible to write common sourceSets ? Can you provide example ? or link to any existing project ? Thanks


(Peter Niederwieser) #4

Unifying Android and Java source sets is something we are working towards. In the meantime, you can just configure the Android source set with the same directory paths as the Java source set.