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 which waits for a debugger to connect before starting but how do I connect this to the debugger from Eclipse so I can debug my UI Automator script?
A Java program can be debugged simply by right clicking on the Java editor class file from Package explorer. Select Debug As → Java Application or use the shortcut Alt + Shift + D, J instead.
Debugging allows you to run a program interactively while watching the source code and the variables during the execution. A breakpoint in the source code specifies where the execution of the program should stop during debugging. Once the program is stopped you can investigate variables, change their content, etc.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With