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
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
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:
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):
3. Make different app icon:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
productFlavors { | |
production { | |
} | |
development { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
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):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
productFlavors { | |
production { | |
resValue "string", "app_name", "Production APP" | |
} | |
development { | |
resValue "string", "app_name", "Development APP" | |
} | |
} |
Output :
3. Make different app icon:
It's pretty easy, I have 2 icons :
ic_launcher.png & ic_launcher_test.png
ic_launcher.png & ic_launcher_test.png
Please add some code like below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
] | |
} | |
} |
Thank you for taking the time to publish this information very useful!
ReplyDeletemac remote team app