Android gradle plugin - flavor dimensions by country

1.9k views Asked by At

I have inherited a project which has the following flavor set up in gradle build file:

productFlavors {
    def STRING = "String"
    def BOOLEAN = "boolean"
    def TRUE = "true"
    def FALSE = "false"
    def BASE_ENDPOINT = "BASE_ENDPOINT"

    mock {
        applicationId "com.example.myapp.mock"
        buildConfigField STRING, BASE_ENDPOINT, '"mock://localhost/gt"'
    }

    qa {
        applicationId "ca.example.myapp"
        buildConfigField STRING, BASE_ENDPOINT, '"https://qa-somesite.com/gt"'
    }

    qaInternal {
        applicationId ".ca.example.myapp.int"
        buildConfigField STRING, BASE_ENDPOINT, '"https://internal-somesite.com/gt"'
    }

    beta {
        applicationId "com.example.myapp.beta"
        buildConfigField STRING, BASE_ENDPOINT, '"https://somesite.com/gt"'
    }

    prod {
        applicationId "ca.ca.example.myapp"
        buildConfigField STRING, BASE_ENDPOINT, '"https://somesite.com/gt"'
    }
}

nothing special going on there just flavors for production qa, mocking , etc.

This is just for for a particular country though. Its for USA. So these flavors are all for USA. I need to use the same app. I'd like to create flavors for another country called France. The france flavors have different configurations.

i was thinking i could do something like this:

flavorDimensions "country","buildtype"

to get me the flavors by country. But then how would i create my own mock,qa,qaInternal,etc flavors for the new country.

So to be clear my end goal is to have product flavors for a new country called france given the code i've pasted above all in android studio.

UPDATE: Let me be more precise on what i desire and the issue. Look at the current product flavors: mock, qa, qaInternal,beta and prod. They all pretain to information about a USA build. This already exists in code. The code is currently built for USA customers. Now i have been asked to make the code also available for French customers so i need a french build.

The issue is things like the applicationID, and many buildConfigField are going to be different in the french build. How can i engineer a solution where i can have for example, a french mock, a french qa, a french qaInternal ,french beta, and a french prod just like i currently have for the USA build ?

The issue has nothing to do with locale per say, its just that we have two products a USA product and we want a french product which will have a different configuration then USA.

take for example the mock flavor. For french i need this to happen:

mock {
        applicationId "french-com.example.mock"
        versionName = versionOverride + 'french-mock'
        buildConfigField STRING, BASE_ENDPOINT, '"mock://localhost/french-gt"'
    }
2

There are 2 answers

0
Mohammad Rahchamani On

I think you should create multiple buildTypes instead of flavors. if you have build types France and USA, and flavors mock, qa and ... , then you would have these build variants : qaUSA , qaFrance, mockUSA, mockFrance and etc.

here is build.gradle example :

productFlavors {
        mock {
             ...
        }
        qa {
            ...
        }
    }

buildTypes {
        USA {
            ...
        }

        France {
            ...
        }
    } 
3
Varun On

What you are doing would work if you assign a flavorDimension to every productFlavor. So in your case the build.gradle could look like

flavorDimensions "country","buildtype"

productFlavors {
    def STRING = "String"
    def BOOLEAN = "boolean"
    def TRUE = "true"
    def FALSE = "false"
    def BASE_ENDPOINT = "BASE_ENDPOINT"

    mock {
        dimension 'buildtype'
        applicationId "com.example.myapp.mock"
        versionName = versionOverride + '-mock'
        buildConfigField STRING, BASE_ENDPOINT, '"mock://localhost/gt"'
    }

    qa {
        dimension 'buildtype'
        applicationId "ca.example.myapp"
        versionName = versionOverride + '-qa'
        buildConfigField STRING, BASE_ENDPOINT, '"https://qa-somesite.com/gt"'
    }

    qaInternal {
        dimension 'buildtype'
        applicationId ".ca.example.myapp.int"
        versionName = versionOverride + '-qaInt'
        buildConfigField STRING, BASE_ENDPOINT, '"https://internal-somesite.com/gt"'
    }

    beta {
        dimension 'buildtype'
        applicationId "com.example.myapp.beta"
        versionName = versionOverride + '-beta'
        buildConfigField STRING, BASE_ENDPOINT, '"https://somesite.com/gt"'
    }

    prod {
        dimension 'buildtype'
        applicationId "ca.ca.example.myapp"
        versionName = versionOverride + '-prod'
        buildConfigField STRING, BASE_ENDPOINT, '"https://somesite.com/gt"'
    }

    usa {
        dimension 'country'
        // whatever else you want
    }

    france {
        dimension 'country'
        // whatever else you want
    }

}

With the flavorDimensions set up, you will now have a combination of productFlavors with dimension&buildtype, In this case it will be mockUsa, mockFrance, qaUsa, qaFrance etc..

Given the fact that there are actual buildtypes debug and release, you might end up with a huge list of variants!