" Beware the barrenness of a busy life. "

Android Debug Bridge(ADB) Memo

1 What is adb ?

The Android Debug Bridge (ADB) is a tool to run commands on a connected Android device. The ADB client can start a background server to multiplex commands sent to devices. Users can simulate humans motion on android devices such as scrolling, click (tapping) , back to home screen, etc.

2 Install adb environment

  1. Download Android_SDK-platform_tools Code:922
  2. Copy location of adb.exe
  3. Add adb to System Variable
  4. Test if it works using cmd : adb version

    image-20200922202409517

3 Connect to Devices

3.1 Connect Android by USB

  1. Enable USB debugging in the devices setting
  2. Connect devices to PC using USB cables
  3. Test if connect successfully or not. (cmd: adb devices

3.2 Connect Android by Wi-Fi

  1. Prerequisite: Android devices and PC should be connected into same LAN(Local Area Network)
  2. Get Android devices’ IP Address under specific Network.
  3. cmd: adb connect 10.129.49.143

4 How to use adb

4.1 Basic Command

Function Command
Start adb server adb start-server
Stop adb server adb kill-server
Search devices status adb devices
Reboot/Restart device adb reboot

Status:

offline —— Connect failed;
device —— Connect successfully;
no device —— no devices;

Function Command
Install APP adb install “-l” “path_to_apk”

-l:将应用安装到保护目录 /mnt/asec;
-r:允许覆盖安装;
-t:允许安装 AndroidManifest.xml 里 application 指定 android:testOnly=“true” 的应用;
-s:将应用安装到 sdcard;
-d:允许降级覆盖安装;
-g:授予所有运行时权限;
path_to_apk: apk的绝对路径。

e.g. Taobao apk: adb install -l /data/local/tmp/taobao.apk

Function Command
uninstall APP adb uninstall -k “packagename”

“packagename”:表示应用的包名,以下相同;
-k 参数可选,表示卸载应用但保留数据和缓存目录。
e.g. :adb uninstall com.taobao.taobao

清除应用数据与缓存命令:adb shell pm clear “packagename”
相当于在设置里的应用信息界面点击「清除缓存」和「清除数据」。
e.g.:adb shell pm clear com.taobao.taobao 表示清除 手机淘宝数据和缓存。

Function Command
Press Button adb shell input keyevent keycode

keycode 含义
3 HOME Button
4 Back
5 Call
6 Hang Up
24 Volume Up
25 Volume Down
26 Power Button
27 Camera
61 Tab Button
64 Open Browser
67 Backspace
80 Camera Focus
82 Menu
85 Play/Pause
86 Stop Playing
92 Page Up
93 Page Down
111 ESC
112 Delete
122 Dpad Up
123 Dpad Down
124 Insert
164 Mute
176 Open System Setting
207 Open Contact
208 Open Calendar
209 Open Music
220 Lighter
221 Darker
223 Hibernate
224 点亮屏幕
231 打开语音助手
Function Command
swipe the screen adb shell input swipe 300 1000 300 500

Parameter 300 1000 300 500 respectively represent Initial X Initial Y Final X Final Y

Function Command
input text adb shell input text

4.2 Useful Function:

Function Command
Screenshot adb exec-out screencap -p > sc.png
pull out screenshot adb pull /sdcard/sc.png
Record screen and save to /sdcard (Max:180s) adb shell screenrecord /sdcard/filename.mp4
Stop Record screen Ctrl+C
Screenshot2 adb shell screencap -p /sdcard/screen.png'
pull out screen recording adb pull /sdcard/filename.mp4
Monkey Testing adb shell monkey -p < packagename > -v 500
Look Processes adb shell ps
Look Performance adb shell top
Look UID adb shell dumpsys package
Close USB debugging adb shell settings put global adb_enabled 0
Open USB debugging Manually open in [setting] – [developer] – [Android debugging]

挂载、查看连接过的 WiFi 密码、开启/关闭 WiFi、设置系统日期和时间都需要root权限
表示向指定的应用程序发送 500 个伪随机事件。

4.3 Screen Status and Setting:

Function Command
Screen Resolution adb shell wm size
Revise Resolution adb shell wm size 480x1024
Reset Resolution adb shell wm size reset
Screen Density adb shell wm density
Revise Density adb shell wm density 160
Reset Density adb shell wm density reset
Cover left/up/right/down edge with white pixels adb shell wm overscan 0,0,0,200
Reset Cover adb shell wm overscan reset
Screen Parameters adb shell dumpsys window displays

Resolution Output might be:

Physical size: 1080×1920 (Original size)
Override size: 480×1024 (Revised size)

DPI Output might be:
Physical density: 480 (Original DPI)
Override density: 160 (Revised DPI)

Screen Parameters Output might be:
WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)
Display: mDisplayId=0
init=1080×1920 420dpi cur=1080×1920 app=1080×1794 rng=1080×1017-1810×1731
deferred=false layoutNeeded=false

##mDisplayId is code of screen,init is initial resolution and DPI. app= means real height equals 1920-126(Bottom button pixels)

4.4 Devices/System Status:

Function Command
Devices Model adb shell getprop ro.product.model
Battery Status adb shell dumpsys battery
android_id adb shell settings get secure android_id
android version adb shell getprop ro.build.version.release
IP address adb shell ifconfig or adb shell netcfg
CPU info adb shell cat /proc/cpuinfo
Memory info adb shell cat /proc/cpuinfo
More properties adb shell cat /system/build.prop
Specific property adb shell getprop <property name>

property name Meaning
ro.build.version.sdk SDK Version
ro.build.version.release Android system Version
ro.product.model Model
ro.product.brand Brand
ro.product.name Device Name
ro.product.board Board Name
persist.sys.isUsbOtgEnabled Support/NOT Support OTG
ro.sf.lcd_density Screen Density
rro.build.version.security_patch Android security patch version

Function Command
Hide/unhide status/navigation panel adb shell settings put global policy_control + key + value
Reset panel adb shell settings put global policy_control null

key Meaning
immersive.full Hide both
immersive.status Hide status panel
immersive.navigation Hide navigation panel
immersive.preconfirms ?

这些键对应的值可则如下值用逗号组合:

value Meaning
apps ALL APP
* ALL interface
packagename Specific APP
-packagename Exclude specific APP

e.g. :adb shell settings put global policy_control immersive.full=* means hide both panels at all interfaces
e.g. :adb shell settings put global policy_control immersive.status=com.package1,com.package2:immersive.navigation=apps,-com.package3 means hide status panel in package1 and package 2; hide navigation panel in all APPs except package3.

5 EXAMPLES (python):

import os

def screen():
    os.system('adb shell screencap -p /sdcard/screen.png')
    os.system('adb pull /sdcard/screen.png')

screen()

import os,
from PIL import Image #pip install Pillow
#pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Pillow

def getDistance():
    image = Image.open(screen.png)

    height = image.size[0]
    width = image.size[1]
    print(height,width)

    for i in range(915,916):
        for j in range(0,height):
            if image.getpixel((i,j))[:3] == (254,119.146)
                print(j)
                #if we want to use 'return', we'd better use 'yield j'
                #生成器 惰性计算(只能运行一次)


    getDistance()

import os
import time

def screen():
    #scroll screen 
    #os.system('adb shell input swipe x1 y1 x2 y2')
    print('scrolling...')
    os.system('adb shell input swipe 1000 540 200 540')
    time.sleep(0.5)

    print('tapping...')
    #tap action
    os.system('adb shell input tap 1205 2305')
screen()


import os
import time
form PIL import Image

def screencap():
    os.system('adb shell screencap -p /sdcard/screen.png')
    os.system('adb pull /sdcard/screen.png')

def getDistance():
    image = Image.open(screen.png)

    height = image.size[0]
    width = image.size[1]
    print(height,width)

    for i in range(915,916):
        for j in range(0,height):
            if image.getpixel((i,j))[:3] == (254,119.146)
                print(j)
                #if we want to use 'return', we'd better use 'yield j'
                #生成器 惰性计算(只能运行一次)




if __name__ == '__main__':
    for _ in range(10):
        screencap()
        xPosition = getDistance()

        for x in xPosition:
            print(x)

Final:

import os
import time

from PIL import Image


def screencap():
    os.system('adb shell screencap -p /sdcard/screen.png')
    os.system('adb pull /sdcard/screen.png')


def getDistance():
    image = Image.open('screen.png')
    width = image.size[0]
    height = image.size[1]
    print(width, height)

    for i in range(1300, 1301):
        for j in range(0, height):
            if image.getpixel((i,j))[:3] == (254,129,154):
                yield j
                # if we want to use 'return', we'd better use 'yield j'
                # 生成器 惰性计算(只能运行一次)


if __name__ == '__main__':
    for _ in range(3):
        screencap()
        yPosition = getDistance()

        for y in yPosition:
            print(y)
            # tap action
            print('subscribing...')
            os.system('adb shell input tap 1243 {}'.format(y))

        print('scrolling...')
        os.system('adb shell input swipe 715 2100 715 450 500')
        time.sleep(0.5)



YOU MIGHT ALSO LIKE

0 0 vote
Article Rating
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x