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 <serial number> - directs command to the USB device or emulator with
the given serial number. Overrides ANDROID_SERIAL
environment variable.
-p <product name or path> - 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 <host>:<port> - connect to a device via TCP/IP
disconnect <host>:<port> - disconnect from a TCP/IP device
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> [<local>] - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] [-s] <file> - 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] <package> - 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: <serial-number>
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 <port> - restarts the adbd daemon listening on TCP on the specified port
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> 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 [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> 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