Flavor Dimension source code

627 views Asked by At

I have a project with about 30 product flavors. Some of these product flavors need to share code, without sharing it with most product flavors. Can I assign code to a flavor group?

1

There are 1 answers

2
Gaket On

1) Do you know how to create folders for a particular product flavor? You can create a folder with sources or resources that will be used for a particular combination of flavors too. https://developer.android.com/studio/build/build-variants.html#sourcesets

For example, we have flavors: "beta", "prod" in one dimension and "newApi", "oldApi" in another. We use one class implementation for all the flavor combinations except when it is beta with new api. So we found how gradle names this buidle variant (betaNewApi), created folder project/app/src/betaNewApi and put our class there, saving project structure for packages. As a result, classes that need this particular class take usual one or this particular only in this combination of flavors.

Our example

2) If you need to share not a whole class but only some small part of code, you can use runtime-checks for flavors:

if (BuildConfig.FLAVOR_releaseType.equals("prod")) 
    && BuildConfig.FLAVOR_apiLvl.equals("newApi")) {
        // here is your shared code
}

We extracted constants like "prod" into our Application class and use them in such ways.