From what I can see, when a UIAutomator script is compiled into a jar file and it gets run by using the adb shell command. I can see that there is a -e debug command line option
I've worked it out. The process is a little long winded but it works!
The key to this is understanding how to use the Dalvik Debug Monitor Server (DDMS) and understanding remote debugging with Java and Eclipse. In brief follow the following steps:
Window > Open Perspective > Other... > DDMS
. You should see your device listed in the Devices tab, assuming you have an emulator/device running.Run > Debug Configurations
...Now we will start running the UI Automator script with the debug option using the command line. For my example the command is (all on one line):
adb shell uiautomator runtest AndroidUIAutomation.jar -c com.example.uiautomation.TestUiAutomation -e debug true
It will then say:
Sending WAIT chunk
In Eclipse go into the DDMS perspective. Under the Devices tab you should see a process with a little red bug symbol. Next to it will be a question mark. In the last column in the table there will be two port numbers such as 8602/8700. The port 8700 is the one you will connect your remote debugging session to. This is what should be configured in step 4 above.
Now you are ready to start remote debugging. Set a breakpoint somewhere in the UI Automator script. Then debug by going to Run > Debug Configurations
... and then select the Remote Java Application configuration that you created earlier and then click on "Debug".
If everything has gone well, then you should be able to debug your UI Automator script!