andorid.flavorDimensions 数组按照先后排序定义了可能使用的 Dimensions 。每一个 Product Flavor 都被分配到一个 Dimensions 中。
上面的例子中将 Product Flavor 分为两组(即两个维度),分别为 price 维度 [free, paid] 和 store 维度 [google, amazon] ,再加上默认的 Build Type 有 [debug, release] ,这将会组合生成以下的 Build Variant:
- free-google-debug
- free-google-release
- free-amazon-debug
- free-amazon-release
- paid-google-debug
- paid-google-release
- paid-amazon-debug
- paid-amazon-release
每一个 Variant 版本的配置由几个 Product Flavor 对象决定:
- android.defaultConfig
- 一个来自 price 组中的对象
- 一个来自 store 组中的对象
android.flavorDimensions 中定义的 Dimensions 排序非常重要(Variant 命名和优先级等)。
flavorDimensions 中的排序决定了哪一个 flavor 覆盖哪一个,这对于资源来说非常重要,因为一个 flavor 中的值会替换定义在低优先级的 flavor 中的值。
flavorDimensions 使用最高的优先级定义,因此在上面例子中的优先级为:price > store > defaultConfig
flavorDimensions 项目同样拥有额外的 sourceSet ,类似于 Variant 的 sourceSet ,只是少了 Build Type:
- android.sourceSets.freeGoogle
位于src/freeGoogle/ - android.sourceSets.paidAmazon
位于src/paidAmazon/
等等...
这允许在 flavor-combination 的层次上进行定制。它们拥有过比基础的 flavor sourceSet 更高的优先级,但是优先级低于 Build Type 的 sourceSet 。