kike
kike

Reputation: 4463

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/

Upvotes: 9

Views: 7278

Answers (4)

kike
kike

Reputation: 4463

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

Upvotes: 5

Housefly
Housefly

Reputation: 4422

Try something like this:

buildTypes {
    debug {
        buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
    }
    release {
        buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
    }
}

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

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

And use it like:

String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH

Upvotes: 4

Pavel  Petkevich
Pavel Petkevich

Reputation: 45

Your main problem is that you do not correctly place your buildTypes for flavors and no correct params inside each company and also I suggest you read more about Gradle setups(developer.android).

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

    flavorDimensions "version", "brand"
    productFlavors {
        dev {
            versionName += "dev"
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }

        prod {
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }
        company1{
            dimension "brand"
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "1")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "true")
        }

        company2 {
            dimension "brand" 
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "2")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "false")


    }

Upvotes: 0

Umair
Umair

Reputation: 6426

You can use flavors to add basic configurations for your application, that ranges from app url, API keys, master password etc.

flavorDimensions "Mobile"
     productFlavors {
        Production {
            dimension "Mobile"   // dimension can be mobile, kiosks, tv, miniKiosks etc

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""

        }
        Demo {
            dimension "Mobile"

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", "services/v1/"
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""
        }

    Local {
            dimension "Mobile"

            resValue "string", "API_KEY", ""
//            resValue "string", "app_name", ""
            resValue "string", "SERVICE_IP", ""
//            resValue "string", "SERVICE_IP", ""
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", ""
            resValue "string", "MASTER_PASSWORD", "a"
        }
    }

Now if you check at your build varients you will get something like this:

enter image description here

Upvotes: 0

Related Questions