With Gradle build system, you now can have different product flavors, each having its own package name. Following is an example gradle script having free and pro flavors of the same app.
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1"
defaultConfig {
applicationId "com.example.my.app"
}
productFlavors {
free {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 12
versionName '12'
}
pro {
applicationId "com.example.my.app.pro"
minSdkVersion 15
targetSdkVersion 23
versionCode 4
versionName '4'
}
}
R
class will still be generated in the package name specified in the AndroidManifest.xml
so you don't need to change a single line of code when switching flavors.
You can switch the flavor from Build Variants
pane which is accessible from left bottom corner of Android Studio. Also, when you want to generate a signed APK, android studio will ask you the flavor you want to build the APK.
Also, you can have different resources for each flavor. For an example, you can create a directory pro
in your src
directory. The directory structure should be similar to the main
directory. (Eg: if you want to have a different launcher icon for pro version, you can place it in src\pro\res\drawable
. This will replace the free icon located in src\main\res\drawable
, when you have switched to pro
flavor).
If you create a strings.xml
in pro
resource directory described above, the main strings.xml
and pro strings.xml
will get merged to get a final strings.xml
when building in pro
flavor. If a certain string key exists in both free and pro xml, the string value will be taken from pro xml.
If you need to check whether current version is pro or free in code, you can use a method like following.
public boolean isPro() {
return context.getPackageName().equals("com.example.my.app.pro");
}
For more information, Refer this