6

If I have 3 flavors flavor1, flavor2 and flavour3, each flavor has Dev,Pat and Prod "sub-flavours" versions which different parameters , but each main flavor have different resources.

So I have now 9 different flavors, but only 3 different resource folders). I would like the same "sub-flavors" to use the same resources.

How can I do that? I have seen in the documentation about the flavorDimensions but not sure how to configure the resource folder.

At the moment I am using something like

sourceSets {

   flavor1_dev{
            res.srcDir  'src/flavor1/res'
        }

   flavor1_prod{
            res.srcDir  'src/flavor1/res'
        }

   flavor2_dev{
            res.srcDir  'src/flavor2/res'
        }

   flavor2_prod{
            res.srcDir  'src/flavor2/res'
        }

}    
Kenenisa Bekele
  • 835
  • 13
  • 34
  • Yeah it works but I would like to know how to use the flavorDimensions , or if there is any other way to do it. – Kenenisa Bekele Feb 08 '16 at 17:14
  • You can exclude resources from different flavor builds, check this https://stackoverflow.com/questions/33263567/how-to-exclude-res-folder-from-gradle-build-flavours/45581826#45581826 – lxknvlk Aug 09 '17 at 04:49

2 Answers2

1

You need Gradle flavours eg. flavor1, flavor2 etc, as well as Build Types like dev, prod etc.

See the example from: http://developer.android.com/tools/building/configuring-gradle.html and http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies

For example:

android.sourceSets.flavor1Debug Location src/flavor1Debug/

android.sourceSets.flavor1Release Location src/flavor1Release/

android.sourceSets.flavor2Debug Location src/flavor2Debug/

android.sourceSets.flavor2Release Location src/flavor2Release/

Also, this question is very similar How can I specify per flavor buildType sourceSets?

Community
  • 1
  • 1
SteveEdson
  • 2,485
  • 2
  • 28
  • 46
  • Any idea on how to do it with flavorDimension ? http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-flavor-variants – Kenenisa Bekele Feb 08 '16 at 17:41
-2

Just put common resources in the main/res. All the resources in the main/res will be overridden by the flavour. For example if you want to override string resources add,

main string file: /src/main/res/values/strings.xml

<resources>
    <string name="app_name">Main</string>
    <string name="app_name_full">Main app</string>
    <string name="app_email">main@abc.com</string>
    <string name="app_phone">123</string>
    <string name="app_website">www.abc.com</string>
</resources>

and flavour string file: /src/flavor1/res/values/strings.xml

<resources>
    <string name="app_name">Flavour App Name</string>
</resources>

Only app_name is going to changed in the flavour and other resources like app_full_name is still accessible in all flavours.

Shailesh
  • 490
  • 5
  • 11
  • 2
    You cannot use main and expect it to be overridden. This will not work. Main should be used for "in everything", else it needs to be separated. – StarWind0 Jul 08 '16 at 13:16
  • 1
    @StarWind has a point here this will fail at compile time with "duplicate resources" – box Aug 20 '17 at 21:08