2

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

alrama
  • 618
  • 11
  • 17

2 Answers2

3

It seems like customer dimension takes part for targetA only. Probably, you need targetB without any customer dimension. So I would try to add a new flavor for the default customer and variantFilter by target dimension.

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


    variantFilter { variant ->
        def names = variant.flavors*.name
        switch (variant) {
            case { names.contains("targetA") and names.contains("customerUndefined") }:
                setIgnore(true)
                break
            case { names.contains("targetB") and names.contains("customerA") }:
                setIgnore(true)
                break
            case { names.contains("targetB") and names.contains("customerB") }:
                setIgnore(true)
                break
            default:
                break
        }
    }
Elizabeth
  • 116
  • 1
  • 3
  • I don't know what setIgnore do, but your solution could be fine for me. When build for targetA I should choose "releaseTargetACustomerUndefined" . Next days I'll give it a try. – alrama Apr 07 '23 at 13:21
  • Result list of build variants is a combination of your dimensions and build types. Here I've added new customer with no `applicationIdSuffix` modification. `setIgnore()` allows to mute build types. `variantFilter` block excludes customerA and customerB from targetB and customerUndefined from targetA. So you will have 3 release variants: targetACustomerARelease, targetACustomerBRelease, targetBCustomerUndefinedRelease. – Elizabeth Apr 07 '23 at 13:46
  • Maybe I cannot use your solution. I need to select source from companyA and companyB java/res folders when building for targetB too, but choosing companyUndefined exclude this – alrama Apr 07 '23 at 14:15
  • I have to assign the bounty. I give it to you because cannot give it to my own answer. – alrama Apr 20 '23 at 08:22
1

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"
            }
        }
}
alrama
  • 618
  • 11
  • 17