Android gradle plugin(7.0.0-alpha15) 移除了 variantFilter 属性,如何恢复功能?
Android gradle plugin(7.0.0-alpha15) removed variantFilter property, how to restore functionality?
当前 build.gradle.kts:
android {
// ...
variantFilter {
ignore = run {
val isSim = flavors[0].name == "sim"
val isNet = !isSim
val isU = flavors[1].name == "custom"
val isMain = !isU
val buildType = buildType.name
val isDebug = buildType == "debug"
val isRelease = buildType == "release"
(isSim && isRelease) ||
(isSim && isU)
}
}
}
如何在新版插件中创建类似的配置?
更新:
使用答案,上面的代码如下:
android {
androidComponents.beforeVariants {
it.enabled = run {
val isSim = it.productFlavors[0].second == "sim"
val isNet = !isSim
val isU = it.productFlavors[1].second == "custom"
val isMain = !isU
val isDebug = it.buildType == "debug"
val isRelease = it.buildType == "release"
!((isSim && isRelease) || (isSim && isU))
}
}
}
变体 API 将在 AGP 7.0.0 中更改为惰性评估模型,Alpha 15 似乎已经为此删除了旧的 API。展望未来,您将需要使用 androidComponents DSL,它可以访问变体。查看 beforeVariants
块以有选择地禁用您的变体:
android {
androidComponents.beforeVariants { variantBuilder ->
// variantBuilder provides access to buildType and flavor names.
// It has an 'enabled' property which you could use to disable some variants
// (logic may need to be inverted from your existing 'ignore' block)
}
}
当前 build.gradle.kts:
android {
// ...
variantFilter {
ignore = run {
val isSim = flavors[0].name == "sim"
val isNet = !isSim
val isU = flavors[1].name == "custom"
val isMain = !isU
val buildType = buildType.name
val isDebug = buildType == "debug"
val isRelease = buildType == "release"
(isSim && isRelease) ||
(isSim && isU)
}
}
}
如何在新版插件中创建类似的配置?
更新: 使用答案,上面的代码如下:
android {
androidComponents.beforeVariants {
it.enabled = run {
val isSim = it.productFlavors[0].second == "sim"
val isNet = !isSim
val isU = it.productFlavors[1].second == "custom"
val isMain = !isU
val isDebug = it.buildType == "debug"
val isRelease = it.buildType == "release"
!((isSim && isRelease) || (isSim && isU))
}
}
}
变体 API 将在 AGP 7.0.0 中更改为惰性评估模型,Alpha 15 似乎已经为此删除了旧的 API。展望未来,您将需要使用 androidComponents DSL,它可以访问变体。查看 beforeVariants
块以有选择地禁用您的变体:
android {
androidComponents.beforeVariants { variantBuilder ->
// variantBuilder provides access to buildType and flavor names.
// It has an 'enabled' property which you could use to disable some variants
// (logic may need to be inverted from your existing 'ignore' block)
}
}