ProductFlavors provide a way of isolating a codebase based on the flavors like free, paid, etc. When flavoring a product sometime we need to provide different permission for different flavors. As an example you might need to add Phone call permission only for paid version and you don’t need Phone call permission to free version. Because the phone call feature is not available in the free version of the application.

You can isolate these permissions based on the productFlavor from the help of the Gradle build system.

Add productFlavors

First, you need to add productFlavors to our app level Gradle. In here I am gone add only the two productFlavors as free and paid.


  productFlavors{
free{
applicationIdSuffix ".free"
}
paid{
applicationIdSuffix ".paid"
}
}

Add Manifest

In Android we have to define permission inside AndroidManifest.xml file. For each productFlavors we have to define separate AndroidManifest.xml file. For that we need to add AndroidManifest to the folder structure. I will create following files inside the each folder and you can follow the same way based on your productFlavors.

Also we have a default AndroidManifest.xml inside the app/src/main folder.

Next, open the AndroidManifest inside the app/src/paid folder and I will add only the Call phone permission to that file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ishan.appfla">
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

Next open the AndroidManifest inside app/src/free folder and add only wifi change state permission to that file.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ishan.appfla">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

In both of these files, we don’t need to add activity, service level informations. Those details will keep in the AndroidManifest inside app/src/main folder.

So that’s all. When you are building relevant productFlavor Gradle will automatically merge the AndroidManifest inside main with the flavour level AndroidManifest. You don’t have to do anything manually

x