3 Mins Read  March 29, 2017  Cuelogic Insights

How to create and configure Android Build Variants

This blog elaborates, with an example, the process of creating and configuring the build variants for QA and PRODUCTION versions in an Android project. An Android APK file is the product of weekly sprints. We may need to create different versions of an APK file based on an application type (free or paid application), the server environment type (QA, PROD), etc.

Android Build Variants

To reduce the complexities of making different versions of an APK file, the powerful Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process. This allows you to define flexible custom build configurations. Each build configuration can define its own set of code and resources while reusing parts common to all application versions.

The Android Plugin for Gradle works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android applications.Steps:
1. Create an Android Project with default setting provided by the Android Studio IDE. Let the project be called ‘Build Variants Demo’.
2. Open module-level build.gradle file, and add productFlavors {} block. Your build.gradle will display, as depicted below: apply plugin: ‘com.android.application’ android { compileSdkVersion 23 buildToolsVersion “23.0.2” defaultConfig { applicationId “com.cuelogic.buildvariantsdemo” minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName “1.0” } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’ } } signingConfigs { release { storeFile file(“my-release-key.jks”) storePassword “password” keyAlias “my-alias” keyPassword “password” } } productFlavors { qa { applicationId “com.cuelogic.buildvariantsdemo.qa” versionCode 1 versionName “1.0” } prod { applicationId “com.cuelogic.buildvariantsdemo.prod” versionCode 1 versionName “1.0” signingConfig signingConfigs.release } } } dependencies { compile fileTree(dir: ‘libs’, include: [‘*.jar’]) testCompile ‘junit:junit:4.12’ compile ‘com.android.support:appcompat-v7:23.4.0’ }

Here, the productFlavours block contains qa & prod flavors with some specific settings like applicationId, versionCode and versionName.

When you sync the project, Gradle automatically creates build variants based on your build types and product flavours, and names them according to <product-flavor><Build-Type>. The display will show as follows:

Build_Variant_Options

You can select any option from the above list and run selected build variant type.

3. Now let’s start creating the source set for build variants. By default, Android Studio creates the main/ source set and directories which will be shared between all your build variants. However, we can create new source sets for build variants. Here, Gradle expects us to maintain the source set files and directories in a certain way, like the Java class files that are specific to qa or prod build type to be in the src/qa/java/ and src/prod/java/ directory respectively. Please check the screenshots.Note:Depending upon your build variant selection, respective classes will be excluded from the build package. For example, you will view BaseRequest.java under prod highlighted with a green icon while BaseRequest.java under qa is depicted inactive with a red icon.

ProductFlavours

4. As per your requirement, you can add assets, java files and resources in respective product flavour folders. Here I have created a BaseRequest.java file which will hold build specific URL, like the QA URL & PRODUCTION URL:

In src/qa/java/com.cuelogic.buildvariantsdemo folder public class BaseRequest { public static final String BASE_URL = “https://www.cuelogic.com/qa”; }

In src/prod/java/com.cuelogic.buildvariantsdemo folder public class BaseRequest { public static final String BASE_URL = “https://www.cuelogic.com/production”; }

We are done! You may now try with different build configurations such as free and paid application, application white labeling, enterprise and market place app variants, etc. This brings us to the article’s conclusion. You may share your suggestions and pose questions at virag.brahme@34.200.212.66.

[wcp-carousel id=”10008″]

Recommended Content

Go Back to Main Page