Android Common ADB Command summary

Source: Internet
Author: User
Tags system log



For mobile Android testing, the ADB command is a very important point, you must memorize the usual ADB commands in the heart, will be a great convenience for Android testing, many of the commands will be used in automated test scripts.


Android Debug Bridge


adb is actuallyAndroid Debug Bridge, the abbreviation of Android Debug Bridge, ADB is a c/S Architecture command-line tool, mainly consists of 3 parts:


    • Client running on PC: it can install, uninstall, and debug Android apps.


Tools such as ADT in Eclipse, DDMS in the SDK Tools directory, and Monitor are all equally used by ADB to interact with Android devices.

PC-side phone helper, such as 360 mobile phone helper, pea pod, app Bao, etc., in addition to the installation of third-party applications convenient, other functions, basically can be done through the ADB command, it is recommended to测试人员try not to install such mobile phone assistant on the computer, because its own ADB program may be with The ADB program under the Android SDK conflicts and the5037port is occupied, resulting in the inability to connect to the device when using the ADB command


    • Service running on the PC side: its management client connects to the ADB background process on the Android device


After the ADB service starts, Windows can find the Adb.exe process in Task Manager


    • The ADB background process running on Android devices


Executeadb shell ps | grep adbd, you can find the background process, Windows usefindstralternative grep


[xuxu:~]$ adb Shell PS | grep adbd
Root 23227 1 6672 832 ffffffff 00019bb4 s/sbin/adbd


Here is a place to note that the ADB uses the port number,5037it is necessary to remember

Next I'll divide the adb command into three parts,adb命令adb shell命令linux命令


ADB command


In the process of development or testing, we can manage multiple devices through the ADB in the general format of:



adb [-e |-d |-s< device serial number;] < sub-command >



With the environment variables in place, enter ADBHELP in the command window or enter ADB directly, and all the option descriptions and subcommands will be listed.



Here are some of the commands that are commonly used:


    • ADB devices, get device list and device status
    • ADB get-state, getting the status of the device
§[xuxu:~]$ ADB devices



Device


The state of the device is 3 minutes, and,deviceofflineunknown

Device: normal connection of devices

Offline: Connection unexpected, device not responding

Unknown: No device connected


    • ADB kill-server, adb start-server, end adb service, start adb service, usually two command


Generally in connection with an exception, use ADB devices not properly list the device, device status is abnormal when using kill-server, and then run Start-server to restart the service


    • ADB logcat, print the Android system log, this can be taken out in terms of
    • ADB bugreport, prints the output of Dumpsys, Dumpstate, Logcat, and is also used for parsing errors


More output, recommended redirection to a file


ADB bugreport > D:\bugreport.log
    • ADB install, installation application, overwrite installation is using the-r option


If you need to install the APK with Chinese name in Windows, you need to change the ADB, Baidu can find the ADB to make changes, support the Chinese command apk, please search by yourself


    • ADB Uninstall, uninstall the application, followed by the parameter is应用的包名, please distinguish fromapk文件名


'-K ' means keep the data and the cache directories,-k option, saving the database and caching directory when uninstalling


    • ADB pull to copy files or folders on your Android device to a local

      For example, copy the Pull.txt file under SDcard to the D drive:
ADB pull Sdcard/pull.txt d:\


If you need to rename to Rename.txt:


ADB pull Sdcard/pull.txt d:\rename.txt


Note the permissions, copy the system permissions in the directory of the file, requires root, and the general Android root can not use the command to copy, and need to use a similar to the RE-file browser on the phone, the system's file system is mounted to read and write, before the mobile system files on the phone copy 


    • ADB push, push local files to Android device

      For example, push the push.txt to SDcard under the D drive:
ADB push D:\push.txt sdcard/


There is no less slash behind the SDcard, or the following error will occur:


[xuxu:~]$ adb push push.txt sdcard
Failed to copy ' Push.txt ' to ' sdcard ': a directory


Permissions issue with pull command


    • ADB root, adb remount, is only useful for phones similar to the  development version, you can directly have these two commands to get root permissions and mount the system file system as read-write status
    • ADB reboot, restart Android device


bootloader, restart device, enter FastBoot mode, with ADB reboot-bootloader command

Recovery, restart the device, enter the recovery mode, often brush the students more familiar with this mode


    • ADB forward, redirecting a port on a host to a port on the device
ADB forward tcp:1314 tcp:8888


After executing this command, all messages sent to host port 1314 will be forwarded to the 8888 port on the Android device, so you can control the Android device remotely.


    • ADB connect remote Connect Android device


Mobile phone, PC is in the same network, phone root, install app Adbwireless, start the app and click the button in the middle of the interface:












Then runadb connect 192.168.1.102, you can connect the phone wirelessly, the disadvantage is that the speed is relatively slow


ADB shell command


Someone asked me why I know so many orders, the answer is I love toss, here first to understand why I want to distinguish between the ADB command and the adb shell command.
To put it simply, the ADB command is a command that comes with the ADB program, while the ADB shell is a command from the Android system that is called, and these commands are placed in the System/bin directory of the Android device, for example, I then command line to knock such a Command:


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


Obviously, this command does not exist in the bin directory.
I love tossing, want to see what commands, and do not want to find documents, so start the simulator, the entiresystem/bindirectory is copied out, and then one by one to try. Embarrassing ~

Opening these files will reveal that some of the commands are actually a shell script, such as opening the monkey file:


# Script to start ' monkey ' on the device, which have a very rudimentary
# shell.
#
Base=/system
Export Classpath= $base/framework/monkey.jar
Trap "" HUP
EXEC app_process $base/bin com.android.commands.monkey.Monkey $*


Another example of opening am:


#!/system/bin/sh
#
# Script to start ' AM ' on the device, which have a very rudimentary
# shell.
#
Base=/system
Export Classpath= $base/framework/am.jar
EXEC app_process $base/bin com.android.commands.am.Am "[Email protected]"


There is also the SDK sources/android-20/com/android/commands directory:


[xuxu:...oid-20/com/android/commands]$ pwd
/users/xuxu/utils/android/android-sdk-macosx/sources/android-20/com/android/commands

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


Are there any familiar commands? Am, PM, uiautomator ...



Here are some common adb shell commands (where PM, am commands are large and use level four headings)


Pm


Package Manager, which can be used to get some application information installed on Android devices

PM Source Pm.java, directly run adb shell Pm can get help information to this command


    • PM List package Lists the apps installed on the device


Without any options: List all the app's package names (don't know how to find the app's package name for the classmates to see here)


ADB Shell PM List Package


-S: List system applications




-3: List third-party apps


ADB shell PM List package-3


-F: List the app package name and the corresponding APK name and storage location


ADB shell PM List package-f


-I: Lists the app package names and their installation sources, showing examples of the results:


ADB shell PM List package-i


Command Add filter: Filter keyword, can easily find the application you want



parameter combinations, for example, to find知乎the package name in a three-party app, the APK storage location, the installation source:


[xuxu:~]$ adb shell pm list package-f -3-i Zhihu
Package:/data/app/com.zhihu.android-1.apk=com.zhihu.android Installer=com.
    • PM Path lists the. apk location for the corresponding package name
    • PM List instrumentation, listing applications with unit test case, followed by parameter-F (as in the PM list package), and [Target-package]
    • PM dump, followed by the package name, lists the dump information for the specified app, with a variety of information and self-viewing
§[xuxu:~]$ adb shell pm Path com.tencent.mobileqq
package:/data/app/com.tencent.mobileqq-1.apk


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

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
    • PM install, installing the app


The target apk is stored on the PC side, please install it with ADB installation

Target apk is stored on Android device, please install with Pminstall


    • PM Uninstall, uninstall the application, with ADB uninstall, followed by the parameters are the package name of the app
    • PM clear, Erase app data
    • PM Set-install-location, PM get-install-location, set app install location, get app install location


[0/auto]: Default is Auto

[1/internal]: default is installed inside the phone

[2/external]: Default installation on external storage


Am


Again a huge order ...

AM Source Am.java


    • Am Start, start an Activity, started the system camera application as an example


Start the camera


[xuxu:~]$ adb shell am start-n com.android.camera/. Camera
starting:intent {cmp=com.android.camera/. Camera}


Stop the target app 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}


Wait for the app to finish booting


[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


Start a default browser to 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}


Start Dialer Dial 10086



starting:intent {Act=android.intent.action.call Dat=tel:xxxxx}
    • Am instrument, start a instrumentation, unit test or Robotium will use
    • Am monitor, monitor crash and ANR
    • Am Force-stop, followed by package name, end application
    • Am StartService, start a service
    • AM broadcast, send a broadcast
§[xuxu:~]$ adb shell am monitor
§monitoring Activity Manager ... available commands:
§ (q) uit:finish monitoring
* * Activity Starting:com.android.camera


There are a lot of options, self-discovery ~ ~


Input


This command can send key events to the Android device, and its source Input.java


    • Input text, send textual content, cannot send Chinese
adb shell input Text test123456


If you first set the keyboard to an English keyboard


    • Input keyevent, Send key event, Keyevent.java
ADB shell input KeyEvent keycode_home


Analog Press the Home button, the source code is defined:

public static final int keycode_home = 3;

Therefore, you can replace the command with theKEYCODE_HOME3


    • Input tap, sends a touch event to the screen
ADB shell input TAP 500 500


Click on the screen where coordinates are 500 500


    • Input swipe, sliding event
ADB shell input swipe 900 500 100 500


Swipe the screen from right to left

If the version is not less than 4.4, you can simulate long-press events


ADB shell input swipe 500 500 501 501 2000


In fact, in a small distance, in a longer duration of the slide, and finally show the result is the long press action



Here you will find that what Monkeyrunner can do, through the ADB command, can be done, if encapsulated, will do better than MR.


Screencap


Command


ADB Shell Screencap-p/sdcard/screen.png


Screenshot, save to SDcard directory


Screenrecord


4.4 New Recording Commands


ADB Shell Screenrecord Sdcard/record.mp4


Action phone after command, CTRL + C end recording, record results saved to SDcard


Uiautomator


Executes UI automation tests to get control information for the current interface



Runtest:executes UI Automation tests Runtestcommand.java

Dumpsys: Get control information, Dumpcommand.java



UI Hierchary dumped to:/storage/emulated/legacy/window_dump.xml


When the [file] option is not added, the default is stored under SDcard


Ime


Input method, Ime.java



com.google.android.inputmethod.pinyin/. Pinyinime
com.baidu.input_mi/. Imeservice


List input methods on a device


[xuxu:~]$ adb shell ime set com.baidu.input_mi/. Imeservice


Select Input Method


Wm


Wm.java


[xuxu:~]$ adb shell WM size


Get Device resolution


Monkey


Please refer to the usage of Android Monkey


Settings


Settings.java, please refer to the new Settings command in Android4.2


Dumpsys


Please refer to the android Dumpsys command using the


Log


This command is very interesting, you can print the information you set in the Logcat, the specific use of their own thinking!


adb shell log-p d-t xuxu "test adb shell Log"


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



---------beginning Of/dev/log/system
---------beginning Of/dev/log/main

Getprop


View the parameter information of the Android device, run only, and the result is displayed as aadb shell getpropkey : valuekey-value pair, such as to get the value of a key:


ADB Shell Getprop RO.BUILD.VERSION.SDK


Get the SDK version of the device


Linux commands


Operate your Android device, common to the commands, list only, unknown solution!



Cat, CD, chmod, CP, date, DF, Du, grep, kill, LN, LS, lsof, netstat, ping, PS, RM, rmdir, top, touch, redirect symbol > >>, pipe |



Some may need to use BusyBox, it is recommended to install one under WindowsCygwin, do not use Baidu encyclopedia Cygwin


END


To supplement the use of a quotation mark:
Scenario 1, execute the Monkey command on the PC and save the information to the D-disk monkey.log, which reads:


adb shell monkey-p com.android.settings > D:\monkey.log


Scenario 2, execute the Monkey command on the PC side, save the information to the phone's sdcard, which may be written like this:


adb shell monkey-p com.android.settings > Sdcard/monkey.log


There will be an error here, because eventually it is written to the SDcard directory of the current directory of the PC, not to the sdcard of the phone.



You need to use the quotation marks here:


adb shell "monkey-p com.android.settings > Sdcard/monkey.log"


After all these commands are familiar, then the next step is to synthesize the application of the programming language and think about how to use the language to handle these commands, making these commands more convenient for testing work.



Android Common ADB Command summary


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.