user116587
user116587

Reputation:

Letting multiple flavorGroups influence packageName in Android gradle build

I have a build configuration with 2 build types (debug/release) and 2 flavorGroups (locale/environment).

These are 3 different axes, and I want to determine the packageName of the build variant by all of them.

However, it seems I can only set the full packageName for a given flavor, and then have a packageNameSuffix for the build type - attempting to do a packageNameSuffix for a flavour leads to an Could not find method packageNameSuffix() for arguments [...] error.

Any way around that, so that I could get a package name for each of the resulting build variants, along the lines of: com.app.LOCALE.ENVIRONMENT.TYPE, without having to "unroll" one of the axes into build types (which would lead to duplication)?

Thanks in advance.

Upvotes: 1

Views: 1125

Answers (2)

Fredrik
Fredrik

Reputation: 477

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

In short, you can find out the combined variant using variantFilter and then update the configuration (e.g. 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.equalsIgnoreCase("fooBar")) {
        variant.getDefaultConfig().applicationId "com.example.foobar"
    }
}

Upvotes: 0

Michael Barany
Michael Barany

Reputation: 1669

You could use a combination of packageName and packageNameSuffix from productFlavors and buildTypes like the following:

android {
    productFlavors {
        foo {
            packageName "com.example.foo"
            versionName "Foo"
        }
        bar {
            packageName "com.example.bar"
            versionName "Bar"
        }
    }
    buildTypes {
        debug {
            packageNameSuffix ".debug"
            versionNameSuffix "-debug"
        }
        release {
            packageNameSuffix ".release"
            versionNameSuffix "-release"
        }
    }
}

But if you're using something like flavorGroups, then this may not work for you. You can also check out this code sample config which adds new buildTypes https://github.com/bradmcmanus/Gradle-Build-Example/blob/master/GradleBuildExample/build.gradle

Upvotes: 0

Related Questions