"Go" Android common ADB Command summary

Source: Internet
Author: User



Original address: http://testerhome.com/topics/2565






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 adbdroot      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 ADB help 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

    [xuxu:~]$ adb deviceslist of devices attached 44c826a0    device  
  • ADB get-state, getting the status of the device

    [xuxu:~]$ adb get-state  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 , it is recommended to use the development version of  Mobile phone, Iuni is also good drop ~ ~

  • 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 sdcardfailed to copy ' Push.txt ' to ' sdcard ': is a directory

    Permissions issue with pull command

  • 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 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 =/systemexport classpath= $base/framewor K/monkey.jartrap "" Hupexec 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=/systemCLASSPATH=$base/framework/am.jar"[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[xuxu:...oid-20/com/android/commands]$ ll Total   0drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 amdrwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 bmgrdrwxr-xr-x  3 Xuxu  Staff   102B  4  2 10:57 budrwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 Contentdrwxr-xr-x  3 Xuxu  staff   102B  4  2 10:57 imedrwxr-xr-x  3 Xuxu   Staff 102B  4  2 10:57 inputdrwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 mediadrwxr-xr-x  3 Xuxu  Staff   102B  4  2 10:57 pmdrwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 Requestsyncdrwxr-xr-x  3 Xuxu  staff   102B  4  2 10:57 settingsdrwxr-xr-x  7 Xuxu  Staff   238B  4  2 10:57 svcdrwxr-xr-x  6 xuxu  staff   204B  4  2 10:57 Uiautomatordrwxr-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 zhihupackage:/data/app/com.zhihu.android-1.apk=com.zhihu.android  Installer=com.
  • PM Path lists the. apk location for the corresponding package name

    [xuxu:~]$ adb shell PM Path com.tencent.mobileqqpackage:/data/app/com.tencent.mobileqq-1.apk
  • 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



    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.apkresourcepath=/data/app/ com.tencent.mobileqq-1.apknativelibrarypath=/data/app-lib/com.tencent.mobileqq-1versioncode=242 targetSdk= 9VERSIONNAME=5.6.0APPLICATIONINFO=APPLICATIONINFO{43842CC8 com.tencent.mobileqq}flags=[HAS_CODE ALLOW_CLEAR_USER _data]datadir=/data/data/com.tencent.mobileqqsupportsscreens=[small, Medium, Large, XLarge, resizeable, anyDensity] Usesoptionallibraries:com.google.android.media.effectscom.motorola.hardware.frontcameratimestamp=2015-05-13 14:04:24firstinstalltime=2015-04-03 20:50:07lastupdatetime=2015-05-13 14:05:02installerpackagename= Com..marketsignatures=packagesignatures{4397f8d8 [43980488]}permissionsfixed=true haveGids=true installstatus=1pkgflags=[has_code allow_clear_user_data]user 0:installed=true blocked=false stopped=false Notlaunched=false Enabled=0grantedperMissions:android.permission.CHANGE_WIFI_MULTICAST_  STATEcom.tencent.qav.permission.broadcastcom.tencent.photos.permission.DATAcom.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 it with PM install

  • 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/. camerastarting:intent {cmp=com.android.camera/. Camera}

    Stop the target app before starting

    
    
    [xuxu:~]$ adb shell am start-s com.android.camera/. CameraStopping:com.android.cameraStarting: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/. camerastarting:intent {act=android.intent.action.main Cat=[android.intent.category.launcher] cmp= com.android.camera/. Camera}status:okactivity:com.android.camera/. Camerathistime:500totaltime:500complete

    Start a default browser to open a Web page

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

    Start Dialer 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}
  • Am instrument, start a instrumentation, unit test or Robotium will use

  • Am monitor, monitor crash and ANR

    [xuxu:~]$ adb shell am monitormonitoring Activity Manager  ... Available commands: (q) uit:finish monitoring** Activity Starting:com.android.camera
  • Am Force-stop, followed by package name, end application

  • Am StartService, start a service

  • AM broadcast, send a broadcast


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

Dump: Get control information, Dumpcommand.java


[xuxu:~]$ adb shell uiautomator dump   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



[xuxu:~]$ adb shell ime list-s                           com.google.android.inputmethod.pinyin/. pinyinimecom.baidu.input_mi/. Imeservice

List input methods on a device


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

Select Input Method

Wm


Wm.java



[xuxu:~]$ adb shell WM sizephysical size:1080x1920  

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


[xuxu:~]$ adb logcat-v time-s xuxu               ---------beginning Of/dev/log/system---------beginning of/dev/log/main05-15 1 3:57:10.286 D/xuxu    (12646): Test adb shell Log  
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.



So a few of the tools on GitHub, the core of which are the ADB commands, are the key to dealing with these commands in the language you've learned.



Looks like the content is a bit long.



"Go" 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.