ADB Shell学习

来源:互联网 发布:何处不是白鹿原 知乎 编辑:程序博客网 时间:2024/04/28 15:52

ADB Shell 学习

又是一个周末了,最怕闲来无事的周末,于是给自己找点事情做,ADB shell这一块一直没有系统的学习,正好趁此机会学习。

主要通过学习资料和adb –help文档学习

怎么使用ADB shell命令

首先找到Andorid的SDK安装路径或者单独安装ADB组件,sdk中adb路径在

cd <adb-path>#执行adb命令adb shell

Mac下

#配置adb环境变量vi ~/.bash_profile#编辑该文件,添加你的adb路径export ADB_PATH= your_adb_pathexport PATH=${PATH}:${ADB_PATH}#退出保存再执行命令使得环境变量生效source .bash_profile

ADB Debugging命令

adb devices

列出连接的设备

adb devices [-l] ‘-l’

参数用于指定需要列出的设备

#打印连接的设备adb devices

返回结果

#执行命令返回设备的序列数字和状态2b70fc6a        device

adb forward

重定向连接,需要开启设备的USB debugging模式

adb forward <local> <remote>
adb froward –no-rebind <local> <remote>作用同上,但是如果已经连接就会失败

adb forward –remove <local>删除指定连接的设备

adb forward –remove-all

#映射本地的8000端口到设备的端口9000adb forward tcp:8000 tcp:9000

adb kill-server

终止adb服务进程 如果服务在运行则终止

adb kill-server

无线连接命令

adb connect

通过WIFI使用ADB

adb connect <host> [:<port>]

第一步 通过USB连接设备

第二步 使用命令查看连接的设备

adb devices

注意: 以上步骤不可忽略

第三步 以TCP模式重启端口:5555

第四步 查看Android设备的IP地址:设置->关于手机->状态->IP地址,将该IP地址以 #.#.#.# 的格式记录下来

第五步 执行命令

#   #.#.#.# 为刚刚记录下来的ip地址adb connect #.#.#.#

第六步 拔掉usb连接线,确认设备是否依然可连接

adb devices

返回结果

#.#.#.#:5555 device

注意: 确保本地和设备连接的wifi为可访问的同一个局域网

adb disconnect

断开通过TCP/IP连接的设备

adb disconnect [<host> [:<port>]]

不带参数则断开所有TCP/IP连接的设备

adb usb

重启USB模式的 ADB

adb usb

App包的管理命令

adb install

安装Android应用到设备,需要指定需要安装的 .apk 文件的全路径

adb install [option]

adb install test.apk
# 给apk上锁,发布 apk 到 android market上时,可以设置相关标志位来保护你的 app。adb install -l test.apk
# 重新安装apkadb install -r test.apk
# 允许测试adb install -t test.apk
# 在sdcard上安装adb install -s test.apk
#允许低版本代码adb install -d test.apk
#授予所有运行权限adb install -g test.apk

adb install-multiple

一次安装多个apk文件

adb install-multiple [-lrtsdpg]

adb uninstall

从设备中卸载app

adb uninstall [-k]

adb uninstall com.test.app
adb uninstall -k com.test.app

adb shell pm list packages

打印出设备安装的所有包信息,可选参数用于过滤要显示的包名

adb shell pm list packages [options]

adb shell pm list packages
#查看相关的文件adb shell pm list packages -f
#只显示禁用的packagesadb shell pm list packages -d
# 只显示可用的packagesadb shell pm list packages -e
#只显示系统级别的packagesadb shell pm list packages -s
#只显示第三方的packages(非系统)adb shell pm list packages -3
#查看安装器(比如google play)adb shell pm list packages -i
# 包括卸载的packagesadb shell pm list packages -u
#查询用户空间adb shell pm list packages --user <USER_ID&gt;

adb shell pm path

打印制定APK的路径

adb shell pm path <PACKAGE>

adb shell pm path com.android.phone

返回结果

package:/system/priv-app/TeleService/TeleService.apk

adb shell pm clear

删除所有有关的数据

adb shell pm clear

adb shell pm clear com.test.abc

返回结果

clearing app data, cache

文件管理

adb pull

从设备下载指定的文件到电脑

adb pull <remote> [local]

#下载 /sdcard/demo.mp4文件到 <android-sdk-path>/platform-tools 目录下adb pull /sdcard/demo.mp4
#下载demo.mp4到 /Users/bsty/Desktop/adb pull /sdcard/demo.mp4 /Users/bsty/Desktop/

adb push

从电脑上传指定文件到设备

adb push <local> <remote>

#复制 <android-sdk-path>/platform-tools/test.apk 到设备的 /sdcard目录下adb push test.apk /sdcard
#复制 /Users/bsty/Desktop/test.apk 到 /sdcard 目录下adb push /Users/bsty/Desktop/test.apk /sdcard

adb shell ls

列出目录类容

* ls [option] <directory>*

第一步.

adb shell

第二步

ls
#显示隐藏的文件ls -a 
#打印每个文件的序号ls -i
#以块的形式打印出每个文件的大小ls -s
#列出详细信息 UIDs和GIDsls -n
#列出所有子目录(递归)ls -R 

提示: 按 Ctrl+C 终止

adb shell cd

切换目录

cd <directory>

第一步

adb shell

第二步

cd /system

adb shell rm

删除文件或者目录

rm [option] <files or directory>

第一步

adb shell

第二步

rm /sdcard/test.txt

#不需要提示强制删除rm -f /sdcard/test.txt 
#删除所有子文件夹内容rm -r /sdcard/tmp
#删除一个目录,即使不是空目录rm -d /sdcard/tmp

提示: rm -d 和rmdir命令相同

#在删除前会有提示信息rm -i /sdcard/test.txt

adb shell mkdir

创建文件夹

mkdir [options] <directory name>

mkdir /sdcard/tmp
#设置权限mkdir -m 777 /sdcard/tmp
#当需要时创建父目录mkdir -p /sdcard/tmp/sub1/sub2

adb shell touch

创建空文件或者修改文件的时间戳

touch [options] <file>

第一步

adb shell

第二步

touch /sdcard/tmp/test.txt

ls /sdcard/tmp

adb pwd

打印当前目录的位置

adb shellpwd

adb shell cp

复制文件和目录

cp [options] <source> <dest>

第一步

adb shell

第二步

cp /sdcard/test.txt /sdcard/demo.txt

adb shell mv

移动或者重命名文件

mv [options] <source> <dest>

第一步

adb shell

第二步

#移动mv /sdcard/tmp /system/tmp
#重命名mv /sdcard/tmp /sdcard/test

网络

adb shell netstat

网络统计

netstat

第一步

adb shell

第二步

netstat

adb shell ping

测试两个网络之间的连接和延迟

第一步

adb shell

第二步

ping www.google.com

adb shell netcfg

通过配置文件管理和配置网络

netcfg [<interface> {dhcplupdown}]

第一步

adb shell

第二步

netcfg

adb shell ip

显示,处理路由,设备,策略路由和隧道

ip [ OPTIONS ] OBJECT

OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |netns | l2tp }

OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |-f[amily] { inet | inet6 | ipx | dnet | link } |-l[oops] { maximum-addr-flush-attempts } |-o[neline] | -t[imestamp] | -b[atch] [filename] |-rc[vbuf] [size]}

第一步

adb shell

第二步

#显示wifi IP地址ip -f inet addr show wlan0

日志

adb logcat

在屏幕上打印日志

adb logcat [option] [filter-specs]

adb logcat

提示: 按 Ctrl-C 停止

#最低优先级,只显示Verbose级别的日志adb logcat *:V 
#只显示 Debug级别的日志adb logcat *:D 
#只显示 info 级别的日志adb logcat *:I 
#只显示Warning级别的日志adb logcat *:W
#只显示Error级别的日志adb logcat *:E
#只显示Fatal级别的日志adb logcat *:F
# 最高优先级,没有打印过的日志adb logcat *:S 

adb logcat -b <Buffer>

#查看包含radio/telephony相关的消息缓冲区adb logcat -b radio
# 包含事件相关的缓冲区adb logcat -b event
#默认adb logcat -b main
#清除整个日志并退出adb logcat -c
#转储日志到屏幕并退出adb logcat -d
#将日志信息写入test.logs文件adb logcat -f test.logs
#打印指定日志buffer的大小并退出adb logcat -g 
#设置日志的最大数adb logcat -n <count>

提示: 默认的值是4. 需要 -r 参数

#Rotates the log file every <kbytes> of outputadb logcat -r <kbytes>

提示: 默认值为16, 需要-f参数

#设置默认 过滤器为 silentadb logcat -s 

adb logcat -v <format>

# 显示优先级/标签并在过程中发出消息(默认格式)的PID。adb logcat -v brief
# 只显示 PIDadb logcat -v process
#只显示优先级和标签adb logcat -v tag
#显示原始日志信息,没有其他数据字段adb logcat -v raw
#显示日期,调用时,优先级/标签,进程发出消息的PID。adb logcat -v time
#显示日期,调用时间,优先级,标记,和该线程发出消息的PID和TIDadb logcat -v threadtime
#显示所有元数据字段和空行分开的消息adb logcat -v long

adb shell dumpsys

转储系统数据

adb shell dumpsys [options]

adb shell dumpsys
adb shell dumpsys battery
#搜集设备的电池信息adb shell dumpsys batterystats
#清除旧信息adb shell dumpsys batterystats -reset 

adb shell dumpstate

转储状态

adb shell dumpstate
#转储信息存到一个文件adb shell dumpstate > state.logs

截屏

adb shell screencap

adb shell screencap <filename>

adb shell screencap /sdcard/screen.png

从设备下载一个文件

adb pull /sdcard/screen.png

adb shell screenrecord

录制屏幕,android4.4(api 19)以上可用

adb shell screenrecord [options] <filename>

adb shell screenrecord /sdcard/demo.mp4

(按 Ctrl-C停止录屏)

从设备下载该录像文件

adb pull /sdcard/demo.mp4

提示:按Ctrl-C停止录屏,默认3分钟自动停止,也可以添加参数 –time-limit 设置录制时间

#设置视频大小:1280*720. 默认为设备分辨率,最好使用设备支持的分辨率adb shell screenrecord --size <WIDTH*HEIGHT>
#设置视频的bit比,默认4Mbps,可以增加比例提升视频清晰度,但是也会增大文件大小,#例子: bit比为5Mbps, *** adb shell screenrecord --bit-rate 5000000 /sdcard/demo.mp4adb shell screenrecord --bit-rate <RATE>
#设置最长(秒),默认为180秒(3分钟)adb shell screenrecord --time-limit <TIME>
#旋转输出90度。实验性功能adb shell screenrecord --rotate
#控制台显示日志信息,如果没有设置该参数,不会在录屏是显示任何信息adb shell screenrecord --verbose

系统

adb root

以root权限重新启动adbd守护进程

adb root

注意:在生产环境中adbd不能以root模式执行,只能用于测试

adb sideload

恢复Andr​​oid update.zip包。

adb sideload <upload.zip>

adb shell ps

打印进程状态信息

ps [options]

第一步

adb shell

第二步

ps

ps -p

adb shell top

显示顶层的cpu进程

top [options]

第一步

adb shell

第二步

top

提示: 按Ctrl-C停止**

#以线程形式展示top -t 

adb shell getprop

通过property service获取设备属性

getprop [options]

第一步

adb shell

第二步

getpropgetprop ro.build.version.sdkgetprop  ro.chipnamegetprop | grep adb 

adb shell setprop

设置property service

**setprop <key> <value>

第一步

adb shell

第二步

setprop service.adb.tcp.prot 5555
2 0