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

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

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