学而实习之 不亦乐乎

Android:adb 命令实用技巧

2022-01-13 10:41:02

一、应用管理

查看所有应用
# adb shell pm list packages

查看系统应用
# adb shell pm list packages -s

查看第三方应用
# adb shell pm list packages -3

包名包含某字符串的应用,比如要查看包名包含字符串 huawei 的应用列表,命令:
# adb shell pm list packages huawei

安装卸载
# adb install <apk文件路径>
# adb install -r <apk文件路径>     通过install命令来安装apk文件,-r参数可以重新安装某个应用并保留应用数据
# adb install -r ~/chrome.apk

卸载应用:
# adb uninstall <软件名>
# adb uninstall -k < 软件名>         如果加 -k 参数,为卸载软件但是保留配置和缓存文件
# adb uninstall com.android.chrome

打印所有包名
# adb shell pm list packages
package:com.huawei.floatMms
package:com.android.defcontainer
package:com.tencent.mm

打印制定包的apk路径
# adb shell pm path com.android.phone
# adb shell pm path com.huawei.android.launcher
package:/system/app/HwLauncher6.apk

删除制定包
# adb shell pm clear com.test.abc

包名管理命令,获得对应包名的对应apk路径
# adb shell pm  path com.migu.lobby

二、模拟输入

1.模拟点击
# adb shell input tap 100 100

2.模拟滑动

# adb shell input swipe x1 y1 x2 y2 
# adb input touchscreen swipe x1 y1 x2 y2 100

# adb shell input swipe 100 100 400 100  300 #左往右
# adb shell input swipe 400 100 100 100  300 #右往左
# adb shell input swipe 100 100 100 400  300 #上往下
# adb shell input swipe 100 400 100 100  300 #下往上
# adb shell input swipe 100 100 400 400  300 #上往下斜
# adb shell input swipe 400 400 100 100  300 #下往上斜

3.模拟长按

# adb shell input swipe 100 100 100 100  1000 //在 100 100 位置长按 1000毫秒
# adb shell input swipe 367 469 367 469 800

4.截图

# adb shell screencap /sdcard/screen.png
# adb pull /sdcard/screen.png #下载到本地

5.获取被点击的位置信息

# adb shell getevent

>
/dev/input/event0 3 39 3e1
/dev/input/event0 1 14a 1
/dev/input/event0 1 145 1
/dev/input/event0 3 35 406  //x坐标
/dev/input/event0 3 54 1083  //y坐标
/dev/input/event0 0 0 0
/dev/input/event0 3 39 ffffffff
/dev/input/event0 1 14a 0
/dev/input/event0 1 145 0
/dev/input/event0 0 0

getevent -l -c 16
输出所有event设备的基本信息

add device 1: /dev/input/event2
  name:     "hi6421_on"
could not get driver version for /dev/input/mouse0, Not a typewriter
add device 2: /dev/input/event4
  name:     "huawei,touchscreen"
add device 3: /dev/input/event0
  name:     "mhl_rcp_dev"
could not get driver version for /dev/input/mice, Not a typewriter
add device 4: /dev/input/event1
  name:     "hisi_gpio_key.14"
add device 5: /dev/input/event3
  name:     "hi3630_hi6401_CARD Headset Jack"

getevent -c 10 //输出10条信息后退出
getevent -l  //将type、code、value以对应的常量名称显示

6.keyevent 模拟输入

adb shell input keyevent 20 #向下
adb shell input keyevent 4 #返回
adb shell input keyevent 3 #Home
adb shell input keyevent 6 #挂机
adb shell input keyevent 84 #搜索
adb shell input keyevent 26 #电源
adb shell input keyevent 24 #音量+
adb shell input keyevent 25 #音量-

三、与活动交互

1.打开对应的activity
# adb shell am start -n {包(package)名}/{包名}.{活动(activity)名称}
# adb shell am start com.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity

2.获得当前活动窗口的信息,包名以及活动窗体
# adb shell dumpsys window windows | grep mCurrent 

3.使用dumpsys命令可以查看Android手机当前正在运行的Activity
# adb shell dumpsys activity activities | findstr "Run"

4.使用 uiautomator dump 获取app上的页面元素
# adb shell uiautomator dump /data/local/tmp/uidump.xml
# adb shell uiautomator dump /sdcard/dump.xml

四、文件操作

1.下载文件
adb pull /sdcard/demo.mp4 

2.上传文件
adb push test.apk /sdcard

3.输入框输入
adb shell input text "ANDROID"

五、设备操作

1.查看所有已经连接上的设备
adb devices

2.关机命令
adb shell
su
reboot -p

3.重启
reboot