8

I'm converting my build.gradle to Kotlin DSL. I have 2 build flavors in the app, and I can't figure out how to set the dimension for the flavors:

flavorDimensions("type")

productFlavors {
    create("free") {
        buildConfigField("boolean", "IS_DONATE", false.toString())
        dimension = "type"
    }
    create("donate") {
        buildConfigField("boolean", "IS_DONATE", true.toString())
        dimension = "type"
    }
}

the dimension = "type" part is failing; how do you set the dimension to each flavor?

Francesc
  • 25,014
  • 10
  • 66
  • 84

3 Answers3

23

Use setDimension("type")

flavorDimensions.add("type")

productFlavors {
    create("free") {
        buildConfigField("boolean", "IS_DONATE", false.toString())
        setDimension("type")
    }
  
    create("donate") {
        buildConfigField("boolean", "IS_DONATE", true.toString())
        setDimension("type")
    }
}
Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
  • 2
    Well, this is awkward, I had tried that, and it was showing in red, as if it was an error, but I just tried again, and now it flies. – Francesc Feb 18 '19 at 16:15
  • @Francesc wasn't it so because gradle needed to resync and the project needed to rebuild? same thing as with annotations and generated code (e.g. dagger) – Antek Apr 07 '20 at 15:53
  • flavorDimensions was deprecated. you should use flavorDimensions.add – Majid Sadeghi Jul 03 '22 at 07:50
3

If you are using a newer gradle version with kotlin script:

flavorDimensions.add("type")
    
productFlavors {
    create("free") {
        dimension = "type"
        buildConfigField(...)
    }
}
Javatar
  • 2,518
  • 1
  • 31
  • 43
2

As an addition to the answer of Shweta Chauhan, in new versions of Gradle, setDimension("type") is deprecated.

You should use dimension("type") instead.

enoler
  • 179
  • 1
  • 11