Article count:1382 Read by:1966155

Account Entry

Summary of commonly used adb commands

Latest update time:2023-06-27
    Reads:
Click on the blue " Linux " in the upper left corner and select " Set as Star "


1. Introduction to adb

adb: Android Debug Bridge, the abbreviation of Android Debug Bridge, adb is a command line tool with C/S architecture.

Mainly consists of 3 parts:

  1. Client running on PC: Android applications can be installed, uninstalled and debugged through it
  2. Service running on the PC: It manages the client's connection to the adb background process on the Android device
  3. adb background process running on Android devices

2. ADB installation

The SDK is already installed on your computer by default. If not, you can directly download an adb toolkit online.

Assume the directory where the adb tool is located on the PC:

H:\rxw\windows\adb_fastboot

Add the adb path to the environment variables (This PC → Properties → Advanced System Settings → Environment Variables)

Double-click Path, then click "New". The name can be chosen by yourself, and then fill in the path of the adb tool.

After adding and saving, you can enter the command to verify windows+R. Open cmd and enter adb. When the version number and menu appear, the configuration is completed.

C:\Users\Administrator
Android Debug Bridge version 1.0.41
Version 31.0.2-eng.lsh.20220519.123535
RockChip modified
Installed as H:\rxw\windows\adb_fastboot\adb.exe

global options:
 -a         listen on all network interfaces, not just localhost
 -d         use USB device (error if multiple devices connected)
 -e         use TCP/IP device (error if multiple TCP/IP devices available)
 -s SERIAL  use device with given serial (overrides $ANDROID_SERIAL)
            or devices devpath like:
            adb -s "\\?\usb#vid_2207&pid_0006#6&3795fe82&1&1#{f72fe0d4-cbcb-407d-8814-9ed673d0dd6b}" shell
            adb -s usb:3-1.2 shell
 -t ID      use device with given transport id
 -H         name of adb server host [default=localhost]
 -P         port of adb server [default=5037]
 -L SOCKET  listen on given socket for adb server [default=tcp:localhost:5037]

2. Syntax of adb command

The basic syntax of the adb command is as follows:

adb [-d|-e|-s <serialNumber>] <command>

If only one device/emulator is connected, you can omit [-d| -e|-s ] This part, use directly

adb <command>

Specify the target device for the command If you have multiple devices/emulators connected, you need to specify the target device for the command.

parameter meaning
-d Specify the only Android device currently connected via USB as the command target
-It is Specify the only currently running emulator as the command target
-s serialNumber> Specify the device/simulator with the corresponding serialNumber number as the command target

serialNumber can be obtained through adb devices command, such as:

C:\Users\Administrator>adb devices
List of devices attached
0123456789ABCDEF        device

For example, you want to specify the device 0123456789ABCDEF to run the adb command to obtain the screen resolution:

adb -s 0123456789ABCDEF shell wm size

3. Commonly used adb commands in android

Start adb service or stop adb service

  • adb kill-server: end adb service
  • adb start-server: When starting the adb service, there is usually an abnormality in the connection. When using adb devices, the device is not listed normally. When the device status is abnormal, use kill-server, and then run start-server to restart the service.

1) Device connection information

adb devices:获取设备信息

2) Install the application

adb install [-lrtsdg] <path_to_apk>

For example: adb install xxx.apk: install the application

parameter meaning
-r Allow override installation
-t Allow test packages (allow installation of applications that specify android:testOnly="true" in application in AndroidManifest.xml)
-d Allow downgrade overwrite installation
-g Grant all runtime permissions
-s Install app to sdcard
-l Install the application to the protected directory /mnt/asec

For example: adb -s device name install xxx.apk: multiple devices, install to the specified device (get the device name through adb devices)

One bite is commonly used:

adb -t install xxx.apk

3) Uninstall the application

adb uninstall [-k] <packagename>:卸载应用
  • The -k parameter is optional, indicating to uninstall the application but retain the data and cache directory
  • adb shell pm uninstall --user 0 package name: uninstall system applications
  • adb shell cmd package install-existing package name: After uninstalling the system application, restore the system application

4) Clear application data and cache

  • adb shell pm clear package name: clear application cache

The effect is equivalent to clicking "Clear Cache" and "Clear Data" on the application information interface in settings.

5) View the foreground Activity (view the class name of the current active window)

  • adb shell dumpsys window | findstr mCurrentFocus

6) Check the application installation path

adb shell pm path <PACKAGE>

like:

C:\Users\Administrator>adb shell pm path com.upuphone.push
package:/data/app/~~Mbj7C7djGMaqwuXZBfScGg==/com.upuphone.push-bIO4mliiakFsN-xk4IxnTA==/base.apk

7) View application list

Command format:

adb shell pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]
parameter show list
none All apps
-f Display the apk file associated with the application
-d Show only disabled applications
-It is Only show enabled applications
-s Show only system apps
-3 Show only third-party apps
-i Show application installer
-in Contains uninstalled apps
<FILTER> The package name contains the <FILTER> string

8) View application version information

adb shell dumpsys package <package_name> | findstr version

like:

C:\Users\Administrator>adb shell dumpsys package com.example.sdkdemo | findstr version
    versionCode=3 minSdk=29 targetSdk=32
    versionName=2.1
    signatures=PackageSignatures{d9064a9 version:2, signatures:[f4d74dfb], past signatures:[]}

If it is a system application, there will be 2 versions. The lower one is the version of the system app itself, and the upper one is the upgraded system app version, such as:

C:\Users\Administrator>adb shell dumpsys package com.android.settings | findstr version
    versionCode=30 minSdk=30 targetSdk=30
    versionName=11
    signatures=PackageSignatures{af44d47 version:3, signatures:[2bf9b00d], past signatures:[]}
    versionCode=30 minSdk=30 targetSdk=30
    versionName=11
    signatures=PackageSignatures{a29ef12 version:0, signatures:[], past signatures:[]}

9) Get log

Get the log command run by Android:

adb logcat <设备里的文件路径> [电脑上的目录]

Example 1: Clear all logcat logs

adb logcat -c

Example 2: Save all logcat logs to the local file peng.log

adb logcat -> peng.log

Press ctrl+c to stop saving

10) Copy the files in the device to the computer pull

Order:

adb pull <设备里的文件路径> [电脑上的目录]

The directory parameter on the computer can be omitted and will be copied to the current directory by default.

example:

adb pull /sdcard/sr.mp4 ~/tmp/

Tips: The file path on the device may require root permissions to access. If your device has been rooted, you can first use the adb shell and su commands to obtain root permissions in the adb shell, and then cp /path/on/device /sdcard /filename copies the file to sdcard, then adb pull /sdcard/filename /path/on/pc.

11) Copy files from the computer to the device for push

Order:

adb push <电脑上的文件路径> <设备里的目录>

Example: Copy the executable program modetest to the bin directory

adb root
adb remount
adb push ~/modetest /bin/

The first two commands are to obtain permissions

12) View device information

model

adb shell getprop ro.product.model

like:

C:\Users\Administrator>adb shell getprop ro.product.model
LE2123

battery power

adb shell dumpsys battery

like:

C:\Users\Administrator>adb shell dumpsys battery
Current Battery Service state:
  AC powered: false
  USB powered: true
  Wireless powered: false
  Max charging current: 4870000
  Max charging voltage: 9000000
  Charge counter: 2200000
  status: 5
  health: 2
  present: true
  level: 100
  scale: 100
  voltage: 4372
  temperature: 350
  technology: Li-ion

Among them, scale represents the maximum power, and level represents the current power. The output above indicates 100% battery remaining.

13) Screen resolution wm size

project Value
adb command illustrate
adb shell wm size View resolution
adb shell wm size 1920x1280 Modify resolution (lowercase x)
adb shell wm density 240 Modify dpi
adb shell wm density reset Reset density
adb shell wm size reset Reset resolution

Output example:

Physical size: 1080x1920

Indicates that the screen resolution of the device is 1080px * 1920px. If modified using the command, the output may be:

Physical size: 1080x1920
Override size: 480x1024

Indicates that the screen resolution of the device was originally 1080px * 1920px and is currently modified to 480px * 1024px.

14) Screen density wm density

project Value
adb command illustrate
adb shell wm density Check the dpi.
adb shell wm density 240 Modify dpi
adb shell wm density reset Reset density

Output example:

Physical density: 420

If modified using the command, the output may be:

Physical density: 480
Override density: 160

Indicates that the screen density of the device was originally 480dpi and is currently modified to 160dpi.

15)android_id

adb shell settings get secure android_id

like:

C:\Users\Administrator>adb shell settings get secure android_id
6596f32b63225bac

16)IMEI

How to obtain android11.0 (requires root permission)

adb shell
su
service call iphonesubinfo 1

like:

C:\Users\Administrator>adb shell
OnePlus9Pro:/ # su
su
/system/bin/sh: su: inaccessible or not found
127|OnePlus9Pro:/ # service call iphonesubinfo 1
service call iphonesubinfo 1
Result: Parcel(
  0x00000000: 00000000 0000000f 00300030 00300031 '........0.0.1.0.'
  0x00000010: 00380030 00340034 00360037 00310039 '0.8.4.4.7.6.9.1.'
  0x00000020: 00300032 00000037                   '2.0.7...        ')
OnePlus9Pro:/ #

Extract the valid content inside to get the IMEI. For example, here it is 001008447691207.

Exit adb shell method:

输入exit即可

17) Android system version

adb shell getprop ro.build.version.release

like:

C:\Users\Administrator>adb shell getprop ro.build.version.release
11

18) IP address

To find the IP address of the device, you have to go to "Settings" - "About Phone" - "Status Information" - "IP Address". Isn't it annoying? It can be easily viewed through adb.

adb shell ifconfig | grep Mask

like:

inet addr:10.130.245.230  Mask:255.255.255.252
inet addr:127.0.0.1  Mask:255.0.0.0

Then 10.130.245.230 is the device IP address.

This command does not output on some devices. If the device is connected to WiFi, you can use the following command to view the LAN IP:

adb shell ifconfig wlan0

like:

C:\Users\Administrator>adb shell ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr a6:44:63:ac:88:7e  Driver cnss_pci
          inet addr:10.20.1.158  Bcast:10.20.1.255  Mask:255.255.254.0
          inet6 addr: fe80::a444:63ff:feac:887e/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21765 errors:0 dropped:2 overruns:0 frame:0
          TX packets:11168 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3000
          RX bytes:6121620 TX bytes:3229281

inet addr:10.20.1.158 is the ip address.

19) Mac address

adb shell cat /sys/class/net/wlan0/address

like:

C:\Users\Administrator>adb shell cat /sys/class/net/wlan0/address
a6:44:63:ac:88:7e

This view is the LAN Mac address, mobile network or other connection information can be

adb shell netcfg 命令来查看

20)CPU information

adb shell cat /proc/cpuinfo

21) Memory information

adb shell cat /proc/meminfo

Among them, MemTotal is the total memory of the device, and MemFree is the current free memory.

22) Screencap screencap

adb exec-out screencap -p > sc.png

Among them, -p refers to saving the file format as png format, sc.png is the specified file name.

However, the actual measured picture cannot be opened. Reference link: link

The following methods can be used:

First take a screenshot and save it to your device:

adb shell screencap -p /sdcard/sc.png

Then export the png file to your computer:

adb pull /sdcard/sc.png

23) Record screen screenrecord

Record screen and save to /sdcard in mp4 format:

adb shell screenrecord /sdcard/filename.mp4

Press Ctrl+C when you need to stop. The default recording time and the maximum recording time are both 180 seconds.

If you need to export to your computer:

adb pull /sdcard/filename.mp4

Often used in communication between the development process and products or testing (when working in remote locations)

24) Set system date and time

Note: Root privileges are required.

adb shell
su
date -s 20230529.131500

Indicates changing the system date and time to 13:15:00 on May 29, 2023.

25) Check whether the device is rooted

adb shell
su

At this time, if the command line prompt is $, it means that you do not have root permissions, and if it is #, it means you are root.

26) Use Monkey for stress testing

Monkey can generate pseudo-random user events to simulate clicks, touches, gestures, etc., and can conduct random stress tests on programs under development.

Simple usage:

adb shell monkey -p <packagename> -v 500

Indicates sending 500 pseudo-random events to the specified application.

27) Simulate keystrokes/input

There is a very useful command in adb shell called input, through which you can do some interesting things.

adb shell input keyevent <keycode>

Different keycodes can achieve different functions

project Value
3 HOME button
4 return key
5 Open the dialer app
6 hang up the phone
24 increase volume
25 lower the volume
26 Power button
27 Take a photo (needs to be in the camera app)
82 menu
85 play / Pause
86 Stop playing
87 Play next song
88 Play previous song
122 Move the cursor to the beginning of the line or the top of the list
123 Move the cursor to the end of the line or the bottom of the list
126 Resume playback
127 Pause playback
164 mute
176 Open system settings
187 Switch app
207 Open contact
208 Open calendar
209 turn on music
210 open calculator
220 Reduce screen brightness
221 Increase screen brightness
223 System hibernation
224 light up screen
231 Open voice assistant
276 If there is no wakelock, let the system sleep

28) Network connection connect

In addition to connecting Android devices through USB, you can also connect through the network. First, make sure that the PC and the device are connected to the network, and then use the following command:

adb connect 192.168.10.111:5555
adb shell

This is Yiyijun’s new book, thank you all for your support!

end


A mouthful of Linux


Follow and reply [ 1024 ] Massive Linux information will be given away

Collection of wonderful articles


Article recommendation

【Album】 ARM
【Album】 Fans Q&A
Album Introduction to linux
Album Computer Network
Album Linux driver


Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号