Android adb常用命令
adb是什么
Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components.
i. A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as DDMS also create adb clients.
ii. A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device.
iii. A daemon, which runs as a background process on each emulator or device instance.
看到篇文章可以熟悉一下 adb 源碼分析
adb的全稱為Android Debug Bridge,就是起到調試橋的作用
adb是android sdk里的一個工具, 用這個工具可以直接操作管理android模擬器或者真實的andriod設備.有三部分組件:
i. 一個客戶端,它運行在您的開發環境上。你可以通過一個終端來使用adb命令。如其他Android DDMS工具也是創建一個adb客戶端。
ii.一個服務器,它作為一個后臺進程運行在您的開發環境上。服務器管理客戶端和adb進程運行在模擬器/設備之間的通信。
iii.一個進程,作為后臺進程運行在模擬器/設備的實例。
adb常用的一些命令
adb命令用于自動化測試中,將為Android測試帶來很大的便利。
- 查看幫助
adb
或adb help
- 獲取應用啟動項
adb shell monkey -p 包名 -c android.intent.category.LAUNCHER -v -v 0 | grep Using
- 獲取當前activity
adb shell dumpsys activity activities | grep mFocusedActivity
adb shell dumpsys window | grep mCurrent
- 獲取包安裝路徑
adb shell pm path 包名
- 開啟-結束adb服務
adb start-server
adb kill-server
- 獲取設備列表信息
adb devices
操作多臺設備時,adb -s <設備序列號> + <命令>
- 重啟手機
adb reboot
- 獲取設備狀態
adb get-state
devices:正常連接,offline:無響應,unknown:沒有連接設備 - 獲取手機mac地址
adb shell cat /sys/class/net/wlan0/address
- 手機CPU序列號
adb shell cat /proc/cpuinfo
- 卸載apk
adb uninstall <package name>
例:adb uninstall com.ex.example
- 卸載apk,但保留數據
adb uninstall -k <package name>
- 安裝apk
adb install <apk file>
例:adb install example.apk
保證apk路徑無誤。 - 保留數據,重新安裝apk
adb install -r <apk file>
- 系統日志
adb logcat
adb logcat > d:\logcat.log
- bugreport
adb bugreport
adb bugreport > d:\bugreport.log
打印dumpsys、dumpstate、logcat的輸出,也是用于分析錯誤 - 復制文件到本地
adb pull sdcard/1.txt d:\
- 推送文件到設備
adb push d:\1.txt sdcard/
- 無線連接設備
usb連上設備后,
查看設備的ip:adb shell ifconfig wlan0
wlan0: ip 192.168.20.10 mask 255.255.254.0 flags [up broadcast running multicast]
adb tcpip 5555
log:restarting in TCP mode port: 5555
adb connect 192.168.20.10:5555
提示 connected to 192.168.20.64:5555 就可以拔掉usb了,
切換回usbadb usb
- adb shell 包含太多內容
啟動應用
adb shell am start -n <package_name>/<activity_class_name>
例:adb shell am start -n com.ex.example/com.example.activity.WelcomActivityNew
列出系統應用adb shell pm list package
列出第三方應用adb shell pm list package -s
列出apk包的位置adb shell pm path com.ex.example
adb shell ps
adb shell top
- adb 輸入、滑動等操作
輸入域操作adb shell input <string>
物理鍵操作adb shell input keyevent <key code number or name>
keyevent指的是android對應的keycode,比如home鍵的keycode=3,back鍵的keycode=4
點擊操作adb shell input tap <x> <y>
(x,y)坐標
滑動操作adb shell input swipe <x1> <y1> <x2> <y2>
- cpu值
adb shell dumpsys cpuinfo
adb shell dumpsys cpuinfo |grep packagename
adb shell top -n 1| grep PackageName
- 獲取內存
adb shell dumpsys meminfo
adb shell dumpsys meminfo pakagename or Pid
- 獲取流量
adb shell cat /proc/Pid/net/dev
Pid為進程id - 截圖
adb shell /system/bin/screencap -p /sdcard/screen01.png
adb pull /sdcard/screen01.png E:\11ss
- 屏幕錄制
adb shell screenrecord /sdcard/record.mp4
adb pulll /sdcard/record.mp4
- 給app賦權限
adb shell pm grant cn.com.weshare.jiekuan android.permission.INTERNET
- 發送按鍵事件
adb shell input keyevent KEYCODE_HOME
67 退格 3 home - 對屏幕發送一個觸摸事件
adb shell input tap 500 500
- 滑動事件
adb shell input swipe 300 300 300 900