I'm creating a very simple app with a free and paid version, for this I'm using the great productFlavors feature in the Android Gradle plugin. I understand how the build.gradle file should be configured and have written it so my build options are;
- freeRelease
- freeDebug
- paidRelease
- PaidDebug
that looks as follows
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.someconfig
}
}
productFlavors {
free {
packageName "com.somepackagename.appfree"
}
paid {
packageName "com.somepackagename.appPaid"
}
}
Basically, all I'm trying to do is restrict a few features and add AdMod to the free version, how would I do that? Do I add if-else statements in my main java classes that check to see if it's free or paid? or can I overwrite java classes in the free directory? How should I go about doing this?
You can encode things in the directory structure, e.g.:
The code and assets that are specific to one version or the other go into the specific directories. The manifests are merged. You can use the
FLAVOR
field generated intoBuildConfig.java
to base if-else statements on.The SourceSets and Dependencies section of the Android-Gradle-Plugin user guide explains this in greater detail.
Yes, you have to add
if-else
in youractivities
/fragments
to check the flavour:Source: BuildConfig