Search code examples
androidgradleandroid-gradle-plugin

Exclude specific build variants


I have the two default build types: debug / release and a couple of flavors: prod / dev.

Now I want to exclude the build variant dev-release, but keep all other possible combinations. Is there a way to achieve this?


Solution

  • Variant filter

    Use the the android gradle plugin to mark certain combinations as ignored, here is an example for AGP 7+ in Kotlin DSL from the official documentation that works with flavor dimensions and shows how it can be used:

    android {
      ...
      buildTypes {...}
    
      flavorDimensions += listOf("api", "mode")
      productFlavors {
        create("demo") {...}
        create("full") {...}
        create("minApi24") {...}
        create("minApi23") {...}
        create("minApi21") {...}
      }
    }
    
    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.enable = false
            }
        }
    }
    ...
    

    Note the comment on checking variantBuilder.buildType to validate the buildType.