adb

常规指令

日志

使用android/log.h来打印日志,并且通过logcat进行日志抓取。 native里面这样实现

#ifdef __ANDROID__
#include <android/log.h>
#endif

int main () {
	__android_log_print(ANDROID_LOG_ERROR, "kaihang_test", "Try to alloc_ion buffer...");
	return 0;
}

通过logcat进行日志抓取

# *:S mute所有其他的日志,kaihang_test:V 表示放开tag kaihang_test的verbose级别的日志
adb logcat *:S kaihang_test:V

# 如果要专门抓取Crash的日志的话
adb logcat *:F

# 清空日志
adb logcat -c

编译的时候,记得-llog,并且不要把NDK中的你链接的liblog.so推上去,会打印不出来日志的。

传输文件

# 上传文件
adb push --sync /path/to/local /path/to/remote

# 下载文件
adb pull --sync /path/to/remote [/path/to/local]

执行指令

# 打开终端
adb shell

# 执行指令
adb shell "remote指令"

查看信息

# 查看所有连接的设备
adb devices

# 打印序列号
adb shell cat /sys/devices/soc0/serial_number

# 打印系统信息可以打印GPU型号、EGL版本、扩展、OpenGL ES版本等。
adb shell dumpsys SurfaceFlinger

# 打印程序内存占用情况
adb shell dumpsys meminfo <package_name>/<PID>

权限

# 以root模式打开 adb
adb root

# 打开某些目录的读写权限 比如:/vendor /odm 等
adb remount

# selinux 限制读写
setenforce 0

# 退出root
adb unroot

端口映射

# 这样可以将手机端的1338 tcp端口映射到手机
adb forward tcp:1338 tcp:1338

远程调试

# 首先在android手机usb连接的端执行一下指令
# 该指令会使得手机开启5555调试端口
adb tcpip 5555

# 查看手机的ip,在手机的设置上查看,假如是192.168.4.172
# 在需要调试的机器上执行
adb connect 192.168.4.172:5555

# 随后执行adb devices就可以看到该手机了