5

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

Krystian Kaniowski
  • 1,959
  • 17
  • 33

3 Answers3

2

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

Krystian Kaniowski
  • 1,959
  • 17
  • 33
2

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
        }
    }
}
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
0

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

variantFilter {
    this.ignore = name == "lorempisum"
}
  • Using this `this.ignore` seems to be correct in IDE, but during compilation, it again throws `Unresolved reference: ignore`. I'm using latest stable android studio 4.0.1 and it not works on both gradle 6.5.1 and 6.6.1 – Krystian Kaniowski Sep 08 '20 at 08:14
  • 1
    Ok. I'm on android studio 4.1.0 RC2 and gradle 6.5. This gradle Kotlin DSL is better supported on 4.1 or 4.2. – souleymane sidibé Sep 09 '20 at 15:44