Android Gradle. How to combine Flavors with buildTypes
Asked Answered
F

4

9

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/
Forney answered 15/1, 2019 at 10:27 Comment(5)
what exactly are your requirements which you want to give in flavor ?Unconscionable
check my answer @Forney , you can modify according to your requirementSwahili
Hi, @kike, did you get the best solution now? I got the same issue today. Unfortunately, I cannot get a satisfying solution only with Gradle. I finally have to define two variables in each flavour, one for debug and the other for release. Then in the code, I check the buildType to get the right value. Any update?Gyroscope
@Gyroscope If these variable are very different (ex. you cannot combine strings) you will have to set them in a separate strings.xml. Check my answer below and let me know if you have more questions.Forney
#60974759Motto
F
5

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

Forney answered 4/3, 2019 at 9:32 Comment(0)
A
4

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

Adaptable answered 15/1, 2019 at 12:19 Comment(2)
Thanks for the answer. It would work for some cases but in my case I need the BASE_URL all together. That URL does not depend on us and may be company1.com/apiDebug or debug.company2.com/apiForney
My idea is to divide the whole URL into parts and put them into Flavours and BuildTypes. This works even if there are combination with more that 2 types. i.e something like Company1-Mobile-Debug, Company2-Tablet-Prod etc.. If you say everything is uncertain here, then you better go with String constants in you Java and use Build Type and Product Flavours along with some Switch statement to build the actual URL at run timeAdaptable
U
0

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

Unconscionable answered 15/1, 2019 at 10:37 Comment(0)
A
0

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")


    }
Androecium answered 15/1, 2019 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.