Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle - Add packageNameSuffix on specific productFlavor

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.

like image 943
Serj Lotutovici Avatar asked Dec 06 '13 11:12

Serj Lotutovici


People also ask

How to add new build variant Android?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

How to select build variant in Android Studio?

To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar. For projects without native/C++ code, the Build Variants panel has two columns: Module and Active Build Variant.

What is buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


2 Answers

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.

like image 104
Xavier Ducrohet Avatar answered Sep 19 '22 10:09

Xavier Ducrohet


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"
    }
}
like image 27
Fredrik Avatar answered Sep 21 '22 10:09

Fredrik