34

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).

2
  • I'm leaving this answer here, hoping it will be useful to anyone having the same need. I tried signing the app as a system app, asking permissions and adding sharedUserId to manifest tag, and it seems to work for now. I'm gonna test it and check if it really works in every case. I will update this answer when tests finish Aug 31, 2018 at 9:39
  • See also android.stackexchange.com/questions/241608/…
    – U. Windl
    Oct 22, 2022 at 19:24

10 Answers 10

60

In Developer Options, scroll down until u find Disable Permission Monitoring (I noticed that they change it to USB debugging (Security setting)and it require a sim)

and turn it on.

Now the pm command should work

6
  • 4
    Perfect solution. I encountered this error in Android 10. This setting works as expected. Thanks May 25, 2020 at 14:47
  • Can't find any such thing in Debugger Options (on an Android 10 Samsungs Active Tab 3) :-/
    – Frank N
    Jun 24, 2021 at 16:15
  • 3
    It is not working on Android 12 (OnePlus 9), still giving the same error.
    – dbdq
    Apr 15, 2022 at 14:47
  • 3
    not working in latest phones
    – Ahmad
    Jul 28, 2022 at 15:23
  • 8
    @dbdq I had the same issue with my OnePlus 9 but managed to get it working by rebooting the device, then killing adb (adb kill-server), then going back into adb shell - the pm grant commands worked without any issues after this.
    – d3Xt3r
    Sep 3, 2022 at 8:06
50

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.

4
  • Thanks for this info. Sadly, it's hard to understand when and if I can use it (I never had it in any of the phones I had), so it's not really reliable. The only way I found is to install another app as system app, signing it with the same key of the ROM (this will work only for pure AOSP, LineageOs or custom ROMs derived from them). This is the approach I currently took for my app, Permission Ruler. I'm still trying to understand if this makes sense and works reliably though. Jan 2, 2019 at 9:50
  • 1
    Still works with MIUI12. Note that you will have to sign into your MI account before being able to do this. Sep 12, 2020 at 18:35
  • And you have to have working SIM card inserted. If your SIM slot is busted for example, you are out of luck May 13, 2021 at 12:26
  • By the time I got here I'd forgotten what I was doing.
    – John Hunt
    Oct 19, 2021 at 14:45
12

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"

1
  • This worked on my Redmi Note 8 Pro. Thank you.
    – Lyrk
    Jul 7, 2021 at 19:07
12

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
3
  • 1
    It works, thanks. Nov 12, 2021 at 6:48
  • Exception occurred while executing 'grant': java.lang.SecurityException: Package com.my.android.app has not requested permission android.permission.READ_CONTACTS
    – magrif
    Oct 9, 2022 at 18:09
  • @magrif I'm getting that for read logs as well. I had to enable that option and restart (after disabling it once) to stop getting this error. 16 hours ago
10

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.

2
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Jul 21, 2022 at 11:06
  • 5
    Needing to force shut the app down fixed it for me Dec 26, 2022 at 8:21
7

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)

3

To grant app permissions you need essentially:

  1. Enable Developer Options -> USB debugging
  2. Enable Developer Options -> Disable Permission Monitoring (this is NOT next to the first option, it's all the way down in the "APPS" area)
  3. Restart your device, unlock (pin code), connect it to a pc
  4. Select File Transfer mode
  5. Check that your device is recognized by adb:
PS C:\adb-tools> ./adb devices
* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached
93c53934        device
  1. Force stop app to grant permissions for and then grant permissions:
PS D:\adb-tools> ./adb shell
OnePlus8Pro:/ $ am force-stop com.conena.logcat.reader
OnePlus8Pro:/ $ pm grant com.conena.logcat.reader android.permission.READ_LOGS

Additional hints:

  • In german the (2) option is called "Erlaubnisüberwachung deaktivieren", area is "APPS" (see this Video)
  • The guide was tested on a OnePlus 8 Pro, Android 12
1

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.

7
  • Ok, there's no way to do it directly. But is there anything that can be used to workaround this? like opening settings and automatically clicking on the screen with an accessibility service, or something else? Aug 29, 2018 at 14:36
  • @StefanoSiano there is no full proof workaround for this. This is two step work; 1.) Open app specific setting if you know package name of that app 2.) Toggling via Accessibility I am not sure if it will work 100% because each OEM has their own customized UI and the their ids etc. Aug 30, 2018 at 0:55
  • And I'd like to do it when the screen is off, too... Is there anything that could be done using Device Administrator APIs, or some way to set the app as system app (using any kind of Android settings)? Aug 30, 2018 at 7:12
  • 1
    @StefanoSiano can not be done with screen off. Nutshell you can not do it secretly. Aug 30, 2018 at 7:24
  • 1
    The only way I found is to install another app as system app, signing it with the same key of the ROM (this will work only for pure AOSP, LineageOs or custom ROMs derived from them). This is the approach I currently took for my app, Permission Ruler. I'm still trying to understand if this makes sense and works reliably though. Jan 2, 2019 at 9:51
-1

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

-1

Go to Developer Option then Disable "Verify apps over USB"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.