Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different applicationId for each flavor combination using flavorDimensions?

Tags:

I have and old android app that I am trying to migrate to the android gradle build system. The app is currently built in a multi project setup and published as four different apps (two different data sets included and free/paid versions for both datasets). I have managed to get away from the multi project setup by using flavorDimensions (previously called flavorGroups), but I can not figure out how to set a different applicationId for each flavor combination.

Since the app versions are already published, I need to keep the same applicationid as they currently have. Because of how my original package naming was done, I can not simply use flavor-buildtype combination with "packageNameSuffix" (which would have been a great option if it was an unpublished app).

https://stackoverflow.com/a/20956353/4177090 is answering how to use different source folders for flavor combinations, but not how (if even possible) to set specific configuration for each combination in the build file.

Gradle build file snippet:

flavorDimensions "price", "dataset"  productFlavors {     free { flavorDimension "price" }     paid { flavorDimension "price" }     dataset1 { flavorDimension "dataset" }     dataset2 { flavorDimension "dataset" } } 

I would like to have something like the following in my gradle build file (notice how unlogic my naming is, which is why I cannot use packageNameSuffix):

freeDataset1 { applicationId "com.beansys.freeappdataset1" } freeDataset2 { applicationId "com.beansys.freedataset2" } paidDataset1 { applicationId "com.beansys.dataset1paid" } paidDataset2 { applicationId "com.beansys.mypaiddataset2" } 
like image 596
Fredrik Avatar asked Oct 24 '14 12:10

Fredrik


People also ask

When would you use product flavor in your build setup?

123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

How do I add Flavours to my android?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.

What are build variants?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

How do I get the current flavor in Gradle?

There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.


1 Answers

The solution proposed by Fredrik stopped working after upgrading Android Studio to 1.0.2 (and gradle plugin to 1.0.0) so I had to add following changes, current as of gradle plugin 1.3.1:

flavorDimensions "price", "dataset"  productFlavors {     free { dimension "price" }     paid { dimension "price" }     dataset1 { dimension "dataset" }     dataset2 { dimension "dataset" } }  android.applicationVariants.all { variant ->     def mergedFlavor = variant.mergedFlavor     switch (variant.flavorName) {         case "freeDataset1":             mergedFlavor.setApplicationId("com.beansys.freeappdataset1")             break         case "freeDataset2":             mergedFlavor.setApplicationId("com.beansys.freedataset2")             break         case "paidDataset1":             mergedFlavor.setApplicationId("com.beansys.dataset1paid")             break         case "paidDataset2":             mergedFlavor.setApplicationId("com.beansys.mypaiddataset2")             break     } } 
like image 193
Iwo Banas Avatar answered Nov 08 '22 21:11

Iwo Banas