How To Build Multiple Variants of APKs in Android Studio

 

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading...

 

Tung Dao Xuan, tungdx1210@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog

 

Introduction

Have you ever built multiple variants of apk files from a single code base to meet some requirements in programming process, such as: free and paid versions or local backend and remote backend or something like that?

I’ll show you how to do that in Android Studio. In this post, I am going to configure the build process to build one paid version and another free version from a single code base. Both versions have the main screen, but in the free version when users open the program, they will see ads screen before main screen.

Source code for this article can be found at https://github.com/tungdx/MultipleApkEditions.git

 

Code Structure Of Project

How To Build Multiple Variants of APKs in Android Studio

Strings.xml

Activity_main.xml

AndroidManifest.xml

MainActivity.java

 

How to configure Android Studio

 

Step 1: Define product flavors

To define two product flavor, edit the build file for the module to add the following configuration.

 

After modifying your build file, sync your Android Studio with the new changes. (Click the below icon)

How To Build Multiple Variants of APKs in Android Studio

 

Open the “Build Variants” view, you will see the result looks like below figure.

How To Build Multiple Variants of APKs in Android Studio

 

Step 2: Create additional directories for each flavor

Paid Program

  1. On project panel, right-click the src directory under app and select New > Directory.
  2. Enter “paid” as the name of new directory and click OK
  3. Create “res” and “values” directories: paid/res/values
  4. Create xml file, enter the following values:
The strings.xml file in “paid” folder will be merged with strings.xml file in code base (main folder)

 

Free Program

Similar to the paid program, create the following files and directories:

free/res/values/strings.xml

free/res/layout/activity_ads.xml

free/AndroidManifest.xml

free/java/vn/tungdx/multipleapkeditions/AdsActivity.java

 

strings.xml

 

activity_ads.xml

 

AndroidManifest.xml

 

This file will be merged with AndroidManifest.xml file in code base (main folder), so in free program version, the ads screen (AdsActivity.java) is run before the main screen (MainActivity.java).

AdsActivity.java

 

The resulting directory structure looks like below figure

How To Build Multiple Variants of APKs in Android Studio

 

Step 3: Build project

To work on files from a particular flavor, click on Build Variants on the bottom left of the IDE window and select the variant you want to modify in the Build Variants panel. Android Studio may show errors in source files from flavors other than the one selected in the Build Variants panel, but this does not affect the outcome of the build.

After build project for each flavor, you will get the result as shown below.

How To Build Multiple Variants of APKs in Android Studio

 

Conclusion

That’s it. Hope you can conveniently make multiple variants of apk files that meet your requirements in a short period of time. You can find more info at:

https://developer.android.com/tools/building/configuring-gradle.html

Thanks for reading!

 

RobustTechHouse is a leading tech company for mobile app development and ECommerce web design and development. If you are interested to engage RobustTechHouse for your projects, you can contact us here.

 

 

Recommended Posts
Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
HybridNative1AndroidVsiOS4