Skip to content
Related Articles

Related Articles

Improve Article

How to Create Different Build Variants in Android?

  • Last Updated : 23 Jul, 2021

During the development and release phases of an Android application, we typically require a variety of APKs or different versions of APKs. For example, you may require one debug APK without proguard and one debug APK with proguard, or you may require one APK for free users and one APK for paying users, or you may require one APK for Android version 6 and above and one APK for Android version below 6, among many other options. But the question is, how are you going to generate all of these different versions of your app? Weekly sprints result in an Android APK file. We may need to build multiple versions of an APK file dependent on the kind of application (free or paid), and so on.

Build Variants in Android

When developing an Android application, we generate several build types such as “debug” and “release.” At the same time, we may develop different product flavors for the same app, such as a free product flavor for free users and a premium product flavor for paying users. As a result, Android Studio includes a feature called Build Variants, which can be thought of as a cartesian product of all of your build types and product flavors.

The build flavor, on the other hand, generally describes what is created for each version of the module (such as which resource and source code files are to be included in the build). Initially, the two versions will be designed in such a way that they only differ visually in terms of the resources needed for each target, such as layouts and string values. The project will then be expanded to demonstrate how each flavor could leverage various source code bases to deliver varied application behavior.

All you have to do is include several build types in your module-level build.gradle file, and then during development or production, you can simply select the Build Variant you wish to test or release.

How to Add Build Types?

By default, when you start a new project, Android Studio will create two build types for it: “debug” and “release.” To add new build types, however, you must add them to your module-level build.gradle file and inside the buildTypes block. Below is the sample Geeks for Geeks App

android {
    defaultConfig {
        applicationId "com.gfg.variants"
        versionCode 1
        versionName "GeeksforGeeks_Rel"
        ...
    }
    buildTypes {
        debug {
            versionNameSuffix ".rel"
            minifyEnabled false
            shrinkResources false
        }
        production {

            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 
        }
        finalRelease{
            versionNameSuffix ".debug"
            debuggable true
            minifyEnabled false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ...
        }
     }
  
}

Note: There are three build types in the above code: debug, production, and finalRelease. The debug and production build types are the same as those created by Android Studio, but with a few more characteristics, while finalRelease is a new build type that is a mix of debug and proguard which is formulated for the final release of the app.

Conclusion

The above method is what adds different product variants in your android project, you may check out any demo projects on Github, or even your android app’s build.gradle file to see all the variants it uses for its development, it’s usually a better idea to divide them to get better functionality in your app, like shrink resources is always not necessary as its time and resource taking, so why not avoid it altogether in the release?

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :