2019-04-28  2024-09-15    2023 字  10 分钟

What Is ADB

Android Debug Bridge (adb) is a command line tool that lets you communicate with an emulator or connected Android device. You can find the adb tool in android sdk/platform-tools or Download ADB Kits.

ADB Debugging

1adb devices
2adb forward
3adb kill-server

adb devices

Prints a list of all attached emulator/device

1adb devices

In response, return serial number and state

1e4b25377        device
2emulator-5554  device

adb forward

forward socket connections

1adb forward <local> <remote>

adb forward tcp:8000 tcp:9000 set up forwarding of host port 8000 to emulator/device port 9000

Prerequisites: Enable USB debugging on the device.

adb kill-server

terminates the adb server process

1adb kill-server

Notes: kill the server if it is running. (terminal adb.exe process)

Wireless

1adb connect <host>[:<port>]
2adb usb

adb connect

use ADB over Wi-Fi

1adb connect <host>[:<port>]

STEP 1.

Connect to the device over USB.

STEP 2.

1adb devices

List of devices attached ######## device

Notes: STEP 1,2 is required

STEP 3.

1adb tcpip 5555

restarting in TCP mode port: 5555

STEP 4.

Find out the IP address of the Android device: Settings -> About -> Status -> IP address. Remember the IP address, of the form #.#.#.#.

STEP 5.

1adb connect #.#.#.#

connected to #.#.#.#:5555

STEP 6.

Remove USB cable from device, and confirm you can still access device:

1adb devices

List of devices attached #.#.#.#:5555 device

Notes: Make sure that your host is still connected to the same Wi-Fi network your Android device is.

adb usb

restarting ADB in USB mode.

See also: adb connect

Package Manager

1adb install [option] <path>
2adb uninstall [options] <PACKAGE>
3adb shell pm list packages [options] <FILTER>
4adb shell pm path <PACKAGE>
5adb shell pm clear <PACKAGE>

adb install

Pushes an Android application (specified as a full path to an .apk file) to an emulator/device.

 1adb install test.apk
 2adb install -l test.apk
 3	# forward lock application
 4adb install -r test.apk
 5	# replace existing application
 6adb install -t test.apk
 7	# allow test packages
 8adb install -s test.apk
 9	# install application on sdcard
10adb install -d test.apk
11	# allow version code downgrade
12adb install -p test.apk
13	# partial application install

adb uninstall

Removes a package from the emulator/device.

1adb uninstall com.test.app
2adb uninstall -k com.test.app
3	# Keep the data and cache directories around after package removal.

adb shell pm list packages

Prints all packages, optionally only those whose package name contains the text in <FILTER>.

 1adb shell pm list packages
 2adb shell pm list packages -f
 3	# See their associated file.
 4adb shell pm list packages -d
 5	# Filter to only show disabled packages.
 6adb shell pm list packages -e
 7	# Filter to only show enabled packages.
 8adb shell pm list packages -s
 9	# Filter to only show system packages.
10adb shell pm list packages -3
11	# Filter to only show third party packages.
12adb shell pm list packages -i
13	# See the installer for the packages.
14adb shell pm list packages -u
15	# Also include uninstalled packages.
16adb shell pm list packages --user <USER_ID>
17	# The user space to query.

adb shell pm path

Print the path to the APK of the given <PACKAGE>.

1adb shell pm path com.android.phone

package:/system/priv-app/TeleService/TeleService.apk

adb shell pm clear

Deletes all data associated with a package.

1adb shell pm clear com.test.abc

Notes: clearing app data, cache

File Manager

 1adb pull <remote> [local]
 2adb push <local> <remote>
 3adb shell ls
 4adb shell cd
 5adb shell rm
 6adb shell mkdir
 7adb shell touch
 8adb shell pwd
 9adb shell cp
10adb shell mv

adb pull

Download a specified file from an emulator/device to your computer.

1adb pull /sdcard/demo.mp4

download /sdcard/demo.mp4 to <android-sdk-path>/platform-tools directory.

1adb pull /sdcard/demo.mp4 e:\

download /sdcard/demo.mp4 to drive E.

adb push

Upload a specified file from your computer to an emulator/device.

1adb push test.apk /sdcard

Copies <android-sdk-path>/platform-tools/test.apk to /sdcard directory.

1adb push d:\test.apk /sdcard

Copies d:\test.apk to /sdcard directory.

adb shell ls

list directory contents

1ls [options] <directory>

STEP 1.

1adb shell

STEP 2.

 1ls
 2ls -a
 3	# do not hide entries starting with
 4ls -i
 5	# print index number of each file
 6ls -s
 7	# print size of each file, in blocks
 8ls -n
 9	# list numeric UIDs and GIDs
10ls -R
11	# list subdirectories recursively

Notes: press Ctrl-C to stop

adb shell cd

change directory

1cd <directory>

STEP 1.

1adb shell

STEP 2.

1cd /system

adb shell rm

remove files or directories

1rm [options] <files or directory>

STEP 1.

1adb shell

STEP 2.

rm /sdcard/test.txt

1rm -f /sdcard/test.txt
2	# force remove without prompt
3rm -r /sdcard/tmp
4	# remove the contents of directories recursively
5rm -d /sdcard/tmp
6	# remove directory, even if it is a non-empty directory

Notes: rm -d equal rmdir command

1rm -i /sdcard/test.txt
2	# prompt before any removal

adb shell mkdir

make directories

1mkdir [options] <directory name>
1mkdir /sdcard/tmp
2mkdir -m 777 /sdcard/tmp
3	# set permission mode
4mkdir -p /sdcard/tmp/sub1/sub2
5	# create parent directories as needed

adb shell touch

create empty file or change file timestamps

1touch [options] <file>

STEP 1.

1adb shell

STEP 2.

1touch /sdcard/tmp/test.txt

ls /sdcard/tmp

adb shell pwd

print current working directory location.

1pwd

adb shell cp

copy files and directories

1cp [options] <source> <dest>

STEP 1.

1adb shell

STEP 2.

cp /sdcard/test.txt /sdcard/demo.txt

adb shell mv

move or rename files

1mv [options] <source> <dest>

STEP 1.

1adb shell

STEP 2.

1mv /sdcard/tmp /system/tmp 
2	# move
3mv /sdcard/tmp /sdcard/test
4	# rename

Network

1adb shell netstat
2adb shell ping
3adb shell netcfg
4adb shell ip

adb shell netstat

network statistics

1netstat

STEP 1.

1adb shell

STEP 2.

1netstat

adb shell ping

test the connection and latency between two network connection.

ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] [hop1 …] destination

STEP 1.

1adb shell

STEP 2.

1ping www.google.com

Notes: press Ctrl-C to stop ping

1ping www.google.com -c 4

adb shell netcfg

configure and manage network connections via profiles

1netcfg [<interface> {dhcp|up|down}]

STEP 1.

1adb shell

STEP 2.

1netcfg

adb shell ip

show, manipulate routing, devices, policy routing and tunnels

1ip [ OPTIONS ] OBJECT

OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |netns | l2tp }

OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |-f[amily] { inet | inet6 | ipx | dnet | link } |-l[oops] { maximum-addr-flush-attempts } |-o[neline] | -t[imestamp] | -b[atch] [filename] |-rc[vbuf] [size]}

STEP 1.

1adb shell

STEP 2.

1ip -f inet addr show wlan0 
2	# show WiFi IP Address

Logcat

1adb logcat
2adb shell dumpsys
3adb shell dumpstate

Prints log data to the screen.

1adb logcat [option] [filter-specs]
1adb logcat

Notes: press Ctrl-C to stop monitor

 1adb logcat *:V
 2	# lowest priority, filter to only show Verbose level
 3adb logcat *:D
 4	# filter to only show Debug level
 5adb logcat *:I
 6	# filter to only show Info level
 7adb logcat *:W
 8	# filter to only show Warning level
 9adb logcat *:E
10	# filter to only show Error level
11adb logcat *:F
12	# filter to only show Fatal level
13adb logcat *:S
14	# Silent, highest priority, on which nothing is ever printed
1adb logcat -b <Buffer>
 1adb logcat -b radio
 2	# View the buffer that contains radio/telephony related messages.
 3adb logcat -b event
 4	# View the buffer containing events-related messages.
 5adb logcat -b main
 6	# default
 7adb logcat -c
 8	# Clears the entire log and exits.
 9adb logcat -d
10	# Dumps the log to the screen and exits.
11adb logcat -f test.logs
12	# Writes log message output to test.logs .
13adb logcat -g
14	# Prints the size of the specified log buffer and exits.
15adb logcat -n <count>
16	# Sets the maximum number of rotated logs to <count>. 

Notes: The default value is 4. Requires the -r option.

1adb logcat -r <kbytes>
2	# Rotates the log file every <kbytes> of output.

Notes: The default value is 16. Requires the -f option.

1adb logcat -s
2	# Sets the default filter spec to silent.
1adb logcat -v <format>
 1adb logcat -v brief
 2	# Display priority/tag and PID of the process issuing the message (default format).
 3adb logcat -v process
 4	# Display PID only.)
 5adb logcat -v tag
 6	# Display the priority/tag only.
 7adb logcat -v raw
 8	# Display the raw log message, with no other metadata fields.
 9adb logcat -v time
10	# Display the date, invocation time, priority/tag, and PID of the process issuing the message.
11adb logcat -v threadtime
12	# Display the date, invocation time, priority, tag, and the PID and TID of the thread issuing the message.
13adb logcat -v long
14	# Display all metadata fields and separate messages with blank lines.

adb shell dumpsys

dumps system data

1adb shell dumpsys [options]
1adb shell dumpsys

adb shell dumpsys meminfo

1adb shell dumpsys battery

Notes: A mobile device with Developer Options enabled running Android 5.0 or higher.

1adb shell dumpsys batterystats
2	# collects battery data from your device

**Notes: Battery Historian converts that data into an HTML visualization. ** STEP 1 adb shell dumpsys batterystats > batterystats.txt STEP 2 python historian.py batterystats.txt > batterystats.html

1adb shell dumpsys batterystats --reset
2	# erases old collection data

adb shell dumpsys activity

adb shell dumpsys gfxinfo com.android.phone measuring com.android.phone ui performance

adb shell dumpstate

dumps state

1adb shell dumpstate
2adb shell dumpstate > state.logs
3	# dumps state to a file

Screenshot

1adb shell screencap
2adb shell screenrecord [4.4+]

adb shell screencap

taking a screenshot of a device display.

1adb shell screencap <filename>
1adb shell screencap /sdcard/screen.png

download the file from the device

1adb pull /sdcard/screen.png

adb shell screenrecord

recording the display of devices running Android 4.4 (API level 19) and higher.

1adb shell screenrecord [options] <filename>
1adb shell screenrecord /sdcard/demo.mp4

(press Ctrl-C to stop recording)

download the file from the device

1adb pull /sdcard/demo.mp4

Notes: Stop the screen recording by pressing Ctrl-C, otherwise the recording stops automatically at three minutes or the time limit set by –time-limit.

1adb shell screenrecord --size <WIDTHxHEIGHT>

Sets the video size: 1280x720. The default value is the device’s native display resolution (if supported), 1280x720 if not. For best results, use a size supported by your device’s Advanced Video Coding (AVC) encoder.

1adb shell screenrecord --bit-rate <RATE>

Sets the video bit rate for the video, in megabits per second. The default value is 4Mbps. You can increase the bit rate to improve video quality, but doing so results in larger movie files. The following example sets the recording bit rate to 5Mbps: adb shell screenrecord –bit-rate 5000000 /sdcard/demo.mp4

1adb shell screenrecord --time-limit <TIME>

Sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes).

1adb shell screenrecord --rotate

Rotates the output 90 degrees. This feature is experimental.

1adb shell screenrecord --verbose

Displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running.

System

1adb rootadb sideload
2adb shell ps
3adb shell top
4adb shell getprop
5adb shell setprop

adb root

restarts the adbd daemon with root permissions

1adb root

Notes: adbd cannot run as root in production builds (test in emulator)

adb sideload

flashing/restoring Android update.zip packages.

1adb sideload <update.zip>

Notes: adb reboot sideload [Android M+]

adb shell ps

print process status

1ps [options]

STEP 1.

1adb shell

STEP 2.

1ps

ps -p

adb shell top

display top CPU processes

1top [options]

STEP 1.

1adb shell

STEP 2.

1top

Notes: (press Ctrl-C to stop monitor)

top -t Show threads instead of processes.

adb shell getprop

get property via the android property service

1getprop [options]

STEP 1.

1adb shell

STEP 2.

1getprop
2getprop ro.build.version.sdk
3getprop ro.chipname
4getprop | grep adb

See Also adb shell setprop

adb shell setprop

set property service

1setprop <key> <value>

STEP 1.

1adb shell 

STEP 2.

setprop service.adb.tcp.port 5555

See Also adb shell getprop

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处