Saturday, September 30, 2017

How to configure Build Variants with Android Studio

Definition
What is the purpose of using Build Variant? We can use it to create different versions of your app from a single project, and how to properly manage your dependencies and signing configurations.





Note:
buildType configure how we package our app
shrinkResources
progaurdFile
etc.

Flavor configure different classes and resources.
in Flavor1 your MainActivity can do something, and in Flavor2 different implementation
difference App name
etc.

Each product flavor can have its own values of the following properties, among others, which are based on the same properties from default config:

applicationId
minSdkVersion
targetSdkVersion
versionCode
versionName


Example


For example, you can do like:
- One app can build into free version and paid version (without creating 2 projects)
- We can limit contents and configuration instead of building multiple APK

Implementation


1. How to recognize which product flavor we are using

Step 1: Add below code into gradle in "android" section:
Step 2: In your MainActivity, let's check if the app is production or development, you will also notice that there is a constant:

Constant :
Step 3: If you chose productionDebug mode from Build Variant it will print like below:



Output:


2. Make app have different's name:

You can add more code into Gradle like below and please remove app_name in file string.xml (if not it will duplicate):

Output :


3. Make different app icon:

It's pretty easy, I have 2 icons :

ic_launcher.png & ic_launcher_test.png
Please add some code like below:

Don't forget edit appIcon in the manifest to "${appIcon}" :
If you chose "developmentDebug" from Build Variant on the left of Android Studio, you will see your icon like below:

Here is the source in github.

1 comment:

  1. Thank you for taking the time to publish this information very useful!
    mac remote team app

    ReplyDelete