0

I am defining productFlavors in my build.gradle file (the one in the app directory). Each flavor has a corresponding res/raw directory with .properties files corresponding to the build type (debug, test, and release). Within my main/res/raw directory, I have build.properties. Here's a visual of the directory structure:

  • src
    • flavor1
      • res
        • raw
          • dev.properties
          • prod.properties
          • test.properties
    • flavor2
      • res
        • raw
          • dev.properties
          • ...
    • ...
    • main
      • res
        • raw
          • build.properties

I would like to copy the contents of <flavor>/res/raw/<build_type>.properties into main/res/raw/build.properties and only deploy build.properties into the APK.

How can I determine which build variant is being built from within build.gradle in order to copy the correct <build_type>.properties file?

How can I exclude the <flavor>/res/raw folder from being built into the APK?

Sean Beach
  • 2,050
  • 4
  • 26
  • 35
  • I have found a solution and described it in this answer https://stackoverflow.com/questions/33263567/how-to-exclude-res-folder-from-gradle-build-flavours/45581826#45581826 – lxknvlk Aug 09 '17 at 04:46

1 Answers1

3

You can have folders for a flavor + build type combination, which will let you do what you want without having to copy and rename files:

src
  flavor1
  flavor1Debug
    res
      raw
        build.properties
  flavor1Prod
    res
      raw
        build.properties
  flavor2
  flavor2Debug
    res
      raw
        build.properties

etc.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Ah, ok. I feel like I should have thought of that. :P In your example, are `flavor1Debug` and `flavor1Prod` nested underneath `flavor1`, or are they on the same directory level? – Sean Beach Jul 29 '14 at 01:00
  • Your solution is not quite what I am wanting. After taking a look at my code again, I realize I DO want the three versions of the properties files available at compile time. For instance, I will sometimes need to access `prod.properties` and use those values while running a debug build. Because of that, I cannot simply hardcode the contents of `/res/raw/.properties` into `/res/raw/build.properties`. I would still like to know the answer to my question, not simply an alternative. Your solution is not wrong (so +1 for that), but it doesn't fit my requirements. – Sean Beach Jul 29 '14 at 14:53
  • To answer your first question, though you maybe figured it out already, the `flavor1Debug` folder goes alongside the `flavor1` folder, not underneath it. To provide some insight into your second question, I think you have to think about it a different way -- don't think about "what variant is being built", but instead build a task that does the copying, and find the right variant-specific task to make it depend on. Sorry, I'm not trying to be intentionally vague, but it would take some research for me to figure out exactly how to do it. I believe there's API for getting the current variant. – Scott Barta Jul 29 '14 at 15:11