I'm trying to work with build flavors. In my build.gradle I've defined 2 flavors, a normal flavor and an admin flavor.
Basicly the admin flavor has an extra button on the main activity.
I understand that I can define different packages/classes for different flavours. But is there a way to make a sort of if case to add/remove a piece of code depending on the flavor?
Basicly I would need two versions of an Activity. But I don't want two entire different versions of the activity and maintain them.
So in my activity I would like to do
=> gradle check if flavour is 'admin' => if yes add this code of the button
is this possible? Or would you need two different physical activities and thus maintain both of them when you add functionality afterwards.
To avoid plain string in the condition, you can define a boolean property:
Then you can use it like this:
You can define either different build configuration fields or different resource values (like string values) per flavor, e.g. (as per Google's gradle tips and recipes), e.g.,
So in this case, something like
and then use it like
or if it is just to have different string values for different flavors, it can be done with different
resValues
and then you don't even need theif/then
BuildConfig.FLAVOR
gives you combined product flavor. So if you have only one flavor dimension:Then you can just check it:
But if you have multiple flavor dimensions:
there are also
BuildConfig.FLAVOR_access
andBuildConfig.FLAVOR_color
fields so you should check it like this:And
BuildConfig.FLAVOR
contains full flavor name. For example,adminBlue
.