11

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?

Zhichao
  • 599
  • 5
  • 14

3 Answers3

11

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");
tophernuts
  • 389
  • 9
  • 14
  • hello, how do i get the Test.jar file ? I'm able to run the uiautomator test from Android Studio. But I get an APK from the build and not a Jar file. Can you please help ? – HaseebR7 Jun 15 '16 at 19:29
  • I haven't done this in a while, but [this link](https://looksok.wordpress.com/2014/02/08/uiautomator-in-eclipse/) seemed to work for me. Essentially just: * create your project: `android create uitest-project -n yes -t 10 -p .` * build your jar : `ant build` – tophernuts Jun 15 '16 at 20:07
  • why It says`Didn't find class "com.tonghu.testpublishwechatmoment.PublishWechantMomentTest" on path:[zip file "/sdcard/androidTest.jar"]`, I had push my androidTest.jar to /sdcard/ – user3875388 Dec 01 '16 at 02:36
5

You need ADB connection (over WIFI or Cable) to run UiAutomator test cases unless you have su permissions. With su permission you can run uiautomator from the device itself.

In UiAutomator test cases, you can implement socket, webSocket and some other communication protocols, so your test case will expose communication connection to the outside world and other devices can connect to it. In this case, you need ADB connection only once to run the test case, then you can disconnect it.

Amir
  • 129
  • 8
1

You can execute your code remotely like.

If your device is rooted you can then first connect your device with machine and use adb tcpip 5555. This will forward listening port on 5555 and then you can execute your script on your machine which will run on device. just use adb -s shell