Exclude specific build variants

If you use flavor dimensions do this:

flavorDimensions "device", "server"

productFlavors {
    emulator {
        dimension = "device"
    }
    phone {
        dimension = "device"
    }
    staging {
        dimension = "server"
    }
    production {
        dimension = "server"
    }
}

android.variantFilter { variant ->
    def device = variant.getFlavors().get(0).name
    def server = variant.getFlavors().get(1).name
    def isRelease = variant.buildType.name.equals('release')
    def isDebug = variant.buildType.name.equals('debug')

    // Disable emulatorProductionRelease build variant
    if (device.equals('emulator') && server.equals('production') && isRelease) {
        variant.setIgnore(true)
    }
}

It's easy to read and you can target specific build variants.


Using variant filters like others I found it was easiest to do this by comparing the variant name against a list of variants that I want to keep.

So in my app/build.gradle file I have something like:

android {
    variantFilter { variant ->
        def needed = variant.name in [
                'stagingQuickDebug',       // for development
                'stagingFullDebug',        // for debugging all configurations
                'stagingFullCandidate',    // for local builds before beta release
                'stagingFullRelease',      // for beta releases
                'productionFullCandidate', // for local builds before going public
                'productionFullRelease'    // for public releases
        ]
        variant.setIgnore(!needed)
    }
    buildTypes {
        debug {
        }
        release {
        }
        candidate.initWith(release)
    }
    flavorDimensions "server", "build"
    productFlavors {
        staging {
            dimension "server"
            buildConfigField "String", "API_URL", '"https://example-preprod.com/"'
        }
        production {
            dimension "server"
            buildConfigField "String", "API_URL", '"https://example.com/"'
        }
        quick {
            dimension "build"
            minSdkVersion 21
            resConfigs("en", "xxhdpi")
        }
        full {
            dimension "build"
        }
    }
}

When working with flavor dimensions try this one

variantFilter { variant ->
    def dim = variant.flavors.collectEntries {
        [(it.productFlavor.dimension): it.productFlavor.name]
    }

    if (dim.dimensionOne == 'paid' && dim.dimensionSecond == 'someVal') {
        variant.setIgnore(true);
    }
}

Variant filter

Use the variantFilter of the gradle android plugin to mark certain combinations as ignored. Here is an example from the official documentation that works with flavor dimensions and shows how it can be used:

android {
  ...
  buildTypes {...}

  flavorDimensions "api", "mode"
  productFlavors {
    demo {...}
    full {...}
    minApi24 {...}
    minApi23 {...}
    minApi21 {...}
  }

  variantFilter { variant ->
      def names = variant.flavors*.name
      // To check for a certain build type, use variant.buildType.name == "<buildType>"
      if (names.contains("minApi21") && names.contains("demo")) {
          // Gradle ignores any variants that satisfy the conditions above.
          setIgnore(true)
      }
  }
}

As the comment says, you can also check the buildType like so:

android {
    variantFilter { variant ->
        def names = variant.flavors*.name
        if(variant.buildType.name == 'release' && names.contains("myforbiddenflavor")) {
            setIgnore(true)
        }
    }
}