Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send messages back and forth between ADB shell and an Android app?

I've been looking for an answer or the past 3 days, and haven't yet found one that works. I'm trying to write an Android app that can be controlled from ADB with custom commands. Is there anyway I can send strings back an forth between an app and an ADB shell?

Thanks in advance, and sorry for my noob qustion.

like image 542
Lai Xue Avatar asked Jun 18 '12 02:06

Lai Xue


1 Answers

Sure, there are several ways to do this.

  • You could use a unix domain socket, and open it from both the android app and from a command line executable you would build by abusing the ndk, push to a version-dependent location on the device (/data/local, /sqlite_stmt_journals, etc) and run. Edit: in more recent android versions there may not be such writable/executable directories. You may have to have the app itself write the executable out to its private directory and set global read and execute permissions on it. Further Edit: adb can forward unix sockets, too.

  • Same thing with an internet socket, only now you have the option of setting up an adb port forward (provided the android app is the 'server' end) so as to communicate from a process running on your development machine directly with the android app, without passing data through the adb shell. Unless declaring internet permission is objectionable (it should be less of a concern than letting your PC-side app "drive" adb) this is probably the method that would stick closest to "official" capabilities and have the least android version dependence. It also can be trivially adapted to communicating over wifi.

  • You could use a pair of fifos and write and read them with shell commands (for portability, create them in the app's private storage but make them world readable/writeable)

  • you may be able to play some games with a pty

  • you could I suppose use files as mailboxes

  • you can use the 'am' command to send Intents (useful at least to start up the android app, if a bit inefficient for the communication)

like image 188
Chris Stratton Avatar answered Oct 31 '22 17:10

Chris Stratton