Creating BuildVariants with Gradle

Creating BuildVariants with Gradle

When creating an application, sometimes at some point we want to get several versions of our application that differ in some more or less important details. To easily obtain different versions of your application, just make a few changes in Gradle.

Creating multiple versions of app – what can be done?

When creating an application, sometimes at some point we want to get several versions of our application that differ in some more or less important details. To easily obtain different versions of your application, just make a few changes in Gradle.

BuildTypes

When creating a project in AndroidStudio in Gradle we have 2 default BuildTypes: debug and release. These types refer to a number of settings for building a project. It is also a good place to define things, e.g. location and name of generated builds:

buildTypes {
        release {
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def file = output.outputFile
                    output.outputFile = new File(file.parent, "releaseBuildHeader" + defaultConfig.versionName + ".apk")
                }
            }
        }

        debug { (...) }
}

ProductFlavors

ProductFlavors, in turn, allows us to define the parameters used in the application code, for example, we can distinguish between minSdkVersion. The example below shows how to differentiate between versions using various url addresses to the API, of which one requires a certificate and the other does not:

productFlavors {
        live {
            applicationId 'pl.holdapp.testapp'
            buildConfigField 'String', 'API_URL', '"https://some-api-url.com"'
            buildConfigField 'boolean', 'NEED_CERTIFICATE', "false"
        }
        dev {
            applicationId 'pl.holdapp.testapp.dev'
            buildConfigField 'String', 'API_URL', '"https://another-api-url.com"'
            buildConfigField 'boolean', 'NEED_CERTIFICATE', "true"
        }
    }

As you can see, here we use buildConfigField, where we specify ‘defined constant type’, ‘name’ and ‘value’ respectively. Okay, but there’s still a question of how to use this value within our application code? As it turns out, it is just as easy. It’s enough to reference the constant in the code as follows:

if (BuildConfig.NEED_CERTIFICATE)
            okHttpBuilder.sslSocketFactory((sslContext.getSocketFactory()));

With BuildTypes and ProductFlavors as we set them, after synchronizing the project (introducing the changes made in Gradle), we now have four BuildVariants:

Build variant panel - devDebug

As a result, we can freely switch between variants to create the right version of an application with basically no workload. This is a very practical and helpful solution that can save you a lot of valuable developer time.

Learn more

ConstraintLayout, or not so fast, after all…

Recently, while browsing through news from Android world I came across the concept of ConstraintLayout. It is a new layout delivered by Android and Google, supporting Android versions from API 9 on. Digging into possibilities it is to give, I decided to check how the new Layout Builder behaves and what is ConstraintLayout like in use.

Read more

Android Small Talks: CoordinatorLayout, or one of the strengths of Android Design Support Library

When Android Lollipop entered the market, there was a breakthrough. Google provided us with an extensive library of Android Design Support Library which facilitates creating applications that are compliant with the principles of Material Design. Creating a user interface in accordance with these guidelines introduces our software to the next level of design and user-application interaction.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2021

Winning app in
EVERYDAY LIFE

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2023

Winning app in MCOMMERCE DEVELOPMENT

23

client reviews

Clutch logo