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:
productFlavors {
production {
}
development {
}
}
view raw build.gradle hosted with ❤ by GitHub
Step 2: In your MainActivity, let's check if the app is production or development, you will also notice that there is a constant:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvExample= (TextView) findViewById(R.id.tvExample);
if(BuildConfig.FLAVOR.equals(ConstantRTK.DEVELOPMENT)){
tvExample.setText(ConstantRTK.DEVELOPMENT);
}else if(BuildConfig.FLAVOR.equals(ConstantRTK.PRODUCTION)){
tvExample.setText(ConstantRTK.PRODUCTION);
}else{
tvExample.setText("Don't know too");
}
}

Constant :
package com.number.ratanakpek.productflavorrtk.constant;
/**
* Created by RatanakPek on 10/1/2017.
*/
public class ConstantRTK {
public static final String DEVELOPMENT="development";
public static final String PRODUCTION="production";
}
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):
productFlavors {
production {
resValue "string", "app_name", "Production APP"
}
development {
resValue "string", "app_name", "Development APP"
}
}
view raw app.gradle hosted with ❤ by GitHub

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:
productFlavors {
production {
resValue "string", "app_name", "Production APP"
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher"
]
}
development {
resValue "string", "app_name", "Development APP"
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher_test"
]
}
}
view raw app_icon.gradle hosted with ❤ by GitHub

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