I think I should answer my question, because I found the best way to do this. I found this project that uses uiautomator togheter with a light weight rpc server so you can send commands to the device:
https://github.com/xiaocong/android-uiautomator-server#build
This makes dumping almost instantly and works very well. It also has a python project if you want to see how to make rpc calls:
https://github.com/xiaocong/uiautomator
But I created a small example here.
Start the server:
# Start the process process = subprocess.Popen( 'adb shell uiautomator runtest ' 'bundle.jar uiautomator-stub.jar ' '-c com.github.uiautomatorstub.Stub', stdout=subprocess.PIPE, shell=True)
Command invocation function ("ping", "dumpWindowHierarchy", etc.):
def send_command(self, method_name, *args): """ Send command to the RPC server Args: method_name(string): Name of method to run *args: Arguments """ data = { 'jsonrpc': '2.0', 'method': method_name, 'id': 1, } if args: data['params'] = args request = urllib2.Request( 'http://localhost:{0}/jsonrpc/0'.format(self.local_port), json.dumps(data), { 'Content-type': 'application/json' }) try: result = urllib2.urlopen(request, timeout=30) except Exception as e: return None if result is None: return None json_result = json.loads(result.read()) if 'error' in json_result: raise JsonRPCError('1', 'Exception when sending command to ' 'UIAutomatorServer: {0}'.format( json_result['error'])) return json_result['result']
Please note that first you need to transfer the files (bundle.jar anduiautomator-stub.jar) from the first project to the device and place them in "/ data / local / tmp /"
source share