Implement Build Variant Specific Permissions

Faruk Toptaş
Android Bits
Published in
1 min readDec 14, 2018
Photo by
Fabrizio Conti on Unsplash

In one of my projects, I needed to separate permissions for each build variants. Using different manifest files is not feasible. Because other components like Activity, Service etc. should be added to each manifest file every time.

Here is the real solution :

src/main/AndroidManifest is your main manifest file for all components.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>

src/[BUILD_VARIANT]/AndroidManifest.xml contains only permission specific to the variant.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

WRITE_EXTERNAL_STORAGE permission will be in your manifest only for the build variant you want to add.

Happy codings :)

--

--