androidandroid-gradle-pluginandroid-productflavorsandroid-build-type

Android Gradle. How to combine Flavors with buildTypes


I'm working on a white brand app.

We create a different flavor per client and each client has Debug and Production APIs, so I'm trying to set them up in the Gradle.

How should I do that?

Here is what I've tried:

buildTypes {
    debug {
        // some configurations
    }
    release {
        // some configurations
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
    }
}

EDIT: I would like to be able to define a different BASE_URL per each Flavor and Buildtype.

Flavor company1, BuildType debug

https://app.company1.com/devApi/

Flavor company1, BuildType release

https://app.company1.com/prodApi/

Flavor company2, BuildType debug

https://dev.company2.com/api/

Flavor company2, BuildType release

https://prod.company2.com/api/

Solution

  • For my specific problem, where the URLs differ a lot one to another, I couldn't make it work with Flavours and BuildTypes.

    I was able to define the debug/production URLs by using specific strings.xml for each build variant (which is each combination of flavour and build type):

    These are the folder structures to do so:

    src/flavour1Debug/res/values/strings.xml 
    src/flavour1Release/res/values/strings.xml 
    

    and

    src/flavour2Debug/res/values/strings.xml 
    src/flavour2Release/res/values/strings.xml 
    

    EXTRA: This can also be used to host different google-services.json files