问题
Right now I'm using uiautomator do dump the UI like this:
adb shell uiautomator dump
And it works fine except that it takes around 3 seconds to perform it. So I wonder if there is a faster way to do it? Like creating a service that dump the UI or will it take just as long?
回答1:
Guess I should answer my own question as I found a better way to do it. I found this project that use 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 make the dumping almost instantly and works really nice. He also have a python project if you want to see how to make the rpc calls:
https://github.com/xiaocong/uiautomator
But I have 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')
Function to call commands ("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']
Note that you have to push the files (bundle.jar anduiautomator-stub.jar) from the first project to the device first and put them in "/data/local/tmp/"
回答2:
Seems like an old thread but here is an updated solution. This can simply be achieved in JS/Node applications by using following npm module. This module uses the same server as mentioned in the above answer but does it seemlesly.
npm install uiautomator-server
Then add following code in your project:
const UIAutomator = require('uiautomator-server');
const myDevice = new UIAutomator((err, currDevice) => {
if (err) {
console.log(`Error starting UI Automator server : ${err}`);
}
console.log(`UI Automator server started`);
currDevice.dump(false, (error, xmlDump) => {
if (error) {
console.log(`Error while getting dump : ${error}`);
}
console.log(`xmlDump in as string: ${xmlDump}`);
});
});
Starting the server(creating new automator object) may take some time (around 1 sec), But once the serve is started, dump and other operations are very fast i.e less than 300 ms in most cases. You can create a new UIAutomator object once and then use the same instance later for all your operations
来源:https://stackoverflow.com/questions/27256911/any-faster-way-to-dump-ui-hierarchy