Can one Android application control another application via UI Automator?

Zhichao picture Zhichao · Jun 6, 2013 · Viewed 7.4k times · Source

I am trying to write an Android application/service which can be deployed on the target device. The app can be used as a hook to remotely control a target device. Starting from Jelly Bean release, there is the UI Automator implementation available, which provides similar functionality. However, it seems that UI Automator can only be used via ADB interface. Application running on the device cannot use UI Automator directly(???). I am trying to find a solution that can work without the help of the ADB. For example, the hook can listen on a socket as a protobuf server. The client can send command to the hook to remotely control and device. I looked into the Andorid SDK source code. It looks like the only way is to use android accessibility APIs. I am wondering if there is any better way?

Answer

tophernuts picture tophernuts · Jul 10, 2013

It is possible to run UiAutomator from an application, you just need to have your Test jar on the device and give your application su permissions.

From your application you can then just call:

uiautomator runtest Test.jar -c com.package.name.ClassName -e key value

And your device will perform whatever your UiAutomatorTestCase would perform.

Quick example:

Process rt = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(rt.getOutputStream());

os.writeBytes("uiautomator runtest Testing.jar -c com.hey.rich.CalculatorDemo" + "\n");
os.flush();
os.writeBytes("exit\n");