Any faster way to reset user interface hierarchy?

I am currently using uiautomator to reset the user interface as follows:

adb shell uiautomator dump 

And it works great, except that it takes about 3 seconds to complete. So I wonder if there is a faster way to do this? How to create a service that resets the user interface or will take so much time?

+6
source share
1 answer

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) # Forward adb ports subprocess.call('adb forward tcp:9008 tcp:9009') 

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 /"

+7
source

Source: https://habr.com/ru/post/979008/


All Articles