adb全名Andorid Debug Bridge. 直接翻译安卓调试桥,环境变量中配置好adb的环境变量,直接通过命令行可以省很多事。
配置好环境变量直接 win+X(win10)快捷键打开命令提示符,输入adb 或者 adb help 打印所有提示信息,如图。
Android Debug Bridge version 1.0.26
-d - directs command to the only connected USB device
returns an error if more than one USB device is present.
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
disconnect : - disconnect from a TCP/IP device
device commands:
adb push - copy file/dir to device
adb pull [] - copy file/dir from device
adb sync [ ] - copy host->device only if changed
(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] - 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)
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 help - show this help message
adb version - show version num
DATAOPTS:
(no option) - don't touch the data partition
-w - wipe the data partition
-d - flash the data partition
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.
adb shell 命令
adb 命令和 adb shell 命令 。
简单点讲,adb 命令是 adb 这个程序自带的一些命令,而 adb shell 进入目标设备的Linux Shell环境, 在该环境中可以执行一些Linux命令.
pm list package
列出安装在设备上的应用 不带任何选项:列出所有的应用的包名
adb shell pm list package
-s:列出系统应用
adb shell pm list package -s
-3:列出第三方应用
adb shell pm list package -3
-f:列出应用包名及对应的apk名及存放位置
adb shell pm list package -f
-i:列出应用包名及其安装来源,结果显示例子:
adb shell pm list package -i
package:com.zhihu.android installer=com.xiaomi.market
install 安装命令 pm install , 安装应用
目标 apk 存放于 PC 端,请用 adb install 安装(adb install apk路径)
目标 apk 存放于 Android 设备上,请用 pm install 安装
pm uninstall , 卸载应用,同 adb uninstall , 后面跟的参数都是应用的包名
pm clear , 清除应用数据
pm set-install-location , pm get-install-location , 设置应用安装位置,获取应用安装位置
am
am start , 启动一个 Activity,已启动系统相机应用为例
启动相机
adb shell am start -n com.android.camera/.Camera
Starting: Intent { cmp=com.android.camera/.Camera }
先停止目标应用,再启动
adb shell am start -S com.android.camera/.Camera
Stopping: com.android.camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }
等待应用完成启动
adb shell am start -W com.android.camera/.Camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }
Status: ok
Activity: com.android.camera/.Camera
ThisTime: 500
TotalTime: 500
Complete
启动默认浏览器打开一个网页
adb shell am start -a android.intent.action.VIEW -d http://baidu.com
Starting: Intent { act=android.intent.action.VIEW dat=http://baidu.com }
启动拨号器拨打 10086
adb shell am start -a android.intent.action.CALL -d tel:10086
Starting: Intent { act=android.intent.action.CALL dat=tel:xxxxx }
am force-stop , 后跟包名,结束应用
input
这个命令可以向 Android 设备发送按键事件,其源码 Input.java
-
input text , 发送文本内容,不能发送中文
adb shell input text test123456
前提先将键盘设置为英文键盘
-
input keyevent , 发送按键事件,KeyEvent.java
adb shell input keyevent KEYCODE_HOME
模拟按下 Home 键 ,源码里面有定义:
public static final int KEYCODE_HOME = 3;
因此可以将命令中的
KEYCODE_HOME
替换为3
-
input tap , 对屏幕发送一个触摸事件
adb shell input tap 500 500
点击屏幕上坐标为 500 500 的位置
-
input swipe , 滑动事件
adb shell input swipe 900 500 100 500
从右往左滑动屏幕
如果版本不低于 4.4 , 可以模拟长按事件
adb shell input swipe 500 500 501 501 2000
其实就是在小的距离内,在较长的持续时间内进行滑动,最后表现出来的结果就是长按动作
screencap截图命令
adb shell screencap -p /sdcard/screen.png
截屏,保存至 sdcard 目录
screenrecord4.4 新增的录制命令
adb shell screenrecord sdcard/record.mp4
执行命令后操作手机,ctrl + c 结束录制,录制结果保存至 sdcard
ime输入法,Ime.java
列出设备上的输入法
adb shell ime list -s
com.google.android.inputmethod.pinyin/.PinyinIME
com.baidu.input_mi/.ImeService
选择输入法
adb shell ime set com.baidu.input_mi/.ImeService
Input method com.baidu.input_mi/.ImeService selected
wm Wm.java 获取设备分辨率
adb shell wm size
Physical size: 1080x1920
getprop
查看 Android 设备的参数信息,只运行 adb shell getprop,结果以 key : value 键值对的形式显示,如要获取某个 key 的值:
获取设备的 sdk 版本
adb shell getprop ro.build.version.sdk