Changing server URL based on flavor and build type on Android Gradle

When developing an Android app I had the need of setting different server URL for each flavor, because it has a server instance for each company that uses our product. Also we use a development server and a production server, so I came to this problem, because there is no way to set a buildConfigField for each variant (company1Debug, company1Release, company2Debug, …), but only for builtType OR productFlavor.

To achieve this goal, I found on web a similar config in David Medenjak’s blog, that uses flavors with ext values to dynamically config the value of BuildConfig.URL. But I prefer to keep using buildTypes as I feel it’s semantically more accurate than having development and production flavors too.

android {
    buildTypes {
        debug {
        }
        release {
        }
    }

    productFlavors {
        company1 {
            versionNameSuffix "-company1"
            ext {
                server = [debug: "https://dev.company1.com.br", release: "https://company1.com.br"]
            }
        }
        company2 {
            versionNameSuffix "-company2"
            ext {
                server = [debug: "https://dev.company2.com.br", release: "https://company2.com.br"]
            }
        }
    }
    ...
}

I don’t have much experience with Groovy (Gradle language), but I used David’s idea to have ext values in each flavor. I created a “server” variable that is a dictionary, where the key is the build type and the value is the server URL that this variant (flavor+buildType) must use.

Then, we need a task to use the ext.server dictionary and set the buildConfigValue accordingly:
Continue reading