zoukankan      html  css  js  c++  java
  • 【Android】ADB常用指令与logcat日志

    【Android】ADB常用指令与logcat日志

    http://www.csdn123.com/html/itweb/20131015/168574.htm

    ADB命令简介

      ADB是一个功能强大的命令行工具。通过它可以直接和模拟器或真机进行交互。它是一个具有客户端和服务器端的程序。

      它主要由三个部分组成

    1. 客户端,它运行在你的开发机上,你可以通过执行adb命令来唤起一个客户端。其他的工具例如:ADT插件以及DDMS也可以创建一个ADB客户端。
    2. 服务端,它运行在你的后台进程,这个服务管理客户端和运行在你模拟起或真机上的守护进程。
    3. 守护进程,它运行在每台模拟器或真机的后台进程。

      当你启动一个adb客户端的时候,客户端会首先检测是否已经有adb服务进程运行,如果没有运行,那么它会首先启动一个服务进程,当服务进程启动后,服务进程绑定本地的5037端口,并且监听所有来自adb 客户端的命令,所有的adb客户端通过5037与adb 服务端进行交互。

      服务端运行之后与所有运行的模拟器或真机建立链接,它通过一组范围从5555 到5585的奇数的端口检索到所有的模拟器或真机。每一台模拟器或真机需要获取一对连续的端口,偶数端口用于控制台的链接,奇数端口用于adb的链接。

      当然,如果你用Eclipse开发,并且也安装了ADT插件,那么你没必要通过adb命令行与模拟器或真机交互,ADT插件已经完美的将adb整合进了Eclipse IDE.当然还是提倡多使用adb命令行,这样的话比较方便自定义debug方法,同时也可以更好的理解其工作原理。

    Adb命令列表

      下列表格列出了adb支持的命令,并对它们的意义和使用方法做了说明.

    Category Command Description Comments
    Options -d 仅仅通过USB接口来管理abd. 如果不只是用USB接口来管理则返回错误.
    -e 仅仅通过模拟器实例来管理adb. 如果不是仅仅通过模拟器实例管理则返回错误.
    -s <serialNumber> 通过模拟器/设备的允许的命令号码来发送命令来管理adb (比如: "emulator-5556"). 如果没有指定号码,则会报错.
    General devices 查看所有连接模拟器/设备的设施的清单. 查看 Querying for Emulator/Device Instances获取更多相关信息.
    help 查看adb所支持的所有命令。.  
    version 查看adb的版本序列号.  
    Debug logcat [<option>] [<filter-specs>] 将日志数据输出到屏幕上.  
    bugreport 查看bug的报告,如dumpsys ,dumpstate ,和logcat 信息。  
    jdwp 查看指定的设施的可用的JDWP信息. 可以用 forward jdwp:<pid> 端口映射信息来连接指定的JDWP进程.例如: 
    adb forward tcp:8000 jdwp:472 
    jdb -attach localhost:8000
    Data install <path-to-apk> 安装Android为(可以模拟器/设施的数据文件.apk指定完整的路径).  
    pull <remote> <local> 将指定的文件从模拟器/设施的拷贝到电脑上.  
    push <local> <remote> 将指定的文件从电脑上拷贝到模拟器/设备中.  
    Ports and Networking forward <local> <remote> 用本地指定的端口通过socket方法远程连接模拟器/设施 端口需要描述下列信息:
    • tcp:<portnum>
    • local:<UNIX domain socket name>
    • dev:<character device name>
    • jdwp:<pid>
    ppp <tty> [parm]... 通过USB运行ppp:
    • <tty> — the tty for PPP stream. For exampledev:/dev/omap_csmi_ttyl.
    • [parm]...  &mdash zero or more PPP/PPPD options, such as defaultroute ,local , notty , etc.

    需要提醒你的不能自动启动PDP连接.

     
    Scripting get-serialno 查看adb实例的序列号. 查看 Querying for Emulator/Device Instances可以获得更多信息.
    get-state 查看模拟器/设施的当前状态.
    wait-for-device 如果设备不联机就不让执行,--也就是实例状态是 device 时. 你可以提前把命令转载在adb的命令器中,在命令器中的命令在模拟器/设备连接之前是不会执行其它命令的. 示例如下:
    adb wait-for-device shell getprop
    需要提醒的是这些命令在所有的系统启动启动起来之前是不会启动adb的 所以在所有的系统启动起来之前你也不能执行其它的命令. 比如:运用install 的时候就需要Android包,这些包只有系统完全启动。例如:
    adb wait-for-device install <app>.apk
    上面的命令只有连接上了模拟器/设备连接上了adb服务才会被执行,而在Android系统完全启动前执行就会有错误发生.
    Server start-server 选择服务是否启动adb服务进程.  
    kill-server 终止adb服务进程.  
    Shell shell 通过远程shell命令来控制模拟器/设备实例. 查看 获取更多信息 for more information.
    shell [<shellCommand>] 连接模拟器/设施执行shell命令,执行完毕后退出远程shell端l.

      adb命令参数详解

    # adb --help
    Android Debug Bridge version 1.0.29
    
     -d                            - directs command to the only connected USB device
                                     returns an error if more than one USB device is present.
     -e                            - directs command to the only running emulator.
                                     returns an error if more than one emulator is running.
     -s             - directs command to the USB device or emulator with
                                     the given serial number. Overrides ANDROID_SERIAL
                                     environment variable.
     -p      - simple product name like 'sooner', or
                                     a relative/absolute path to a product
                                     out directory like 'out/target/product/sooner'.
                                     If -p is not specified, the ANDROID_PRODUCT_OUT
                                     environment variable is used, which must
                                     be an absolute path.
     devices                       - list all connected devices
     connect [:]       - connect to a device via TCP/IP
                                     Port 5555 is used by default if no port number is specified.
     disconnect [[:]]  - disconnect from a TCP/IP device.
                                     Port 5555 is used by default if no port number is specified.
                                     Using this command with no additional arguments
                                     will disconnect from all connected TCP/IP devices.
    
    device commands:
      adb push      - copy file/dir to device
      adb pull  []  - copy file/dir from device
      adb sync [  ]     - copy host->device only if changed
                                     (-l means list but don't copy)
                                     (see 'adb help all')
      adb shell                    - run remote shell interactively
      adb shell           - run remote shell command
      adb emu             - run emulator console command
      adb logcat [  ] - View device log
      adb forward   - forward socket connections
                                     forward specs are one of:
                                       tcp:
                                       localabstract:
                                       localreserved:
                                       localfilesystem:
                                       dev:
                                       jdwp: (remote only)
      adb jdwp                     - list PIDs of processes hosting a JDWP transport
      adb install [-l] [-r] [-s] [--algo  --key  --iv ]
                                   - push this package file to the device and install it
                                     ('-l' means forward-lock the app)
                                     ('-r' means reinstall the app, keeping its data)
                                     ('-s' means install on SD card instead of internal storage)
                                     ('--algo', '--key', and '--iv' mean the file is encrypted already)
      adb uninstall [-k]  - remove this app package from the device
                                     ('-k' means keep the data and cache directories)
      adb bugreport                - return all information from the device
                                     that should be included in a bug report.
    
      adb backup [-f ] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] []
                                   - write an archive of the device's data to .
                                     If no -f option is supplied then the data is written
                                     to "backup.ab" in the current directory.
                                     (-apk|-noapk enable/disable backup of the .apks themselves
                                        in the archive; the default is noapk.)
                                     (-shared|-noshared enable/disable backup of the device's
                                        shared storage / SD card contents; the default is noshared.)
                                     (-all means to back up all installed applications)
                                     (-system|-nosystem toggles whether -all automatically includes
                                        system applications; the default is to include system apps)
                                     ( is the list of applications to be backed up.  If
                                        the -all or -shared flags are passed, then the package
                                        list is optional.  Applications explicitly given on the
                                        command line will be included even if -nosystem would
                                        ordinarily cause them to be omitted.)
    
      adb restore            - restore device contents from the  backup archive
    
      adb help                     - show this help message
      adb version                  - show version num
    
    scripting:
      adb wait-for-device          - block until device is online
      adb start-server             - ensure that there is a server running
      adb kill-server              - kill the server if it is running
      adb get-state                - prints: offline | bootloader | device
      adb get-serialno             - prints:
      adb status-window            - continuously print device status for a specified device
      adb remount                  - remounts the /system partition on the device read-write
      adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
      adb reboot-bootloader        - reboots the device into the bootloader
      adb root                     - restarts the adbd daemon with root permissions
      adb usb                      - restarts the adbd daemon listening on USB
      adb tcpip              - restarts the adbd daemon listening on TCP on the specified port
    networking:
      adb ppp  [parameters]   - Run PPP over USB.
     Note: you should not automatically start a PPP connection.
      refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
     [parameters] - Eg. defaultroute debug dump local notty usepeerdns
    
    adb sync notes: adb sync [  ]
       can be interpreted in several ways:
    
      - If  is not specified, both /system and /data partitions will be updated.
    
      - If it is "system" or "data", only the corresponding partition
        is updated.
    
    environmental variables:
      ADB_TRACE                    - Print debug information. A comma separated list of the following values
                                     1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
      ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.
      ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.
    View Code

      查看所有当前通过USB设备连接的设备

    # adb devices
    List of devices attached
    emulator-5554    device
    0288424641407417    device
    HT9CRP810308    device
    View Code

      指定某一个模拟器或真机

    # adb -s emulator-5554 shell

      打印日志

    # adb logcat

      上传一个文件

    # adb push ./sina_login.png /mnt/sdcard/DCIM/Camera/sina_login.png

      上传一个目录

    # adb push ./emo /mnt/sdcard/DCIM/Camera/emo

      下载一个文件

    # adb pull /mnt/sdcard/DCIM/Camera/IMG_20121130_102210.jpg ./IMG_20121130_102210.jpg
    280 KB/s (24741 bytes in 0.086s)

      下载一个目录这里不用设置递归参数-R,而且空目录没有下载下来

    # adb pull /mnt/sdcard/GigaTalk ./GigaTalk

      安装APK

    # adb install BigramPaid.apk
    1872 KB/s (30541229 bytes in 15.930s)
        pkg: /data/local/tmp/BigramPaid.apk

      指定安装到SD卡

    # adb install -s BigramPaid.apk
    2262 KB/s (30541229 bytes in 13.180s)
        pkg: /sdcard/tmp/BigramPaid.apk
    Success

      再次安装,提示错误信息:已安装

    # adb install -s BigramPaid.apk
    2322 KB/s (30541229 bytes in 12.839s)
        pkg: /sdcard/tmp/BigramPaid.apk
    Failure [INSTALL_FAILED_ALREADY_EXISTS]
    View Code

      重新安装,保留原有数据,允许共用包名及数字签名

    # adb install -r BigramPaid.apk
    2526 KB/s (30541229 bytes in 11.806s)
        pkg: /data/local/tmp/BigramPaid.apk

      保留数据以及缓存目录

    # adb uninstall com.gigabud.bigram

      重新加载/system 分区

    # adb remount
    remount succeeded

      安装APK过程中常见的一些错误信息

    int INSTALL_FAILED_ALREADY_EXISTS                       //程序已经存在
    int INSTALL_FAILED_CONFLICTING_PROVIDER            //存在同名的内容提供者
    int INSTALL_FAILED_DEXOPT                //dex优化验证失败
    int INSTALL_FAILED_DUPLICATE_PACKAGE            //已存在同名程序
    int INSTALL_FAILED_INSUFFICIENT_STORAGE            //没有足够的存储空间
    int INSTALL_FAILED_INVALID_APK                //无效的APK
    int INSTALL_FAILED_INVALID_URI                //无效的链接
    int INSTALL_FAILED_MISSING_SHARED_LIBRARY        //需求的共享库已丢失
    int INSTALL_FAILED_NO_SHARED_USER            //要求的共享用户不存在
    int INSTALL_FAILED_OLDER_SDK                //系统版本过旧
    int INSTALL_FAILED_REPLACE_COULDNT_DELETE
    int INSTALL_FAILED_SHARED_USER_INCOMPATIBLE        //需求的共享用户签名错误
    int INSTALL_FAILED_UPDATE_INCOMPATIBLE            //版本不能共存
    int INSTALL_PARSE_FAILED_BAD_MANIFEST            //错误的MANIFEST配置文件
    int INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME        //错误的包名
    int INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID        //错误的共享用户
    int INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING        //证书编码无效
    int INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES    //证书不一致
    int INSTALL_PARSE_FAILED_MANIFEST_EMPTY            //MANIFEST配置文件为空
    int INSTALL_PARSE_FAILED_MANIFEST_MALFORMED        //MANIFEST配置文件非法
    int INSTALL_PARSE_FAILED_NOT_APK            //不是有效的APK文件
    int INSTALL_PARSE_FAILED_NO_CERTIFICATES        //无认证证书
    int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION        //异常
    int INSTALL_SUCCEEDED                    //安装成功
    int REPLACE_EXISTING_PACKAGE                //包名已存在
    View Code

     


    启用logcat日志

      Android日志系统提供了记录和查看系统调试信息的功能。日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命令来查看和使用.

    Logcat命令列表

    Option Description
    -b <buffer> 加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing Alternative Log Buffers.
    -c 清楚屏幕上的日志.
    -d 输出日志到屏幕上.
    -f <filename> 指定输出日志信息的<filename> ,默认是stdout .
    -g 输出指定的日志缓冲区,输出后退出.
    -n <count> 设置日志的最大数目<count> .,默认值是4,需要和 -r 选项一起使用。
    -r <kbytes> <kbytes> 时输出日志,默认值为16,需要和-f 选项一起使用.
    -s 设置默认的过滤级别为silent.
    -v <format> 设置日志输入格式,默认的是brief 格式,要知道更多的支持的格式,参看Controlling Log Output Format .

    使用logcat命令

      你可以用 logcat 命令来查看系统日志缓冲区的内容

    [adb] logcat [<option>] ... [<filter-spec>] ... 

      请查看Listing of logcat Command Options ,它对logcat命令有详细的描述 .

      你也可以在你的电脑或运行在模拟器/设备上的远程adb shell端来使用logcat 命令,也可以在你的电脑上查看日志输出

    $ adb logcat  //logcat

    过滤日志输出

      每一个输出的Android日志信息都有一个标签和它的优先级.

    • 日志的标签是系统部件原始信息的一个简要的标志。(比如:“View”就是查看系统的标签).
    • 优先级有下列集中,是按照从低到高顺利排列的:
      • V — Verbose (lowest priority)
      • D — Debug
      • I — Info
      • W — Warning
      • E — Error
      • F — Fatal
      • S — Silent (highest priority, on which nothing is ever printed)

      在运行logcat的时候在前两列的信息中你就可以看到 logcat 的标签列表和优先级别,它是这样标出的:<priority>/<tag> .

      下面是一个logcat输出的例子,它的优先级就似乎I,标签就是ActivityManage:

    I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

      为了让日志输出能体现管理的级别,你还可以用过滤器来控制日志输出,过滤器可以帮助你描述系统的标签等级.

      过滤器语句按照下面的格式描tag:priority ... tag 表示是标签, priority 是表示标签的报告的最低等级.

      从上面的tag的信息中可以得到日志的优先级. 你可以在过滤器中多次写tag:priority .

      这些说明都只到空白结束。

      下面有一个列子,例子表示支持所有的日志信息,除了那些标签为”ActivityManager”和优先级为”Info”以上的和标签为” MyApp”和优先级为” Debug”以上的。 小等级,优先权报告为tag.

    adb logcat ActivityManager:I MyApp:D *:S

      上面表达式的最后的元素 *:S ,,是设置所有的标签为"silent",所有日志只显示有"View" and "MyApp"的,用 *:S 的另一个用处是能够确保日志输出的时候是按照过滤器的说明限制的,也让过滤器也作为一项输出到日志中.

      下面的过滤语句指显示优先级为warning或更高的日志信息:

    adb logcat *:W

    控制日志输出格式

      日志信息包括了许多元数据域包括标签和优先级。

      修改日志的输出格式,可以显示出特定的元数据域。可以通过 -v 选项得到格式化输出日志的相关信息。

    • brief — Display priority/tag and PID of originating process (the default format).
    • process — Display PID only.
    • tag — Display the priority/tag only.
    • thread — Display process:thread and priority/tag only.
    • raw — Display the raw log message, with no other metadata fields.
    • time — Display the date, invocation time, priority/tag, and PID of the originating process.
    • long — Display all metadata fields and separate messages with a blank lines.

      当启动了logcat ,你可以通过-v 选项来指定输出格式

    [adb] logcat [-v <format>]

      下面是用 thread 来产生的日志格式:

    adb logcat -v thread

      需要注意的是你只能-v 选项来规定输出格式 option.

    查看可用日志缓冲区

      Android日志系统有循环缓冲区,并不是所有的日志系统都有默认循环缓冲区。

      为了得到日志信息,你需要通过-b 选项来启动logcat 。如果要使用循环缓冲区,你需要查看剩余的循环缓冲期

    • radio — 查看缓冲区的相关的信息.
    • events — 查看和事件相关的的缓冲区.
    • main — 查看主要的日志缓冲区

      -b 选项使用方法:

    [adb] logcat [-b <buffer>]

      下面的例子表示怎么查看日志缓冲区包含radio 和 telephony信息:

    adb logcat -b radio

    不完全汇总 

    (1) 显示全部日志信息:adb logcat

    (2) 显示某一TAG的日志信息:adb logcat -s TAG名称

    (3) 显示某一TAG的某一级别的日志信息:adb logcat TAG名称:级别.....TAG名称:级别 *:S

      注:日志级别(V-verbose,D-debug,I-info,W-warning,E-error,F-fatal,S-silent),*:S(确保日志输出的时候是按照过滤器的说明限制)

    (4) 显示某一级别以上的全部日志信息:adb logcat *:级别

    (5) 以某种格式显示日志信息:adb logcat -v 格式

      注:日志格式(brief,process,tag,thread,raw,time,long)

    (6) 显示缓冲区中的日志信息:adb logcat -b 缓冲区类型

      注:缓冲区类型(radio-无线缓冲区,events-事件缓冲区,main-主缓冲区,默认)

    (7) 清理已经存在的日志:adb logcat -c

    (8) 将日志显示在控制台后退出:adb logcat -d

    (9) 将日志输出到文件:adb logcat -f 文件名

    参考文章

      http://www.cnblogs.com/playing/archive/2010/09/19/1830799.html

      http://www.cnblogs.com/ithouge/archive/2012/12/09/ithouge_android_adb.html

  • 相关阅读:
    [Xarmrin.IOS]使用Build Host 在Windows上建置IOS程式及DeBug (转帖)
    [Xamarin.Android]使用SqliteNET (转帖)
    [Xamarin.Android]使用Java Bindings Libary專案 Binding Java元件(.jar) 與Metadata.xml、型別對應 (转帖)
    [iOS]使用Windows Azure來做iOS的推播通知 (转帖)
    [Xamarin.Android] 結合Windows Azure與Google cloud message 來實現Push Notification (转帖)
    [Xamarin.Android] 儲存資料於Windows Azure (转帖)
    [Xamarin.Android]如何引用JAR檔案 (转帖)
    [Xamarin.iOS] 如何引用Objective-c寫的Class Library (转帖)
    [Xamarin.Android] 如何使用Google Map V2 (转帖)
    JS函数
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3647988.html
Copyright © 2011-2022 走看看