How can I use adb to grant permission without root?

Viewed 56866

Background

I'm trying to create an app that can grant and revoke permissions of other apps.
To do this I'm using adb commands:

pm grant packageName permissionName

This works great if I call it through a shell with root permissions.

Problem

The problem is that when I call this command without root permissions, I receive

Operation not allowed: java.lang.SecurityException: grantRuntimePermission: Neither user 2000 nor current process has android.permission.GRANT_RUNTIME_PERMISSIONS

This is to be expected (it would be a big problem otherwise).

Question

Is it possible to allow my app to manage other apps permissions through Android settings?
I'm looking for something like granting USAGE_STATS permission to an app going to Settings -> Security -> "App with usage access", but for GRANT_RUNTIME_PERMISSIONS (and REVOKE_RUNTIME_PERMISSIONS).

8 Answers

I was trying to install BBS to my Redmi Note 4, MIUI 10.1 (not sure if details are relevant). Got same error. Then I found a menu item three rows below main 'USB debugging' menu in developer options. It was called

USB debugging (Security settings)

Allow granting permissions and simulating input via USB debugging

Attempt to enable this option prompted three warning windows with information about terrible things that can happen if you proceed. After last confirmation (not sure if the phone was connected or I connected it after) I ran

adb -d shell pm grant packageName android.permission.BATTERY_STATS

but now it succeeded.

In Developer Options, scroll (all the way) down until u find Disable Permission Monitoring

and turn it on.

Now the pm command should work

I noticed that they change it to: USB debugging (Security setting'

and location moved up

and it require a sim

Xiaomi users may need to enable an setting in the Developer Options. It's called "USB debugging (Security settings)" with the subheading "Allow granting permissions and simulating input via USB debugging"

For those who are using MIUI:

I'm using MIUI 12.0.5 Stable. And in this version - to run any granting permission commands, we must first enable "USB Debugging" and "USB Debugging (Security settings)" from the developer options, and only after then we will be able to execute permission commands via adb like this one:

adb shell pm grant com.ivianuu.immersivemodemanager android.permission.WRITE_SECURE_SETTINGS

It worked for me with the solution Elia Weiss provided. (unfortunately I haven't enough reputation to upvote and comment)

Switch on "Developer Options" / "Disable Permission Monitoring" (at the end of the section)

This is probably different depending on Android versions and or OS variants.

In my case it's ColorOS 6.1 / Android 9 on a realme X2 global variant. (exactly: OS version RMX1993EX_11_A15)

You can not do that. It is straight forward security breach which of course Google will not allow you.

You can not control permissions of other apps via your app or simply by using adb commands.

I fixed this by changing the usb configuration to "MIDI" while aving all usb debuging options turned on.

The answer from @elia-weiss here almost worked for me. I also had to make sure the app I was granting to was stopped. I just used force stop in app info.

Related