0

I have many product flavors and 2 build types debug and release, I have a task to copy of /src/test/res to testRootDir/res , now I need a way to compute the value of testRootDir depending on buildVariant right now I have separate copy tasks for each build variant, for example product flavor demo and build type release, i have task that copies from /src/test/res and puts it in ${buildDir}/intermediates/classes/test/demo/release/debug/res This is not feasible as I would have to write separate copy task for each variant, how do I handle this?

EDIT

Current solution (In pseudo code, taken from RaGe's comment)

for each flavor, for each buildType 
    { copy into $buildDir/something/$flavor.name/$buildType.name/res }

What I neeeed

But this solution is not complete as it will create multiple copies of res directories no matter which build variant is selected, I want to create copy task specific for the current build variant being built is this at all possible?

Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • You have a separate folder for every flavor ? – Shark Feb 04 '16 at 15:42
  • Nope, I dont ..is that really necessary? – Bhargav Feb 04 '16 at 15:46
  • you could do something along the lines of: `for each flavor, for each buildType { copy into $buildDir/something/$flavor.name/$buildType.name/res }`. However, don't flavors inherit res from main anyway? – RaGe Feb 04 '16 at 16:06
  • well, no it's not and yes it is, when you have a separate folder for every flavor, you don't need to copy all that stuff. the `main` flavor has everything, and the `yourFlavor1`, `yourFlavor2` flavors only contain the files that are specific/changed for those flavors. – Shark Feb 04 '16 at 16:06
  • @Shark as you can see these resources are not for my main src sets but for my unit tests, thanks RaGe I think I can build a solution from your comment – Bhargav Feb 04 '16 at 16:36
  • 1
    @RaGe but according to that code no matter the build variant selected there will be multiple copies of res in different folders in the build dir – Bhargav Feb 04 '16 at 16:42
  • I understood your question to mean that you *wanted* to create multiple res folders and copy res contents into them - without having to create a bunch of copy tasks. Attempting to parse your question again. – RaGe Feb 04 '16 at 17:48
  • well ideally I wouldnt want to create a bunch of copies no matter the build variant selected, the directory to which the res folder gets copied to should only be to the destination build test dir of the current build variant, thats what I want in an ideal scenario – Bhargav Feb 05 '16 at 01:51
  • You really should update the question to state what you want in ideal, less-than-ideal and worst-case scenarios. – Shark Feb 05 '16 at 08:46
  • @Shark done, its not that I want an ideal solution, whatever solution RaGe gave is not "complete", its a workaround something temporary I can use as long as there is very little assets in use for test – Bhargav Feb 05 '16 at 10:31
  • Isn't your current "solution" a workaround as well ? – Shark Feb 05 '16 at 10:37
  • 1
    @Shark yea exactly, and I gave proper attribution to RaGe in the brackets – Bhargav Feb 05 '16 at 10:47
  • @Shark anyway even though multiple tasks are created for each build variant(i.e if 4 product flavors and 2 build types then 4*2 = 8 build variants so 8 copy tasks) only 1 copy task runs depending on the variant as I define test task dependency only on one copy task, so creation of multiple copies is averted – Bhargav Feb 08 '16 at 11:55
  • @RaGe is there a way to maybe simplify this? `testAlphaDebugUnitTest.dependsOn(copyResToTestDirForAlphaDebug)` `testAlphaReleaseUnitTest.dependsOn(copyResToTestDirForAlphaRelease)` – Bhargav Feb 08 '16 at 12:13
  • The question you really should be asking is, how can I tell inside a task, what the current flavor and/or buildtype is. Unfortunately there doesn't seem to be a straightforward say. Saw some workarounds, see if this helps: http://stackoverflow.com/questions/25739163/how-to-get-current-buildtype-in-android-gradle-configuration – RaGe Feb 08 '16 at 16:40
  • However, I also feel you shouldn't be copying resources **at all** - that seems like a very un-gradle way to do things. You should instead be adding a common resource directory as a resDir in each flavor/buildType config - that way there is never more than 1 copy of common resources. – RaGe Feb 08 '16 at 16:42
  • What happens if you do not copy resources, but leave them in `/src/test/res`? Are they not picked up by all build variants? – RaGe Feb 08 '16 at 19:38
  • @RaGe nope, they're not my unit test fails, this line `URL is = getClass().getClassLoader().getResource("res/myfile");` myfile needs to be present in the build folders, and only way to do this is to run the copy task. The `res` that gradle handles is for the main source set, not the test – Bhargav Feb 09 '16 at 03:47

0 Answers0