0

I am able to execute an adb shell command of type from the terminal:

./adb shell 'sh -c "param1 param2 param3 param4"'

However, when I am trying to run this inside android app using Runtime.exec the above command gets broken down into different params of the form

[adb, shell, 'sh, -c, "param1, param2, param3, param4"]

However, that is a single input param when ran from shell. How to run this inside the app?

note: param is a kind of path e.g. /a/b/c/

grad9
  • 39
  • 11

1 Answers1

2

To avoid funkyness with how your strings are split, it's usually safer to just use the multi-paramter version of Runtime.exec:

System.getRuntime().exec("adb", "shell", "sh -c \"param1 param2 param3 param4\"");

Note that in this specific call you've got 2 arguments to the adb executable. shell and the whole command which is going to be parsed by the shell on the Android device. The whole command always has to go into a separate argument as demonstrated above.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • thanks..it worked 95% exact... saved the tussle.....! I just added single ' ' to make it work for me, in accordance to the question's sample... – grad9 Jun 05 '18 at 03:10