How to set different applicationId for each flavor

2019-01-10 18:03发布

问题:

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" }

回答1:

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
    }
}


回答2:

I finally managed to solve this. I think the solution is elegant (although the actual code could most likely be written a lot nicer by someone with groovy knowledge).

Solution for setting a specific applicationId for each combined flavor:

flavorDimensions "price", "dataset"

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

android.variantFilter { variant ->
    def flavorString = ""
    def flavors = variant.getFlavors()
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }
    if(flavorString.equalsIgnoreCase("freeDataset1")) {
        variant.getDefaultConfig().applicationId "com.beansys.freeappdataset1"
    }
    if(flavorString.equalsIgnoreCase("freeDataset2")) {
        variant.getDefaultConfig().applicationId "com.beansys.freedataset2"
    }
    if(flavorString.equalsIgnoreCase("paidDataset1")) {
        variant.getDefaultConfig().applicationId "com.beansys.dataset1paid"
    }
    if(flavorString.equalsIgnoreCase("paidDataset2")) {
        variant.getDefaultConfig().applicationId "com.beansys.mypaiddataset2"
    }
}