Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Multi-Dimension Flavor ApplicationId

I have a relatively complicated project that requires two flavor dimensions for each app. I've rewritten it much more simply in the example below:

flavorDimensions "shape", "color"

productFlavors {

     blue {
         flavorDimension "color"
     }

     red {
         flavorDimension "color"
     }

     green {
         flavorDimension "color"
     }


     square {
         flavorDimension "shape"
     }

     circle {
         flavorDimension "shape"
     }

I want to be able to set a different applicationId for each variant, eg: squareblue would have a different applicationId to circleblue. I can't set the applicationId in the color dimension because it would be the same for each shape. I would need to have 6 different applicationIds in the above example. These Ids also don't follow any pattern, they could be anything.

I've seen the answer here: How to set different applicationId for each flavor combination using flavorDimensions? but that would mean I need to set it up manually, which isn't feasible for my project, due to the number of variants (1000s).

What I really want to do is set two applicationids on the color dimension, then it picks the correct one, depending on the shape dimension, when it's built. I've tried defining variables but haven't had any success with that, they just get overwritten by the last variant.

like image 932
David Scott Avatar asked Oct 28 '14 15:10

David Scott


1 Answers

Gradle has an extras property built in, so you could do this without defining a class.

Would look something like this, might have made a typo or two:

productFlavors {
    blue {
        flavorDimension "color"
        ext.squareId = "yourAppId"
        ext.circleId = "yourAppId"
    }

    android.applicationVariants.all { variant ->
        def flavors = variant.getFlavors()
        if (flavors[0].name.equals("square")){
            variant.mergedFlavor.setApplicationId(flavors[1].ext.squareId)
        } ...
    }
like image 136
Chris Avatar answered Oct 13 '22 07:10

Chris