Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use adb to grant permission without root?

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

like image 850
Stefano Siano Avatar asked Aug 29 '18 14:08

Stefano Siano


People also ask

Can adb work without root?

Sure. You can set up and utilize an ADB shell without your Android based device being rooted. You are simply restricted from running ADB as root and making system-level changes. You can push files to your device, pull files from your device, sideload app packages and updates, etc., and many more non-root functions.

How do I get root permission from adb?

The general steps to enable it is Settings>About Phone and tab the Build Number seven times to unlock Developer Options. Now go to Developer Options in System Settings and activate the root option (ADB only or Apps and ADB). After this, adb will restart and you will be able to use root from the cmd line.


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

like image 165
brand Avatar answered Sep 24 '22 17:09

brand


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

like image 29
Elia Weiss Avatar answered Sep 25 '22 17:09

Elia Weiss


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
like image 35
Arshit Vaghasiya Avatar answered Sep 23 '22 17:09

Arshit Vaghasiya


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"

like image 45
jkk Avatar answered Sep 23 '22 17:09

jkk