Android 有没有办法通过 adb 获取当前活动的布局和视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26586685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:55:01  来源:igfitidea点击:

Is there a way to get current activity's layout and views via adb?

androidadbandroid-viewandroid-uiautomator

提问by Filipe Arruda

For environment reasons I can only use adbcommands.

出于环境原因,我只能使用adb命令。

Is there a wayto get the current layout attributes like id, position, textetc ?

有没有办法让像目前的布局属性idpositiontext

Similar to what uiautomatorviewershows.

类似于uiautomatorviewer显示的内容。

回答by Alex P.

Use adb shell uiautomator dumpcommand:

使用adb shell uiautomator dump命令:

Usage: uiautomator <subcommand> [options]

Available subcommands:

help: displays help message

runtest: executes UI automation tests
    runtest <class spec> [options]
    <class spec>: <JARS> < -c <CLASSES> | -e class <CLASSES> >
      <JARS>: a list of jar files containing test classes and dependencies. If
        the path is relative, it's assumed to be under /data/local/tmp. Use
        absolute path if the file is elsewhere. Multiple files can be
        specified, separated by space.
      <CLASSES>: a list of test class names to run, separated by comma. To
        a single method, use TestClass#testMethod format. The -e or -c option
        may be repeated. This option is not required and if not provided then
        all the tests in provided jars will be run automatically.
    options:
      --nohup: trap SIG_HUP, so test won't terminate even if parent process
               is terminated, e.g. USB is disconnected.
      -e debug [true|false]: wait for debugger to connect before starting.
      -e runner [CLASS]: use specified test runner class instead. If
        unspecified, framework default runner will be used.
      -e <NAME> <VALUE>: other name-value pairs to be passed to test classes.
        May be repeated.
      -e outputFormat simple | -s: enabled less verbose JUnit style output.

dump: creates an XML dump of current UI hierarchy
    dump [--verbose][file]
      [--compressed]: dumps compressed layout information.
      [file]: the location where the dumped XML should be stored, default is
      /storage/emulated/legacy/window_dump.xml

events: prints out accessibility events until terminated

By default it dumps the views hierarchy to $EXTERNAL_STORAGE/window_dump.xml

默认情况下,它将视图层次结构转储到 $EXTERNAL_STORAGE/window_dump.xml

adb shell uiautomator dump
UI hierchary dumped to: /sdcard/window_dump.xml

Usually you would want to pull that file to your PC for further processing which would be an extra step. But there is a neat trick which allows to combine dumping and pulling into a single command. Using /dev/ttyas a dump destination would make a single command which would print the whole dump to the stdout:

通常,您希望将该文件拉到您的 PC 上进行进一步处理,这将是一个额外的步骤。但是有一个巧妙的技巧可以将转储和拉取合并到一个命令中。使用/dev/tty为转储目的地会使一个命令这将打印整个转储到stdout

adb exec-out uiautomator dump /dev/tty
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><hierarchy rotation="0"><node ...></node></hierarchy>UI hierchary dumped to: /dev/tty

回答by apricot

adb pull $(adb shell uiautomator dump | grep -oP '[^ ]+.xml') /tmp/view.xml

Open /tmp/view.xmlin a web browser like:

/tmp/view.xml在 Web 浏览器中打开,例如:

google-chrome /tmp/view.xml

adb exec-out uiautomator dump /dev/tty

adb exec-out uiautomator 转储 /dev/tty

from the other answer didn't work for me.

来自另一个答案对我不起作用。

回答by Diego Torres Milano

AndroidViewClient/culebra's dumptool lets you do precisely that. AndroidViewClient can be used as a library and also provides some tools like dumpand culebra.

AndroidViewClient/culebradump工具可让您做到这一点。AndroidViewClient 可以用作库,还提供了一些工具,如dumpculebra

usage: dump [OPTION]... [serialno]

Options:
  -H, --help                       prints this help                             
  -V, --verbose                    verbose comments                             
  -v, --version
  -I, --ignore-secure-device       ignore secure device                         
  -E, --ignore-version-check       ignores ADB version check                    
  -F, --force-view-server-use      force view server use (even if UiAutomator present:w)
  -S, --do-not-start-view-server   don't start ViewServer                       
  -k, --do-not-ignore-uiautomator-killed don't ignore UiAutomator killed              
  -w, --window=WINDOW              dump WINDOW content (default: -1, all windows)
  -a, --all                        dump all information about Views             
  -i, --uniqueId                   dump View unique IDs                         
  -x, --position                   dump View positions                          
  -b, --bounds                     dump View bounds                             
  -d, --content-description        dump View content descriptions               
  -g, --tag                        dump View tags                               
  -c, --center                     dump View centers                            
  -f, --save-screenshot=FILE       save screenshot to file                      
  -W, --save-view-screenshots=DIR  save View screenshots to files in directory  
  -D, --do-not-dump-views          don't dump views, only useful if you specified -f or -W
  -A, --device-art=MODEL           device art model to frame screenshot (auto: autodetected)
  -Z, --drop-shadow                drop shadow for device art screenshot        
  -B, --glare                      screen glare over screenshot                 
  -h, --use-uiautomator-helper     use UiAutomatorHelper Android app            
  -X, --debug=LIST                 debug options 

running dumpwith no options, just prints the tree of Views and some of its basic properties, like ID

dump无选项运行,只打印视图树及其一些基本属性,如 ID

android.widget.FrameLayout  
   com.android.launcher3.Workspace com.google.android.apps.nexuslauncher:id/workspace 
      android.widget.FrameLayout com.google.android.apps.nexuslauncher:id/workspace_blocked_row 
         android.widget.TextView com.google.android.apps.nexuslauncher:id/date_text1 May 1
         android.widget.TextView com.google.android.apps.nexuslauncher:id/date_text2 MONDAY, 2017
      android.widget.TextView  Maps
   android.widget.ImageView com.google.android.apps.nexuslauncher:id/g_icon 
   android.widget.ImageView com.google.android.apps.nexuslauncher:id/all_apps_handle 
   android.view.ViewGroup com.google.android.apps.nexuslauncher:id/layout 
      android.widget.TextView  Messenger
      android.widget.TextView  Chrome

on the other hand, if you want the positions of the views, you can run

另一方面,如果你想要视图的位置,你可以运行

$ dump -x

$转储 -x

android.widget.FrameLayout   (0, 0, 1440, 2392)
   com.android.launcher3.Workspace com.google.android.apps.nexuslauncher:id/workspace  (0, 0, 1440, 2392)
      android.widget.FrameLayout com.google.android.apps.nexuslauncher:id/workspace_blocked_row  (30, 126, 1380, 372)
         android.widget.TextView com.google.android.apps.nexuslauncher:id/date_text1 May 1 (1037, 194, 337, 162)
         android.widget.TextView com.google.android.apps.nexuslauncher:id/date_text2 MONDAY, 2017 (1092, 356, 282, 73)
      android.widget.TextView  Maps (30, 1614, 276, 372)
   android.widget.ImageView com.google.android.apps.nexuslauncher:id/g_icon  (30, 214, 276, 196)
   android.widget.ImageView com.google.android.apps.nexuslauncher:id/all_apps_handle  (636, 1986, 168, 98)
   android.view.ViewGroup com.google.android.apps.nexuslauncher:id/layout  (0, 2084, 1440, 308)
      android.widget.TextView  Messenger (306, 2112, 276, 280)
      android.widget.TextView  Chrome (858, 2112, 276, 280)

The options will give you control over the output. You can get some more info here.

这些选项将使您能够控制输出。您可以在此处获得更多信息。

回答by running-codebase

I used the following on a Mac to print formatted xml to the console:

我在 Mac 上使用以下命令将格式化的 xml 打印到控制台:

adb shell uiautomator dump && adb pull /sdcard/window_dump.xml $TMPDIR && more $TMPDIR/window_dump.xml | xmllint --format -

adb shell uiautomator dump && adb pull /sdcard/window_dump.xml $TMPDIR && more $TMPDIR/window_dump.xml | xmllint --format -

You may need to change the path of the /sdcard/window_dump.xmlfile to pull based on where the xml file is dumped.

您可能需要/sdcard/window_dump.xml根据 xml 文件的转储位置更改要拉取的文件的路径。

回答by Oush

Use the following to view the xml layout in notepad or your default text editor

使用以下命令在记事本或默认文本编辑器中查看 xml 布局

adb shell uiautomator dump && adb pull /sdcard/window_dump.xml && start window_dump.xml

回答by Andrii Kovalchuk

In 2020 it's recommended to use a tool built-in Android Studio.

2020 年建议使用 Android Studio 内置工具。

  1. Connect your phone
  2. Follow the screenshot
  1. 连接您的手机
  2. 按照屏幕截图

Source: https://developer.android.com/studio/debug/layout-inspector

来源:https: //developer.android.com/studio/debug/layout-inspector

enter image description here

在此处输入图片说明