ProductFlavors based permission in Android

ProductFlavors based permission in Android

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.

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 eachfolder and you can follow the same way based on your productFlavors.

app/src/paid/AndroidManifest.xml

app/src/free/AndroidManifest.xml

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.

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