0

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?

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Shahal
  • 1,008
  • 1
  • 11
  • 29
  • 1
    No it can't. Here is a small explanation https://stackoverflow.com/a/34112381/3290339 – Onik Mar 07 '19 at 07:19
  • @Onik It says i don't need to pass `adb` in the command. It doesn't say i can't run adb commands from app. Or that's what i understood. If i'm wrong, can you just explain it to me ? – Shahal Mar 07 '19 at 07:26
  • 1
    you missed the key point in my answer... How can you run 'adb' commands without using 'adb' itself? This is a Linux binary, called 'adb' and the commands are it's arguments which are to be parsed by the binary. Using the command without 'adb' doesn't make any sense. – Onik Mar 07 '19 at 07:52
  • @Onik oh okay. Thanks for the help. Can you look into this as well https://stackoverflow.com/q/55003244/5742437 – Shahal Mar 07 '19 at 07:55

1 Answers1

1

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.

Shahal
  • 1,008
  • 1
  • 11
  • 29