I have an issue running uiautomator in my python script (a similar shell script runs fine). I use the following method for convenience:
import subprocess
def host_exec(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out= p.communicate()
return out
then I use a combination of
host_exec("path_to_adb shell uiautomator dump /sdcard/dump.xml")
host_exec("path_to_adb pull /sdcard/dump.xml ./dump.xml")
--read file here--
host_exec("path_to_adb shell rm /sdcard/dump.xml")
host_exec("rm ./dump.xml")
I use this a lot, and the first 1-2 run throughs usually work, after that it seems to sporadically fail at line 1 (creating the dump.xml) giving error code
ERROR: null root node returned by UiTestAutomationBridge.
I looked into UiTestAutomationBridge.java (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/accessibilityservice/UiTestAutomationBridge.java) and it seems to me that the cause is that the timeout that the devs placed on line 65, that is sometimes enough, and sometimes not enough for the bridge to form correctly. (correct me if I'm wrong.
What really bugs me is that I've been using the same code for almost a week, and I only got the error for the first time this morning, and now it's happening almost every single time. I've checked to make sure I close the file after reading it, I delete the files before dumping and copying new files, and I have tried up to time.sleep(10) before and after each command, but still no go. I've run the 'time' command from command line for my shell script that hasn't failed a single time yet, and it takes about 2 seconds for the entire process, so 10 seconds should be enough for anything that is bogging down to get sorted out.