How to exclude (ignore) android build variants in gradle kts

3.1k Views Asked by At

During migration build script from groovy to kotlin I met problem with excluding build variants.

In groovy it's pretty easy:

android {
    variantFilter { variant ->
        if (variant.name == "lorempisum") {
            setIgnore(true)
        }
    }
}

but in kotlin similar thing does not work. It seems to be ok in android studio, but during compilation it throws Unresolved reference: isIgnore

android {
    variantFilter {
        if (buildType.name == "lorempisum") {
            isIgnore = true
        }
    }
}

from the other side this reports Unresolved reference: setIgnore, but works during compilation

android {
    variantFilter {
        if (buildType.name == "lorempisum") {
            this.setIgnore(true)
        }
    }
}

Anybody has idea how do it in right way?

I'm using kotlin 1.3.72, android studio 4.0.1 and gradle 6.5.1

---- EDIT ----

I fix example ignore -> isIgnore in second block, it also not works

3

There are 3 best solutions below

0
On BEST ANSWER

Soultion is ignore = true with a little deatil.

This works if you keep in top-level build.gradle.kts this line:

classpath("com.android.tools.build:gradle:4.0.1")

and not really only on buildSrc on this:

implementation("com.android.tools.build:gradle:4.0.1")

2
On

You should first update to the last version of android studio and the plugins. And try this

variantFilter {
    this.ignore = name == "lorempisum"
}
0
On

Use the beforeVariants block:

androidComponents {
    beforeVariants { variantBuilder ->
        // To check for a certain build type, use variantBuilder.buildType == "<buildType>"
        if (variantBuilder.productFlavors.containsAll(listOf("api" to "minApi21", "mode" to "demo"))) {
            // Gradle ignores any variants that satisfy the conditions above.
            variantBuilder.enabled = false
        }
    }
}