Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which takes precedence, Gradle build types or flavors?

Tags:

android

gradle

Say I have a constant defined at both levels: in the build type I set it to "mybuild" and in the flavor I set it to "myflavor".

Such as in here:

buildTypes {
    debug {
        resValue "string", "analytics_key", "XXX_SANDBOX_KEY_XXX"
    }
}

productFlavors {
    appA {
        resValue "string", "analytics_key", "XXX_KEY_FOR_A_XXX"
    }
    appB {
        resValue "string", "analytics_key", "XXX_KEY_FOR_A_XXX"
    }
}

I want to send the events of the different apps (thta is, flavors) to different accounts in my analytics platform. But, if I'm debugging, I want to send them all to my sandbox account.

The original question is: which takes precedence? From my test, I can already answer that: the one in the build type.

However, the more interesting one is: is this guaranteed?

(Or, is there a better way to do this?)

like image 287
espinchi Avatar asked Dec 24 '22 22:12

espinchi


1 Answers

is this guaranteed?

Build types generally are considered higher priority than are product flavors for things at the same level (e.g., the same module). Quoting the documentation:

Usually, the Build Type configuration is an overlay over the other configuration. For instance, the Build Type's packageNameSuffix is appended to the Product Flavor's packageName.

Manifest merging, though, is rather complicated, requiring its own set of docs.

In your case, you are creating resources. In that case, it should follow the same rules as if you had put these values as string resources in relevant sourcesets:

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

If resValue started behaving differently than the equivalent using sourcesets, I would consider that to be a bug.

like image 160
CommonsWare Avatar answered Dec 31 '22 13:12

CommonsWare