Android Studio : How to remove/filter build variants for default debug and release buildTypes and keep only those using custom buildTypes?

AndroidMechanic - Viral Patel picture AndroidMechanic - Viral Patel · Dec 17, 2015 · Viewed 7.6k times · Source

I have created custom buildTypes as follows:

 buildTypes {
        releasefree.initWith(buildTypes.release)
        releasefree {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        releasepro.initWith(buildTypes.release)
        releasepro {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationIdSuffix ".pro"
        }
        debugfree.initWith(buildTypes.debug)
        debugfree {
            shrinkResources true
            applicationIdSuffix ".debug"
            debuggable true
        }
        debugpro.initWith(buildTypes.debug)
        debugpro {
            shrinkResources true
            applicationIdSuffix ".pro.debug"
            debuggable true
        }
    }

I am not going to use the default debug and release build types ever and want to remove them from the build variants list. I have more than a few flavors and the list of variants is too huge. Removing the variants with default debug and release types will help as I'm never going to use them.

I tried using variant filter as follows but it did not work

android.variantFilter { variant ->
    if(variant.buildType.name.endsWith('Release') || variant.buildType.name.endsWith('Debug')) {
        variant.setIgnore(true);
    }
}

Is there something wrong in the way I'm filtering the variants or is it just not possible to remove the variants with default debug and release build types.

Answer

Figured it out. It was a really silly mistake on my part. The above variant filter does work. The names are all lower case and the upper case in the strings i was comparing with were the culprit.

Changing to the following (making compare strings lower case) made it work as expected:

android.variantFilter { variant ->
    if(variant.buildType.name.endsWith('release') || variant.buildType.name.endsWith('debug')) {
        variant.setIgnore(true);
    }
}

or this

android.variantFilter { variant ->
    if(variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
        variant.setIgnore(true);
    }
}