Gradle flavors with duplicate names but different dimension

820 Views Asked by At

Is it possible to have the following setup:

flavorDimension "production", "staging"

productFlavors {
    staging {
        dimension "staging"
        ...
    }

    production {
        dimension "production"
        ...
    }

    flavorOne {
        dimension "staging"
        ...
    }

    flavorOne {
        dimension "production"
        ...
    }

}

flavorOne exists two times and contains different settings but also with different dimensions.

What I need is a flavor flavorOne with production and staging and also res files for flavorOneStaging and flavorOneProduction. If I am doing it like this, I only see flavorOneStagingDebug and flavorOneStagingRelease (release and debug are my buildTypes) but nor flavorOneProduction..

Do I have to create a flavor for every combination? Staging and production contains data that never changes except res files and flavorOne is just one of N. I don't want to create N*2 flavors to have every flavor in production and staging? Any solutions?

3

There are 3 best solutions below

0
On

What you need is:

flavorDimension "environment", "flavorType"

productFlavors {
    staging {
        dimension "environment"
        ...
    }

    production {
        dimension "environment"
        ...
    }

    flavorOne {
        dimension "flavorType"
        ...
    }

    flavorTwo {
        dimension "flavorType"
        ...
    }

}

Assuming your build types are release and debug, this will produce:

staging-flavorone-release.apk
staging-flavorone-debug.apk
staging-flavortwo-release.apk
staging-flavortwo-debug.apk
release-flavorone-release.apk
release-flavorone-debug.apk
release-flavortwo-release.apk
release-flavortwo-debug.apk
4
On

Please read: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-flavor-variants

There you should see that you should to this the other way round: have production and staging applied as flavors and maybe then apply dimensions on them (if its still needed).

0
On

AFAIK there is no default way to do this - I had the same problem once and solved it like this:

applicationVariants.all { variant ->

    def variantName = variant.name
    if (variantName == "flavorOneProduction") {
        variant.buildConfigField "String", "FOO",'"BAR"'
    } else if (variantName == "flavorOneStaging") {
        variant.buildConfigField "String", "FOO",'"BAR2"'
    } else if (variantName == "flavorTwoProduction") {
        variant.buildConfigField "String", "FOO",'"BAR3"'
    } else if (variantName == "flavorTwoStaging") {
        variant.buildConfigField "String", "FOO",'"BAR4"'
    } 
}

I think you get the gist