以下代码摘录自官网,仅供参考:官网地址
使用demo如下:
android {
...
buildTypes {
debug {...}
release {...}
}
// Specifies the flavor dimensions you want to use. The order in which you
// list each dimension determines its priority, from highest to lowest,
// when Gradle merges variant sources and configurations. You must assign
// each product flavor you configure to one of the flavor dimensions.
flavorDimensions "api", "mode"
productFlavors {
// Configurations in the "api" product flavors override those in "mode"
// flavors and the defaultConfig {} block. Gradle determines the priority
// between flavor dimensions based on the order in which they appear next
// to the flavorDimensions property above--the first dimension has a higher
// priority than the second, and so on.
minApi24 {
dimension "api"
minSdkVersion '24'
// To ensure the target device receives the version of the app with
// the highest compatible API level, assign version codes in increasing
// value with API level. To learn more about assigning version codes to
// support app updates and uploading to Google Play, read Multiple APK Support
versionCode 30000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi24"
...
}
minApi23 {
dimension "api"
minSdkVersion '23'
versionCode 20000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi23"
...
}
minApi21 {
dimension "api"
minSdkVersion '21'
versionCode 10000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi21"
...
}
demo {
// Assigns this product flavor to the "mode" flavor dimension.
dimension "mode"
...
}
full {
dimension "mode"
...
}
}
}
...
Gradle 创建的构建变体数量等于每个风味维度中的风味数量与您配置的构建类型数量的乘积。在 Gradle 为每个构建变体或对应 APK 命名时,属于较高优先级风味维度的产品风味首先显示,之后是较低优先级维度的产品风味,再之后是构建类型。以上面的构建配置为例,Gradle 可以使用以下命名方案创建总共 12 个构建变体:
构建变体: [minApi24, minApi23, minApi21] [Demo, Full] [Debug, Release] 这里就有322个变体
对应 APK:app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk
就是以flavorDimensions的 第一个参数为基准与后面的进行拼接, [minApi24, minApi23, minApi21] 这3个是必须要有。
例如,
构建变体:minApi24DemoDebug
对应 APK:app-minApi24-demo-debug.apk
除了可以为各个产品风味和构建变体创建源集目录外,您也可以为每个产品风味组合创建源集目录。例如,您可以创建 Java 源并将其添加到 src/demoMinApi24/java/ 目录中,Gradle 仅会在构建组合了这两种产品风味的变体时使用这些源。与属于各个产品风味的源集相比,您为产品风味组合创建的源集拥有更高的优先级
过滤变体
Gradle 会为您配置的产品风味与构建类型的每个可能的组合创建构建变体。不过,某些特定的构建变体在您的项目环境中并不必要,也可能没有意义。您可以在模块级 build.gradle 文件中创建一个变体过滤器,以移除某些构建变体配置。
以上一部分中的构建配置为例,假设您计划为演示版本的应用仅支持 API 级别 23 和更高级别。您可以使用 variantFilter {} 代码块过滤出组合了“minApi21”和“演示”产品风味的所有构建变体配置:
android {
...
buildTypes {...}
flavorDimensions "api", "mode"
productFlavors {
demo {...}
full {...}
minApi24 {...}
minApi23 {...}
minApi21 {...}
}
variantFilter { variant ->
def names = variant.flavors*.name
// To check for a certain build type, use variant.buildType.name == "<buildType>"
if (names.contains("minApi21") && names.contains("demo")) {
// Gradle ignores any variants that satisfy the conditions above.
setIgnore(true)
}
}
}
...
在您向构建配置添加变体过滤器并点击通知栏中的 Sync Now 后,Gradle 将忽略满足您指定的条件的任何构建变体,在您点击菜单栏中的 Build > Select Build Variant(或工具窗口栏中的 Build Variants)时,这些构建变体将不会再 显示在下拉菜单中。