Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Different permissions for different flavors

I have An app with tow flavors ... only one of them needs some permissions. How can I request these permissions only for this flavor during building apk?

These are the permissions needed:

 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
like image 411
Mohammad Mahmoud Avatar asked Nov 06 '17 20:11

Mohammad Mahmoud


People also ask

What are the different permissions in Android?

Types of permissions. Android categorizes permissions into different types, including install-time permissions, runtime permissions, and special permissions.

How does Android handle multiple permissions?

In case one or more permissions are not granted, ActivityCompat. requestPermissions() will request permissions and the control goes to onRequestPermissionsResult() callback method. You should check the value of shouldShowRequestPermissionRationale() flag in onRequestPermissionsResult() callback method.

How do I set custom permissions on Android?

You can use the sharedUserId attribute in the AndroidManifest. xml 's manifest tag of each package to have them assigned the same user ID. By doing this, for purposes of security the two packages are then treated as being the same app, with the same user ID and file permissions.

How do I grant all permissions to Android apps?

You can grant all run-time permissions on installation by using; adb install -g. Or just use ';' between each pm grants like this?


Video Answer


2 Answers

For the purposes of this answer, I assume that you have two product flavors, chocolate and vanilla. The chocolate one is the one that needs the additional permissions.

Step #1: In src/main/AndroidManifest.xml, put the <uses-permission> elements that are in common between all the flavors

Step #2: Create src/chocolate/, a source set that will hold source that will only be used for chocolate builds, not vanilla builds

Step #3: In src/chocolate/AndroidManifest.xml, inside an otherwise-empty <manifest> element, put the <uses-permission> elements that chocolate needs that vanilla does not

Now, your chocolate builds will have all of the permissions, while vanilla builds will only have those from the main manifest.

like image 95
CommonsWare Avatar answered Oct 21 '22 21:10

CommonsWare


For each flavor you have to define a manifest wich contains unsimilarities

like image 30
Nawrez Avatar answered Oct 21 '22 23:10

Nawrez