32

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Stefano Siano
  • 569
  • 1
  • 4
  • 13
  • 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 – Stefano Siano Aug 31 '18 at 09:39
  • See also https://android.stackexchange.com/questions/241608/neither-user-2000-nor-current-process-has-android-permission-grant-runtime-perm – U. Windl Oct 22 '22 at 19:24

10 Answers10

54

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

Community
  • 1
  • 1
Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
49

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.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
brand
  • 501
  • 5
  • 7
  • 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. – Stefano Siano Jan 02 '19 at 09:50
  • 1
    Still works with MIUI12. Note that you will have to sign into your MI account before being able to do this. – Agoston Horvath Sep 12 '20 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 – Matija Nalis May 13 '21 at 12:26
  • By the time I got here I'd forgotten what I was doing. – John Hunt Oct 19 '21 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"

jkk
  • 131
  • 1
  • 5
11

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
9

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.

Sjoerd Hemminga
  • 136
  • 1
  • 4
  • 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](/review/late-answers/32260726) – The Dreams Wind Jul 21 '22 at 11:06
  • 4
    Needing to force shut the app down fixed it for me – Jesse_Pinkman Dec 26 '22 at 08:21
6

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)

Harald
  • 71
  • 1
  • 3
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.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • 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? – Stefano Siano Aug 29 '18 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. – Vikasdeep Singh Aug 30 '18 at 00: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)? – Stefano Siano Aug 30 '18 at 07:12
  • 1
    @StefanoSiano can not be done with screen off. Nutshell **you can not do it secretly**. – Vikasdeep Singh Aug 30 '18 at 07:24
  • Then my app will be useless without root :/ I'll think about something else then, thanks. I'll leave the question open just a couple of days, hoping someone will come with an idea to face this question in another way – Stefano Siano Aug 30 '18 at 11:05
  • @StefanoSiano sure you can wait but this is unfortunately not possible as I mentioned in my answer above. – Vikasdeep Singh Sep 01 '18 at 13:39
  • 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. – Stefano Siano Jan 02 '19 at 09:51
1

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
FireEmerald
  • 1,002
  • 12
  • 21
-1

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
-1

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