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

前端 未结 3 975
栀梦
栀梦 2020-11-30 02:10

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 differen

相关标签:
3条回答
  • 2020-11-30 02:28

    I had problems converting the answers given to the gradle kotlin dsl. I found a solution and shared it here: https://stackoverflow.com/a/60103604/2793394

    0 讨论(0)
  • 2020-11-30 02:37

    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"
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:39

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题