Search code examples
androidgradleandroid-gradle-plugingradle-kotlin-dsl

Android gradle plugin(7.0.0-alpha15) removed variantFilter property, how to restore functionality?


current 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)
        }
    }
}

How to create similar config in the new version of plugin?

UPDATE: Using answer, code above will be as below:

    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))
            }
        }
    }

Solution

  • The Variant API is going to change to a lazily-evaluated model in AGP 7.0.0, and it seems like Alpha 15 has removed the old APIs for this now. Going forward, you will need to use the androidComponents DSL, which gives access to variants. Check out the beforeVariants block to selectively disable your variants:

    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)
      }
    }