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

ConstraintLayout, or not so fast, after all…

Recently, while browsing through news from Android world I came across a 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 new Layout Builder behaves and what is ConstraintLayout like in use.
Read more

Static shortcuts in Android 7.1 Nougat

With the new Android version, Nougat 7.1 (API 25), Google gives us some interesting possibilities. One of them is the option to create shortcuts to an application. In this post I will show you how static shortcuts work.
Read more

Project estimation

Check out how we use our knowledge in practice, and make your project with us.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in M-commerce category

17

clients reviews

Clutch logo
Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

60+

projects in portfolio