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

BDD using JGiven – How It Can Make Your Work Easier?

BDD or Behavior-Driven-Development is a method of programming in which business clients, developers and testers describe the behavior of the application with the help of scenarios. The scenarios are written using agreed notation and language, so that they are understood by all parties.

Read more

5 Reasons Why You Should Build the UI in ConstraintLayout

ConstraintLayout is a popular solution that Android developers use every day. And not without a good reason. If you’re at the beginning of your journey as a software engineer, get familiar with the most important features this layout can offer. It will help you understand the way it works, and Android app development will become easier.

Read more

How to Implement Feature Flags and Gain More Control over Your App?

Every person with software development experience can share a few stories about incorrectly working features. We build our apps out of small pieces, introduce advanced architecture patterns, and yet sometimes one of the elements fails, causing bugs or even system failures. In such cases, feature toggling can save the day! Find out how to implement feature flags, and make your app more stable.

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 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORT & RECREATION category

17

client reviews

Clutch logo