Common commands for android adb

Keywords: Android shell Mobile Java

Reprint http://xuxu1988.com/2015/05/15/android-adb-commands 

For mobile Android testing, adb command is an important point. We must memorize the commonly used adb commands, which will bring great convenience to Android testing. Many of these commands will be used in scripts for automated testing.

Android Debug Bridge

adb is actually Android Debug Bridge, the abbreviation of Android Debug Bridge. adb is a command-line tool based on C/S architecture. It consists of three parts:

  • Client running on PC: It can install, uninstall and debug Android applications.
    ADT in Eclipse, DDMS in SDK Tools directory, Monitor and other tools, all use the function of adb to interact with Android devices.  
    PC-side mobile phone assistants, such as 360 mobile phone assistants, pea pods, app treasures, etc., can basically complete other functions through adb commands besides installing third-party applications. It is recommended that testers try not to install such mobile phone assistants on computers, because their own adb programs may conflict with the adb programs under Android SDK, 5037 The port is occupied, resulting in the failure to connect to the device when using the adb command
  • Service running on PC: Its management client's connection to adb background process on Android device

    After the start of the ADB service, Windows can find the adb.exe process in the task manager

  • adb background processes running on Android devices

    Execute adb shell ps | grep adbd to find the background process. windows please use findstr instead of grep

    [xuxu:~]$ adb shell ps | grep adbd
    root      23227 1     6672   832   ffffffff 00019bb4 S /sbin/adbd
    • 1
    • 2

    One thing to note here is the port number used by adb, 5037. It's necessary to note that.
    Next, I will divide the adb command into three parts: adb command, adb shell command and linux command.

adb command

In the process of development or testing, we can manage multiple devices through adb. Its general format is:

ADB [- e | - D | - s < device serial number >] < subcommand >
  • 1
  • 2

On the premise of matching environment variables, enter adb help in the command window or directly enter adb, which will list all options and subcommands.

Here are some common commands:

  • adb devices, get device list and device status

    [xuxu:~]$ adb devices
    List of devices attached 
    44c826a0    device  
    • 1
    • 2
    • 3
  • adb get-state, to get the state of the device

    [xuxu:~]$ adb get-state  
    device
    • 1
    • 2

    The state of the device is 3 minutes, device, offline, unknown
    Device: Normal device connection
    offline: The connection is abnormal and the device does not respond.
    unknown: no connection device

  • adb kill-server, adb start-server, end the adb service, start the adb service, usually two commands used together

    Generally, when the connection is abnormal, adb devices are not listed properly, kill-server is used when the device is abnormal, and then start-server is run to restart the service.

  • adb logcat, print Android system logs, which can be taken out separately

  • Adb bug report, which prints the output of dumpsys, dumpstate and logcat, is also used to analyze errors

    It is recommended to redirect to a file because of the large output.

    adb bugreport > d:\bugreport.log
    • 1
  • adb install, install application, override installation using the - r option

    If you need to install apk with Chinese name under windows, you need to modify the adb. Baidu can find the modified ADB and support the apk of Chinese command. Please search for it by yourself.

  • adb uninstall, uninstall application, followed by the parameter is the package name of the application, please distinguish it from the apk file name

    '-k'means keep the data and cache directories, -k option, save data and cache directories when uninstalled

  • adb pull, copy files or folders on Android devices to local
    For example, copy the pull.txt file under Sdcard to disk D:

    adb pull sdcard/pull.txt d:\
    • 1

    If you need to rename.txt:

    adb pull sdcard/pull.txt d:\rename.txt
    • 1

    Pay attention to permissions, copying files under the directory of system permissions requires root, and the general Android machine root can not use commands to copy, but need to use RE-like file browser on the mobile phone. First, the file system of the system is mounted as readable and written before copying mobile system files on the mobile phone. Here we recommend the development version of millet mobile phone. Ben, IUNI is also good.~~

  • adb push, pushing local files to Android devices
    For example, push.txt to Sdcard on the D disk:

    adb push d:\push.txt sdcard/
    • 1

    The slash behind the sdcard must not be less, otherwise the following mistakes will occur:

    [xuxu:~]$ adb push push.txt sdcard
    failed to copy 'push.txt' to 'sdcard': Is a directory
    • 1
    • 2

    The issue of permission is the same as pull command

  • Adb root, ADB remount, is only useful for mobile phones like millet development version. You can get root privileges directly from these two commands and mount the system file system in a readable and writable state.

  • adb reboot, restart Android device

    Bootloader, restart the device, enter fastboot mode, with the adb reboot-bootloader command
    Recovery, restart the device and enter recovery mode. Students who often brush the machine are familiar with this mode.

  • adb forward, redirects a port on the host to a port on the device

    adb forward tcp:1314 tcp :8888
    • 1

    After executing this command, all messages and data sent to port 1314 of the host will be forwarded to port 8888 of the Android device, so the Android device can be controlled remotely.

  • adb connect remotely connects to Android devices

    Mobile phone and PC are in the same network. Mobile phone root installs the application adbWireless and clicks the button in the middle of the interface after starting the application.

    Then run adb connect 192.168.1.102, which can connect the mobile phone through wireless mode. The disadvantage is that the speed is relatively slow.

adb shell command

Someone has asked me why I know so many commands. The answer is that I love tossing. Here you should first understand why I want to distinguish the adb command from the adb shell command.  
Simply put, the adb command is some command of the adb program, while the adb shell is the command of the Android system invoked. These Android-specific commands are placed in the system/bin directory of the Android device, for example, when I type such a command from the command line:

[xuxu:~]$ adb shell hehe
/system/bin/sh: hehe: not found
  • 1
  • 2

Obviously, this command does not exist in the bin directory.  
I love tossing around, want to see what commands, do not want to look for documents, so I started the simulator, copy the entire system/bin directory, and then try one by one. Embarrassed ~
Opening these files reveals that some of the commands are actually shell scripts, such as opening monkey files:

# Script to start "monkey" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/monkey.jar
trap "" HUP
exec app_process $base/bin com.android.commands.monkey.Monkey $*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Another example is to open am:

#!/system/bin/sh
#
# Script to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am "$@"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

There are also SDK sources/android-20/com/android/commands directories:

[xuxu:...oid-20/com/android/commands]$ pwd
/Users/xuxu/utils/android/android-sdk-macosx/sources/android-20/com/android/commands
[xuxu:...oid-20/com/android/commands]$ ll   
total 0
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 am
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 bmgr
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 bu
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 content
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 ime
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 input
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 media
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 pm
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 requestsync
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 settings
drwxr-xr-x  7 xuxu  staff   238B  4  2 10:57 svc
drwxr-xr-x  6 xuxu  staff   204B  4  2 10:57 uiautomator
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 wm
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Are there familiar commands? am, pm, uiautomator...

Here are some commonly used adb shell commands (pm, am commands are relatively large, using four-level headings)

pm

Package Manager, which can be used to obtain some application information installed on Android devices
Source code of pm Pm.java Running adb shell pm directly can get help information for this command

  • pm list package lists applications installed on equipment

    No options: List the package names of all applications (see here for those who don't know how to find the package names of applications)

    adb shell pm list package
    • 1

    - s: List system applications

    adb shell pm list package -s 
    • 1

    - 3: List third-party applications

    adb shell pm list package -3
    • 1

    - f: List application package names and corresponding apk names and storage locations

    adb shell pm list package -f
    • 1

    - i: List the application package name and its installation source. The results show examples:
    package:com.zhihu.android installer=com.xiaomi.market

    adb shell pm list package -i
    • 1

    FILTER is added at the end of the command: filtering keywords makes it easy to find the application you want.

    Parameter combinations, such as finding package names, apk storage locations, and installation sources known in tripartite applications:

    [xuxu:~]$ adb shell pm list package -f -3 -i zhihu
    package:/data/app/com.zhihu.android-1.apk=com.zhihu.android  installer=com.xiaomi.market
    • 1
    • 2
  • pm path lists the. apk location for the corresponding package name

    [xuxu:~]$ adb shell pm path com.tencent.mobileqq
    package:/data/app/com.tencent.mobileqq-1.apk
    • 1
    • 2
  • pm list instrumentation, listing applications containing unit test case s, followed by parameter-f (as in pm list package), and [TARGET-PACKAGE]

  • pm dump, followed by the package name, lists the dump information for the specified application, which contains various information for self-viewing

    adb shell pm dump com.tencent.mobileqq

    Packages:
    Package [com.tencent.mobileqq] (4397f810):
    userId=10091 gids=[3003, 3002, 3001, 1028, 1015]
    pkg=Package{43851660 com.tencent.mobileqq}
    codePath=/data/app/com.tencent.mobileqq-1.apk
    resourcePath=/data/app/com.tencent.mobileqq-1.apk
    nativeLibraryPath=/data/app-lib/com.tencent.mobileqq-1
    versionCode=242 targetSdk=9
    versionName=5.6.0
    applicationInfo=ApplicationInfo{43842cc8 com.tencent.mobileqq}
    flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
    dataDir=/data/data/com.tencent.mobileqq
    supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
    usesOptionalLibraries:
    com.google.android.media.effects
    com.motorola.hardware.frontcamera
    timeStamp=2015-05-13 14:04:24
    firstInstallTime=2015-04-03 20:50:07
    lastUpdateTime=2015-05-13 14:05:02
    installerPackageName=com.xiaomi.market
    signatures=PackageSignatures{4397f8d8 [43980488]}
    permissionsFixed=true haveGids=true installStatus=1
    pkgFlags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
    User 0:  installed=true blocked=false stopped=false notLaunched=false enabled=0
    grantedPermissions:
    android.permission.CHANGE_WIFI_MULTICAST_STATE
    com.tencent.qav.permission.broadcast
    com.tencent.photos.permission.DATA
    com.tencent.wifisdk.permission.disconnect
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • pm install, installation application

    Target apk is stored on PC, please install it with adb install.
    Target apk is stored on Android devices. Install it with pm install

  • pm uninstall, uninstall application, and adb uninstall, followed by parameters are the package name of the application

  • pm clear, clear application data

  • PM set-install-location, PM get-install-location, set application installation location, get application installation location

    [0/auto]: Default is automatic
    [1/internal]: Installed inside the phone by default
    [2/external]: Installed by default in external storage

am

Another big order...  
am source code Am.java

  • am start, start an Activity, start a system application as an example

    Start up camera

    [xuxu:~]$ adb shell am start -n com.android.camera/.Camera
    Starting: Intent { cmp=com.android.camera/.Camera }
    • 1
    • 2

    Stop the target application before starting

    [xuxu:~]$ adb shell am start -S com.android.camera/.Camera
    Stopping: com.android.camera
    Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]     cmp=com.android.camera/.Camera }
    • 1
    • 2
    • 3

    Waiting for the application to complete and start

    [xuxu:~]$ adb shell am start -W com.android.camera/.Camera
    Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }
    Status: ok
    Activity: com.android.camera/.Camera
    ThisTime: 500
    TotalTime: 500
    Complete
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Start the default browser and open a web page

    [xuxu:~]$ adb shell am start -a android.intent.action.VIEW -d http://testerhome.com
    Starting: Intent { act=android.intent.action.VIEW dat=http://testerhome.com }
    • 1
    • 2

    Start the dialer and dial 10086

    [xuxu:~]$ adb shell am start -a android.intent.action.CALL -d tel:10086            
    Starting: Intent { act=android.intent.action.CALL dat=tel:xxxxx }
    • 1
    • 2
  • am instrument, to start an instrumentation, unit testing or Robotium will use

  • am monitor, monitor crash and ANR



  • [xuxu:~]$ adb shell am monitor 
    Monitoring activity manager... available commands: 
    (q)uit: finish monitoring 
    ** Activity starting: com.android.camera 
  • Amforce-stop, followed by package name, terminates the application

  • am startservice, start a service

  • am broadcast, send a broadcast

There are many options to explore more by yourself.~~

input

This command can send keystroke events to Android devices, with their source code. Input.java

  • input text, send text content, cannot send Chinese

    adb shell input text test123456
    • 1

    The first step is to set the keyboard to English keyboard.

  • Input key event, which sends key events. KeyEvent.java

    adb shell input keyevent KEYCODE_HOME
    • 1

    Simulate pressing the Home key, the source code has a definition:
    public static final int KEYCODE_HOME = 3; 
    So you can replace KEYCODE_HOME in the command with 3

  • Intap, which sends a touch event to the screen

    adb shell input tap 500 500
    • 1

    Click on the coordinates on the screen at 500,500

  • input swipe, sliding event

    adb shell input swipe 900 500 100 500
    • 1

    Slide the screen from right to left
    If the version is no less than 4.4, you can simulate long press events

    adb shell input swipe 500 500 501 501 2000
    • 1

    In fact, in a small distance, in a longer period of time for sliding, the final result is the long press action.

You will find that Monkey Runner can do everything through the adb command. If it is encapsulated, it will do better than MR.

screencap

Screenshots command

adb shell screencap -p /sdcard/screen.png
  • 1

Screen capture, save to sdcard directory

screenrecord

4.4 New Recording Order

adb shell screenrecord sdcard/record.mp4
  • 1

Operate the phone after executing the command, ctrl + c ends recording, and the recording results are saved to sdcard

uiautomator

Execute UI automation tests to obtain control information for the current interface

runtest: executes UI automation tests RunTestCommand.java 
dumpsys: Get control information. DumpCommand.java

[xuxu:~]$ adb shell uiautomator dump   
UI hierchary dumped to: /storage/emulated/legacy/window_dump.xml
  • 1
  • 2

When the [file] option is not added, it is stored under sdcard by default.

ime

Typewriting, Ime.java

[xuxu:~]$ adb shell ime list -s                           
com.google.android.inputmethod.pinyin/.PinyinIME
com.baidu.input_mi/.ImeService
  • 1
  • 2
  • 3

List input methods on devices

[xuxu:~]$ adb shell ime set com.baidu.input_mi/.ImeService
Input method com.baidu.input_mi/.ImeService selected    
  • 1
  • 2

Selective Input Method

wm

Wm.java

[xuxu:~]$ adb shell wm size
Physical size: 1080x1920  
  • 1
  • 2

Obtaining device resolution

monkey

Please refer to Use of Android Monkey

settings

Settings.java Please refer to ____________. Explore the new settings command in Android 4.2

dumpsys

Please refer to The dumpsys command in android uses

log

This command is very interesting, you can print the information you set in the logcat, and think for your own specific purpose!

adb shell log -p d -t xuxu "test adb shell log"
  • 1

- p: Priority, - t: tag, tag, followed by message

[xuxu:~]$ adb logcat -v time -s xuxu               
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
05-15 13:57:10.286 D/xuxu    (12646): test adb shell log  
  • 1
  • 2
  • 3
  • 4

getprop

Viewing the parameter information of Android device, only running adb shell getprop, the result is displayed in the form of key: value key pair, if you want to get the value of a key:

adb shell getprop ro.build.version.sdk
  • 1

Get the sdk version of the device

linux command

Operate your Android device, commonly used commands, only listed, not detailed!

cat, cd, chmod, cp, date, df, du, grep, kill, ln, ls, lsof, netstat, ping, ps, rm, rmdir, top, touch, redirection symbol ">">", pipeline"|"

Some may need to use busybox. In addition, it is suggested to install Cygwin under windows. If you haven't used it, please Baidu Encyclopedia. Cygwin

END

Use of a quotation mark:
Scenario 1. Execute the monkey command on the PC side and save the information to the D disk monkey.log, which will read as follows:

adb shell monkey -p com.android.settings 5000 > d:\monkey.log
  • 1

Scenario 2. Execute the monkey command on the PC side and save the information to the SDCD of the mobile phone. It may be written as follows:

adb shell monkey -p com.android.settings 5000 > sdcard/monkey.log
  • 1

Errors are bound to be reported here, because they are ultimately written to the Sdcard directory of the current directory on the PC side, rather than to the Sdcard on the mobile phone.

A quotation mark is needed here:

adb shell "monkey -p com.android.settings 5000 > sdcard/monkey.log"
  • 1

After familiarizing with these commands, the next step is to synthesize the application of programming language and think about how to deal with these commands in language, so that these commands are more convenient for testing.

So personal  github  The core of these tools is adb commands. The key point is how to deal with these commands in the language you have learned.

Posted by Rayman3.tk on Wed, 15 May 2019 06:27:33 -0700