Home » Android » android – Different drawable based on build variant in Gradle

android – Different drawable based on build variant in Gradle

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two flavors in my gradle file: flavor1 and flavor2. Along with the build types, they create the following build variants:

flavor1-debug
flavor1-release
flavor2-debug
flavor2-debug

Assuming I want to have a different drawable for each variant, where should I put it?

My folder structure is

src
 |__ main
 |__ flavor1
 |__ flavor2
 |__ debug
 |__ release

I am aware, according to this that

All resources (Android res and assets) are used using overlay priority
where the Build Type overrides the Product Flavor, which overrides the
main sourceSet.

This means however that if I put it in the Build Type folder, then the debug variants will have the same debug drawable and the release variants will have the same release drawable.

I guess I could create more specific flavors from the beginning, e.g. flavor1release, flavor1debug, flavor2release, flavor2debug, but I would end up with 8 variants. I could probably not build some of the build types according to this, but that doesn’t seem very clean.

How to&Answers:

There are actually additional sourceSets that are available. You can do this:

src
 |__ main
 |__ flavor1
 |__ flavor1Debug
 |__ flavor1Release
 |__ flavor2
 |__ flavor2Debug
 |__ flavor2Release
 |__ debug
 |__ release

which I think will give you what you want — inside one of the flavor + build type folders, you can have your variant-specific resource in the appropriate resource subdirectory.

See the docs at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies for more information.