Android 日志记录系统是系统进程 logd 维护的一组结构化环形缓冲区。这组可用的缓冲区是固定的,并由系统定义。
最相关的缓冲区为:main(用于存储大多数应用日志)、system(用于存储源自 Android 操作系统的消息)和 crash(用于存储崩溃日志)。
每个日志条目都包含一个优先级(VERBOSE、DEBUG、INFO、WARNING、ERROR 或 FATAL)、一个标识日志来源的标记以及实际的日志消息。
内容不全面,这里仅仅记录经常使用到的命令。
使用的代码片段如下:
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信息
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
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
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
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
当接手别人的项目或者页面跳转关系复杂时,特别有用
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}
在知道具体路径的情况下,文件传输可以非常方便.
adb push d:/a.txt /sdcard/
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]
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
adb install -r AndroidTest.apk
这里添加 -r 参数可以在不卸载旧版的情况下,进行版本升级(保留App缓存内容)。
adb uninstall com.example.androidtest
在复杂App开发中,会比较常用。可以跳过手动点击一层层跳转,直接打开某页面。
adb shell am start com.example.androidtest/.MainActivity
or
adb shell am start com.example.androidtest/com.example.androidtest.MainActivity
这里有一个前提条件,就是页面是可启动的。应用首页或者明确设置exported=true
or
adb shell pm clear com.example.androidtest
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)