任何更快的转储UI层次结构的方法?

use*_*562 6 android android-ui android-layout android-uiautomator

现在我正在使用uiautomator像这样转储UI:

adb shell uiautomator dump
Run Code Online (Sandbox Code Playgroud)

它工作正常,但执行它需要大约3秒钟.所以我想知道是否有更快的方法呢?就像创建一个转储UI的服务一样,还是需要一段时间?

use*_*562 8

猜猜我应该回答我自己的问题,因为我发现了一个更好的方法.我发现这个项目使用uiautomator togheter与轻量级rpc服务器,所以你可以发送命令到设备:

https://github.com/xiaocong/android-uiautomator-server#build

这使得倾销几乎立即,并且工作得非常好.如果你想看看如何进行rpc调用,他还有一个python项目:

https://github.com/xiaocong/uiautomator

但我在这里创造了一个小例子.

启动服务器:

# 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')
Run Code Online (Sandbox Code Playgroud)

调用命令的函数("ping","dumpWindowHierarchy"等):

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']
Run Code Online (Sandbox Code Playgroud)

请注意,您必须先将文件(bundle.jar anduiautomator-stub.jar)从第一个项目推送到设备,然后将它们放在"/ data/local/tmp /"中