Search code examples
androidgradlebuildandroid-productflavors

Setting the applicationId in gradle for a combined product flavor


I have an app with two productFlavors:

flavorDimensions "type", "country"

productFlavors {
    free {
        dimension "type"
    }
    premium {
        dimension "type"
    }
    us {
        dimension "country"
    }
    de {
        dimension "country"
    }
}

This leads to four buildVariants (ignoring build types).

Now my challenge is to set different applicationIds for these four variants but I cannot figure out how to do this. The only way I found to actually change the applicationId is inside the productFlavors definition like so:

productFlavors {
    free {
        applicationId "some.application.id"
        dimension "type"
    }

but the problem is, that the flavor is actually freeDe for instance and I would like to set the applicationId for that combined one.

I tried:

productFlavors {
    freeDe {
        applicationId "some.random.id"
    }

and

productFlavors {
    free {
        de {
            applicationId "some.random.id"
        }
    }

and a bunch of other combinations but none of them seem to work.

Important: I cannont use pre/suffix for that as the application ids are totally different from each other :(

Help is greatly appreciated!

Thanks and all the best!


Solution

  • Use it

    android.applicationVariants.all { variant ->
        def mergedFlavor = variant.mergedFlavor
        switch (variant.flavorName) {
            case "freeUs":
                mergedFlavor.setApplicationId("com.application.id.freeUs")
                break
            case "freeDe":
                mergedFlavor.setApplicationId("com.application.id.freeDe")
                break
    
        }
    }