跳到主要内容
版本:0.17.0+

ADB

overview

# show devices
adb start-server
adb devices

# stop device
adb kill-server

# restart system (warm reboot)
adb shell setprop ctl.restart.zygote

# start launcher (android 11, `.Launcher3` in android 10)
adb shell am start -n com.android.launcher3/.uioverrides.QuickstepLauncher

# start gallery
adb shell am start -n com.android.gallery3d/.app.GalleryActivity

connection relative

how to check available devices (especially via usb)

adb devices

how to get wifi from adb devices

# restart server first
adb tcpip 5555

# list first ip of device and connect
adb shell ifconfig | grep inet | head -n1 | awk '{print $2}' | awk -F : '{print $2}' | xargs adb connect

After connection over wifi, the usb cable can be plugged out then.

fast push

# init
adb root
adb disable-verity
adb remount
CROOT=/home/mark/work@arpara/rock-android11
PRODUCT=rk3399_ROCKPI4B_Android11
MODULE=surfaceflinger
USE_SYMBOL=1

OUT_DIR=$CROOT/out/target/product/$PRODUCT$($USE_SYMBOL?"/symbols":"")/system

adb push $OUT_DIR/bin/$MODULE /system/bin/
adb push $OUT_DIR/lib/lib$MODULE.so /system/lib/
adb push $OUT_DIR/lib64/lib$MODULE.so /system/lib64/

sys relative

get android/sdk(API) version

# get android version
adb shell getprop ro.build.version.release

# get sdk version
adb shell getprop ro.build.version.sdk

ref:

dumpsys

# list all the available services
adb shell dumpsys -l

# view the display service
adb shell dumpsys display

picture 44

ref

getprop | setprop

!!!warning the getprop | setprop is for global build props, not framework level!

For example, I defined a sysprop under surfaceflinger, but I can not get it if I only push `surfaceflinger | libsurfaceflinger.so` into device.
# see whole list of props
adb shell getprop

# see the value of specific prop
adb shell getprop ro.build.version.sdk

# ambiguous search of specific prop
adb shell getprop | grep timezone

see: - shell - Android ADB commands to get the device properties - Stack Overflow

TODO: sysprop(api)

log relative

config log level

先这样屏蔽下,logcat 的负载会降下来 logcat 屏蔽冗余日志输出

SERVICE=log.tag.JniUsbDevice

# 屏蔽JniUsbDevice TAG显示
adb shell setprop $SERVICE ERROR

# 恢复某TAG显示
adb shell setprop $SERVICE VERBOSE

ref

bugfix: log datetime

android 的日志系统的时间,是基于系统时间,对于刚刷的模拟机来说,系统里是默认自动获取(从网络),但由于刚刷的模拟机也没设置无线网,所以导致时间可能就比较随意了(目测可能就是 build 起的时间)。

在连接无线网后,会自动更新时间;再配置时区为中国上海,这个时候 adb 的日志时间就正常了。

picture 3

package relative

function getPackageInfoFromApkFile() {aapt dump badging $1}

# use `perl` for non-greedy match
function getPackageNameFromApkFile() {aapt dump badging $1 | grep package | perl -pe "s/^.*name='(.*?)'.*$/\1/"}

activity relative

get activity name from specific package

function getActivitiesFromPackageName() {adb shell "dumpsys package | grep -i $1 | grep 'Activity' | sort | uniq"}

see:

trigger an intent for choosing activity

adb shell am start -a android.intent.action.VIEW -d

ref:

play a video/movie/mp4

resolution 1:

adb push FILE /data/local/
FILE=/data/local/$FILE

# directly play the video using specific player
adb shell am start -n com.android.gallery3d/.app.MovieActivity -d $FILE

resolution 2:

# use intent to select which player
adb shell am start -a android.intent.action.VIEW -t video/mp4 -d $FILE

start/stop activity(package)

# start app(activity)
db shell am start -n PACKAGE/ACTIVITY

# kill app(package)
adb shell am force-stop PACKAGE

see: - (1 条消息) adb kill app 进程及启动 app 方法_蓝不蓝编程-CSDN 博客_adb kill 应用

TODO: kill app/process/package

# kill background process
adb shell am kill XXX

# kill all background processes
adb shell am kill-all

list all package names

adb shell pm list packages | cut -c 8-

sample:

picture 30

see: - android - Get Application Name/ Label via ADB Shell or Terminal - Stack Overflow


It may also be helpful if we use adb shell dumpsys package, while there are too much noise.

find current activity names

adb shell dumpsys window windows | grep "Window #"

picture 29

see: - android - Get Application Name/ Label via ADB Shell or Terminal - Stack Overflow

screen relative

change window size

adb shell wm size 1920x1080

adb shell wm density 390

adb reboot

ref:

screenshot/screencap

# gen screenshot
adb shell screencap /sdcard/screen.png

# pull screenshot
adb pull /sdcard/screen.png

ref

layer 相关

ref:

permission

官方文档:权限控制

adb 启用读写 SD 卡权限

adb shell pm grant PACKAGE android.permission.WRITE_EXTERNAL_STORAGE

adb shell pm grant PACKAGE android.permission.READ_EXTERNAL_STORAGE

ref:

tool

TODO: autoadb

edit file in adb shell

adb shell
busybox vi /sdcard/Download/test.txt

ref:

input

input show trackball

adb shell input trackball tap X Y

ref:

debug


# find all activities of specific package

# find launchable activities of specific package
adb shell pm dump PACKAGE_NAME | grep -A 1 MAIN

# find activities in current windows
adb shell dumpsys window windows | grep "Window #"


# kill app, sample: `adbkill com.android.systemui`
alias adbkill='_adbkill(){ adb root;adb shell kill `adb shell ps | grep $1 | awk {'\''print $2'\''} | head -n 1`; }; _adbkill'

ref: