How can I use adb to grant permission without root?
Asked Answered
C

10

42

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

Chemoprophylaxis answered 29/8, 2018 at 14:3 Comment(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 finishChemoprophylaxis
See also android.stackexchange.com/questions/241608/…Rudolfrudolfo
U
72

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

Restart your device.

Now the pm command should work

Unscrupulous answered 30/8, 2019 at 6:40 Comment(7)
Perfect solution. I encountered this error in Android 10. This setting works as expected. ThanksNatachanatal
Can't find any such thing in Debugger Options (on an Android 10 Samsungs Active Tab 3) :-/Winder
It is not working on Android 12 (OnePlus 9), still giving the same error.Sharitasharity
not working in latest phonesAnciently
@Sharitasharity 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.Crosby
And don't forget to restart your deviceEveretteverette
This worked great on a Xiaomi Mi Mix Fold 3Mcarthur
B
54

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.

Bureaucratic answered 27/12, 2018 at 0:6 Comment(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.Chemoprophylaxis
Still works with MIUI12. Note that you will have to sign into your MI account before being able to do this.Lanta
And you have to have working SIM card inserted. If your SIM slot is busted for example, you are out of luckBoozy
By the time I got here I'd forgotten what I was doing.Strained
C
17

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"

Cyrenaica answered 22/2, 2021 at 12:42 Comment(2)
This worked on my Redmi Note 8 Pro. Thank you.Dexterous
Nice, works for me, now find another error android.permission.DUMP allow' was 255 instead of 0 any solution here?Climatology
M
13

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
Menstruum answered 7/4, 2021 at 9:44 Comment(3)
It works, thanks.Kinghood
Exception occurred while executing 'grant': java.lang.SecurityException: Package com.my.android.app has not requested permission android.permission.READ_CONTACTSBankruptcy
@Bankruptcy 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.Blanks
I
11

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.

Indianapolis answered 16/7, 2022 at 8:8 Comment(2)
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 ReviewLupita
Needing to force shut the app down fixed it for meNahuatl
K
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)

Kuban answered 30/12, 2019 at 15:40 Comment(0)
M
7

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
Madelinemadella answered 21/5, 2023 at 13:55 Comment(1)
In my phone the 2nd option is called "Berechtigungsüberwachung deaktivieren" in German.Socialize
P
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.

Phosphorylase answered 29/8, 2018 at 14:6 Comment(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?Chemoprophylaxis
@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.Phosphorylase
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)?Chemoprophylaxis
@StefanoSiano can not be done with screen off. Nutshell you can not do it secretly.Phosphorylase
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 wayChemoprophylaxis
@StefanoSiano sure you can wait but this is unfortunately not possible as I mentioned in my answer above.Phosphorylase
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.Chemoprophylaxis
E
-1

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

Equities answered 8/5, 2022 at 14:1 Comment(0)
N
-1

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

Nativity answered 4/5, 2023 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.