adb shell使用总结

文章目录

日志记录系统概览

复制代码
Android 日志记录系统是系统进程 logd 维护的一组结构化环形缓冲区。这组可用的缓冲区是固定的,并由系统定义。
最相关的缓冲区为:main(用于存储大多数应用日志)、system(用于存储源自 Android 操作系统的消息)和 crash(用于存储崩溃日志)。
每个日志条目都包含一个优先级(VERBOSE、DEBUG、INFO、WARNING、ERROR 或 FATAL)、一个标识日志来源的标记以及实际的日志消息。

adb 使用方式

  1. android 手机打开USB调试模式
  2. 将platform-tool配置到windows的path环境变量中
  3. 将手机和PC使用数据线连接

adb命令

内容不全面,这里仅仅记录经常使用到的命令。

日志过滤

使用的代码片段如下:

复制代码
        findViewById(R.id.btn).setOnClickListener((view) -> {
            Log.d("ABC", "aaa");
            Log.d("aoe", "aaa");
            Log.i("ABC", "aaa");
            Log.w("ABC", "aaa");
            Log.e("ABC", "aaa");
        });
  • 打印日志

    adb logcat

执行这条命令,控制台很快刷屏,可见必须要进行过滤。

  • 清空日志

    adb logcat c

按照告警等级进行过滤

  • 显示警告级别以上的信息

    C:\Users\biobase>adb logcat *:W
    --------- beginning of system
    --------- beginning of main
    ...
    07-26 17:44:08.607 30248 30248 W ABC : aaa
    07-26 17:44:08.607 30248 30248 E ABC : aaa
    ...

  • 显示错误级别以上的信息

    C:\Users\biobase>adb logcat *:E
    --------- beginning of system
    --------- beginning of main
    ...
    07-26 17:44:08.607 30248 30248 E ABC : aaa
    ...

    所有告警等级对应的标签:

    V :Verbose 详尽的打印内容

    D :Debug 默认打印内容

    I :Info 提示打印内容

    W :Warn 警告打印内容

    E :Error 错误打印内容

    F :Fatal 崩溃打印内容

    S :Silent 不打印

    这些告警等级逐渐提高 eg. W时会打印E信息,E时不会打印W信息

按照tag进行过滤

复制代码
C:\Users\biobase>adb logcat -s ABC
--------- beginning of system
--------- beginning of main
07-26 17:44:08.607 30248 30248 D ABC     : aaa

07-26 17:44:08.607 30248 30248 I ABC     : aaa

07-26 17:44:08.607 30248 30248 W ABC     : aaa

07-26 17:44:08.607 30248 30248 E ABC     : aaa

根据告警等级和tag进行联合过滤

复制代码
C:\Users\biobase>adb logcat -s ABC:W
--------- beginning of system
--------- beginning of main
07-30 14:38:51.436 14709 14709 W ABC     : aaa
07-30 14:38:51.436 14709 14709 E ABC     : aaa

屏蔽系统和其他App干扰,仅仅关注App自身日志

  • 获取进程id

    C:\Users\biobase>adb shell ps | findstr com.biobase.helloxx
    u0_a1282 18409 607 3967996 122424 0 0 S com.biobase.helloxx

    C:\Users\biobase>

其中第二列18409是进程id

  • 仅仅显示指定进程id打印的App信息

    C:\Users\biobase>adb logcat -b main -v color --pid 18409
    07-30 14:55:11.880 18409 18409 W Choreographer: OPTS_INPUT: First frame was drawed before optimized, so skip!
    07-30 14:55:25.503 18409 18409 W ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@2983ba7

    07-30 15:11:11.137 18409 18409 D ABC : aaa
    07-30 15:11:11.137 18409 18409 D aoe : aaa
    07-30 15:11:11.137 18409 18409 I ABC : aaa
    07-30 15:11:11.137 18409 18409 W ABC : aaa
    07-30 15:11:11.137 18409 18409 E ABC : aaa
    07-30 15:11:11.272 18409 18409 D ABC : aaa
    07-30 15:11:11.272 18409 18409 D aoe : aaa
    07-30 15:11:11.272 18409 18409 I ABC : aaa

查看"当前页面"Activity

当接手别人的项目或者页面跳转关系复杂时,特别有用

复制代码
C:\Users\biobase>adb shell dumpsys activity | findstr ResumedActivity
    mResumedActivity: ActivityRecord{c44ad9a u0 com.miui.gallery/.activity.InternalPhotoPageActivity t4130}
  ResumedActivity: ActivityRecord{c44ad9a u0 com.miui.gallery/.activity.InternalPhotoPageActivity t4130}

文件传输

在知道具体路径的情况下,文件传输可以非常方便.

  • 将PC文件传输到手机

    adb push d:/a.txt /sdcard/

  • 将手机文件传输到PC

    C:\Users\biobase\Desktop>adb pull /sdcard/bluetooth .
    /sdcard/bluetooth/: 10 files pulled, 0 skipped. 25.0 MB/s (10782875 bytes in 0.411s)

截屏和录屏

  • 截屏

    C:\Users\biobase\Desktop>adb shell screencap /sdcard/a.png

    C:\Users\biobase\Desktop>

为了应对经常截图的需要,稍微学习了一下windows脚本。将下面的内容保存成bat文件,双击一下,手机屏幕截图就到PC上了。

复制代码
@ECHO OFF
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png "%~dp0\screen.png"
ren screen.png "%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%.png"
adb shell rm /sdcard/screen.png

@ECHO ON 
  • 录制视频

    adb shell screenrecord /sdcard/movie.mp4

Ctrl+C停止。

要进一步控制视频的录制,请看下面的参数说明

复制代码
1|grus:/ $ screenrecord --h
Usage: screenrecord [options] <filename>

Android screenrecord v1.2.  Records the device's display to a .mp4 file.

Options:
--size WIDTHxHEIGHT
    Set the video size, e.g. "1280x720".  Default is the device's main
    display resolution (if supported), 1280x720 if not.  For best results,
    use a size supported by the AVC encoder.
--bit-rate RATE
    Set the video bit rate, in bits per second.  Value may be specified as
    bits or megabits, e.g. '4000000' is equivalent to '4M'.  Default 20Mbps.
--bugreport
    Add additional information, such as a timestamp overlay, that is helpful
    in videos captured to illustrate bugs.
--time-limit TIME
    Set the maximum recording time, in seconds.  Default / maximum is 180.
--verbose
    Display interesting information on stdout.
--help
    Show this message

安装、卸载App

  • 安装App

    adb install -r AndroidTest.apk

这里添加 -r 参数可以在不卸载旧版的情况下,进行版本升级(保留App缓存内容)。

  • 卸载APP

    adb uninstall com.example.androidtest

启动activity

在复杂App开发中,会比较常用。可以跳过手动点击一层层跳转,直接打开某页面。

复制代码
adb shell am start com.example.androidtest/.MainActivity
or
adb shell am start com.example.androidtest/com.example.androidtest.MainActivity

这里有一个前提条件,就是页面是可启动的。应用首页或者明确设置exported=true

复制代码
<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>

or

        <activity
            android:name=".SignatureActivity"
            android:exported="true"></activity>

其他

  • 清除App数据

    adb shell pm clear com.example.androidtest

  • 使用默认浏览器打开一个URL

    adb shell am start -a android.intent.action.VIEW -d http://www.baidu.com

  • 关闭/打开手机屏幕

    adb shell input keyevent 26

  • 查看手机屏幕分辨率

    C:\Users\biobase\Desktop>adb shell wm size
    Physical size: 1080x2340

  • 模拟点击按键

    adb shell input keyevent 4 (BACK) 返回键
    adb shell input keyevent 3 (HOME)

相关推荐
xiaokangzhe1 小时前
MySQL主从复制读写分离笔记
笔记·mysql·adb
ego.iblacat6 小时前
MySQL 主从复制与读写分离
android·mysql·adb
炸炸鱼.7 小时前
MySQL 主从复制与读写分离
adb
-ONLY-¥8 小时前
MySQL主从复制与读写分离实战
adb
独隅8 小时前
MacOS 系统下 ADB (Android Debug Bridge) 全面安装与配置指南
android·macos·adb
刘晨鑫19 小时前
MySQL主从复制与读写分离
数据库·mysql·adb
炸炸鱼.1 天前
MySQL 全量、增量备份与恢复(极简易懂版)
android·adb
独隅1 天前
Linux 系统下 ADB 环境 的详细安装步骤和基础设置指南
linux·运维·adb
2301_805348971 天前
MySQL源码编译部署主从及MHA高可用集群实战
adb