How to Create Build Variants in Android Studio

Posted By : Chandan Wadhwa | 30-Dec-2014

One of the important features of Android Studio is to generate build variants. Build variants generate different flavors of one application.

The goal of this blog is to use the build variants feature of Android Studio to create a project which can be built in two flavors designed to target phone and tablet devices respectively. The build environment will be configured such that each flavor can be built using either a release or debug build type. 
The end result, therefore, will be four build variant options available for selection within Android Studio:

 

phoneDebug
phoneRelease
tabletDebug
tabletRelease

 

This raises the question as to the difference between a build type and a build flavor. In general, a build type defines how a module is built (for example whether or not ProGuard is run, how the resulting application package is signed and whether debug symbols are to be included).
The build flavor, on the other hand, typically defines what is built (such as which resource and source code files are to be included in the build) for each variant of the module. Initially the two flavors will be configured such that they differ only visually in terms of the resources that are used for each target such as layouts and string values. The project will then be further extended to provide an example of how each flavor might make use of different source code bases in order to provide differing application behavior.

 

Adding the Build Flavors to the Module Build File

With the initial project created, the next step is to configure the module level build.gradle file to add the two build flavor configurations. Within the Android Studio Project tool window, navigate to the build.gradle file located in BuildExample -> app -> build.gradle and double click on it to load it into the editor:

 

Once loaded into the editor, the build file should resemble the following listing:

 

 

 

As previously outlined, the project is going to consist of two build types (release and debug) together with two flavors (phone and tablet). As is evident from the build file, the release build type is already declared in the file so this does not need to be added. In practice, Gradle is also using sensible default settings for the debug build so this type also does not need to be added to the file. All this leaves is the requirement to declare the two build flavors as follows:

 

 

Finally Choose Build Variants from Build Variants Window and Run it.

 

THANKS

About Author

Author Image
Chandan Wadhwa

Chandan is an Android Apps developer with good experience in building native Android applications.

Request for Proposal

Name is required

Comment is required

Sending message..