Search code examples
androidadbandroid-permissionsandroid-6.0-marshmallow

Can i call grant permission ADB command from app


I am having an issue granting WRITE_EXTERNAL_STORAGE runtime permission in the app. On accepting the permission app is restarting, this I figured out is because of our custom firmware.

What I like to know is that, can we call ADB command for granting permission from the app. I've tried calling like this

final String STORAGE_PERMISSION_COMMAND = "sh -c adb -d shell pm grant com.example.easypermission.easypermissionsample android.permission.WRITE_EXTERNAL_STORAGE";
try {
     Runtime.getRuntime().exec(STORAGE_PERMISSION_COMMAND,null,null);
} catch (IOException e) {
    e.printStackTrace();
}

But nothing is happening. Can someone tell me whether this can be done, if so how?


Solution

  • As the link @Onik shared said, we can't run ADB commands inside app. When you run an app, its code is executed in the environment. So when you call Runtime.getRuntime().exec("adb shell command") what you actually do is trying to start another adb server process (on a target device now) which starts on tcp port 5038, since port 5037 is busy.