androidgradleandroid-gradle-pluginbuild.gradleandroid-productflavors

android different applicationId & applicationIdSuffix by flavors chains


I have 2 target android flavor, with 2 different customer flavor. I need:

  1. first target with an appId and different suffix by customer
  2. second target with a different appId without any suffix.

My tries:

flavorDimensions "customer","target"
productFlavors {
    customerA {
        dimension "customer"
        applicationId = "com.mycompany.newappname.customerA"
    }
    customerB {
        dimension "customer"
        applicationId = "com.mycompany.newappname.customerB"
    }
    targetA{
        dimension "target"
    }
    targetB{
        dimension "target"
        applicationId = "com.mycompany.oldappname"
    }
}

it doesn't work on targetB. I get appId defined in dimension customer, not the one in dimension target.

And I tried:

defaultConfig {
    applicationId "com.mycompany.newappname"
}
...
flavorDimensions "customer","target"
productFlavors {
    customerA {
        dimension "customer"
        applicationIdSuffix = ".customerA"
    }
    customerB {
        dimension "customer"
        applicationIdSuffix = ".customerB"
    }
    targetA{
        dimension "target"
    }
    targetB{
        dimension "target"
        applicationIdSuffix = ""
        applicationId = "com.mycompany.oldappname"
    }
}

but I get on resulting targetB appId= "com.mycompany.oldappname.customerB"

EDIT

I have specialized code in folders java/res of companyA/companyB needed both for targetA and targetB


Solution

  • Found the solution, developing on this answer and this other one

    applicationVariants.all { variant ->
            variant.outputs.each { output ->
                if (variant.productFlavors[1].name.equals("targetb")) {
                    variant.mergedFlavor.applicationId = "com.mycompany.oldappname"
                } else if (variant.productFlavors[0].name.equals("companya")) {
                    variant.mergedFlavor.applicationId = "com.mycompany.newappname.companya"
                } else if (variant.productFlavors[0].name.equals("companyb")) {
                    variant.mergedFlavor.applicationId = "com.mycompany.newappname.companyb"
                }
            }
    }