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 Answers
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.
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.

- 6,533
- 2
- 37
- 67
-
What can I do in the gradle to say "only compile this library if the flavor is in dimension x"? – Rebekah Bryant Nov 11 '16 at 15:53
-
Instead of using `compile "com.android.support:support-v4:$supportLibraryVersion"` You should use `betaOldApiComplie "com.android.support:support-v4:$supportLibraryVersion"` Here, _beta_ and _oldApi_ two different flavors from two different dimensions – Gaket Nov 11 '16 at 19:41