6

I have a Gradle Android Project with this product Groups and Flavors configuration:

/*
 * Define different flavor groups
 */
flavorGroups 'market', 'version'

/*
 * Defile product flavors
 */
productFlavors {

    amazon {
        flavorGroup 'market'
    }

    google {
        flavorGroup 'market'
    }

    flav1 {
        flavorGroup 'version'
        packageName 'com.company.flav1'
    }

    flav2 {
        flavorGroup 'version'
        packageName 'com.company.flav2'
    }

    flav3 {
        flavorGroup 'version'
        packageName 'com.company.flav3'
    }
}

// .. Other stuff

It works great. All sources and resources are merged correctly. But for specific reasons I need the package suffix to be .amz for the amazon product flavor. How can I achieve that?

I tried this way:

amazon {
    flavorGroup 'market'
    packageNameSuffix '.amz'
}

but gradle throws an exception.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Serj Lotutovici
  • 4,370
  • 5
  • 32
  • 41
  • I once had a similar question, it may help: http://stackoverflow.com/q/18606152/2331953 – flx Dec 06 '13 at 11:55
  • I read this before @fix. By the way helped me a lot, when I started with this project. But the problem is that I'm not adding any suffix, I need to add one. Preferably somewhere in the debug and release tasks. – Serj Lotutovici Dec 06 '13 at 12:25

3 Answers3

11

This is not possible at the moment. packageNameSuffix is strictly a property of Build Type.

I'd like to offer a way to customize the ProductFlavor's values for a given variant (ie a given combination of all flavor dimensions and of build type) but it's not possible at the moment.

Instead of a new dimension of flavor, you could do a "amzRelease" buildtype that extends the existing release buildtype and add a suffix.

If your current "amazon" flavor does more (configure versionCode/Name/etc...), it won't work though. You could then use both your amazon flavor and a amzRelease build type. It'll create a lot more variants than you need but it'd work until we have some better.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • Thanks Xavier for your feedback. We decided to stick with same package name. This was required for our API to understand which users are accessing it, so I added a secret param as an workaround. Will be waiting for the plugin update. Your doing a great job, thanks. – Serj Lotutovici Dec 07 '13 at 13:07
  • i wanted to add - this is also required for our scenario. if there is any progress on this, it would be great. – Andreas Petersson Feb 24 '14 at 15:39
1

You could use a variant of the solution I've written about here: https://stackoverflow.com/a/26585241/4177090

In short, you can find the combined variants using variantFilter and then update the appliciationId from there:

android.variantFilter { variant ->
    def flavorString = ""
    def flavors = variant.getFlavors()
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }
    if(flavorString.contains("amazon")) {
        variant.getDefaultConfig().applicationId variant.getDefaultConfig().applicationId + ".amz"
    }
}
Community
  • 1
  • 1
Fredrik
  • 487
  • 5
  • 12
0

Working solution for gradle 1.0.0+:

android.applicationVariants.all { variant ->
    def flavorName = variant.getVariantData().getVariantConfiguration().getFlavorName()
    def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();

    if (flavorName.toLowerCase().contains("amazon")) {
        mergedFlavour.setApplicationId(mergedFlavour.getApplicationId() + ".amz")
    }
}