RandomType
RandomType

Reputation: 57

How to have optional flavors in Gradle for Android

I am trying to find a way to have optional flavors without being forced to have a "disabled" flavor of the same dimension.

For example, I have defined some dimensions and flavors here:

flavorDimensions flavors.flavorDefaultDimension, "featureX", "featureXType"
productFlavors {
    product {
        dimension flavors.flavorDefaultDimension
        getIsDefault().set(true)
    }

    noX{
        dimension "featureX"
        getIsDefault().set(true)
    }
    x{ dimension "featureX" }

    noType {
        dimension "featureXType"
        getIsDefault().set(true)
    }
    udp { dimension "featureXType" }
    serial { dimension "featureXType" }

}


The rules that I want are:

In the variant filter I use:

// Filter flavors that we do not want
variantFilter { variant ->
    def names = variant.flavors*.name
    // Ignore builds where `Udp` or `Serial` is used without `x`
    if ((names.contains("udp") || names.contains("serial")) && names.contains("noX")) {
        setIgnore(true)
    }
    // Ignore build `noType` with `x`
    if (names.contains("noType") && names.contains("x")) {
        setIgnore(true)
    }
}

This produce the following Build Variants:

productNoXNoTypeDebug
productNoXNoTypeRelease
productXUdpDebug
productXUdpRelease
productXSerialDebug
productXSerialRelease

but I would like to have:

productDebug
productRelease
productXUdpDebug
productXUdpRelease
productXSerialDebug
productXSerialRelease

As you can see I want all the default dimensions to be removed from the final flavors so that it is simpler for a user to read it.

Is there a better way to have optional flavors without forcing to have a "noSomething" flavor just to have the flavor disabled?

I have checked missing Dimension Strategy article: https://kiranrao.in/blog/2020/03/31/gradle-missing-flavors/ but I dont understand how this will solve the problem.

In addition, I have seen something called Feature Flags in gradle to enable or disable features for specific flavor? For a given set of flavors if a feature is enabled, then add this feature name in the final flavor and build variant. How can I do this?

Upvotes: 0

Views: 61

Answers (0)

Related Questions