3

I'm trying to send a JSON argument to an activity, and it seems like I can't do so via a command like the following:

adb shell am start -n <activity> -e argument_name '{"blah":"blah"}'

Any double quotes inside of the JSON string get stripped out once my app gets them ("blah":"blah" becomes blah:blah). I have to launch adb shell first, then run am start so that the quotes don't get stripped out. This happens on Windows Powershell.

I tried the solution suggested here by Jerry101 (i.e. custom handling of quotes) but that didn't help. I would prefer not to enter the shell first because I want to automate the starting of android apps from a Powershell script. This means running the adb shell command that fires off commands to the shell like the example above (as a one-liner).

Community
  • 1
  • 1
luxchar
  • 625
  • 1
  • 7
  • 15
  • No because that is for linux. instead of "\" should be whatever escape character is for Powershell – Chisko Feb 03 '17 at 04:03
  • 2
    Possible duplicate of [Escaping quotes and double quotes](http://stackoverflow.com/questions/18116186/escaping-quotes-and-double-quotes) – Chisko Feb 03 '17 at 04:05
  • `echo 'am start -n -e argument_name ''{"blah":"blah"}''' | adb shell` – Alex P. Feb 03 '17 at 05:27

2 Answers2

6

Through experimentation, I've figured out that I need another single quote inside of the initial single quotes (and each single quote needs to be escaped by using pair of them). The double quotes need to be escaped using a back-slash character. Here is an example that works:

adb shell am start -n <activity> -e argument_name '''{\"blah\":\"blah\"}'''

That works for my case. The receiving side (which is a Unity) sees the double quotes.

luxchar
  • 625
  • 1
  • 7
  • 15
2

As pointed in comments, you should find out how to escape chars in Powershell.

The alternative could be creating a file containing your command

# file.sh
am start -n <activity> -e argument_name '{"blah":"blah"}'

then

adb push file.sh /storage/self/primary/file.sh
adb shell sh /storage/self/primary/file.sh
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 1
    In my case, I needed to put another single quote inside of the single quotes that I had. I also needed to use a backslash to escape the double quotes. So the correct version would be: am start -n -e argument_name '''{\"blah\":\"blah\"}''' – luxchar Feb 23 '17 at 03:26