2014-12-02 71 views
6

现在,我使用uiautomator做倾倒这样的UI:任何更快的转存UI层次结构的方法?

adb shell uiautomator dump 

,它工作正常,除了它需要3秒钟,进行了它。所以我想知道是否有更快的方法来做到这一点?就像创建一个转储UI的服务一样,还是需要花费很长时间?

回答

7

猜我应该回答我自己的问题,因为我找到了一个更好的方法来做到这一点。我发现这个项目有重量轻RPC服务器togheter使用uiautomator这样你就可以将命令发送到设备:

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

这使得倾销几乎立即和作品真的很好。他也有一个Python项目,如果你想看看如何让RPC调用:

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') 

函数调用命令( “中国平安”, “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'] 

请注意,你必须按下文件(bundle.jar anduiautomator -stub.jar),并将它们放在“/ data/local/tmp /”中