There is always a way to learn, or at least not to be ill-informed.

The full name of ADB is Android Debug Bridge, which makes it easy to Debug Android applications. As a student of mobile terminal development, it is very necessary to master the REQUIRED ADB operation commands, so I will sort out the ADB commands used in my daily work. (It is easier to use ADB action commands with shell aliases in everyday use)

ADB common commands

1.Start/stop adb server commands

Adb start-server // adb kill-server // Stop commandCopy the code

2. Use ADB to view device information

  1. Example Query connected devices or emulators

    adb devices
  2. Viewing the Phone Model

    adb shell getprop ro.product.model
  3. Check battery status

    adb shell dumpsys battery
  4. View screen Resolution

    adb shell wm size
  5. View screen density

    adb shell wm density
  6. Viewing Display Parameters

    adb shell dumpsys window displays
  7. View the Android version

    adb shell getprop ro.build.version.release
  8. Viewing CPU Information

    adb shell cat /proc/cpuinfo
  9. View the CPU architecture of the mobile phone

    adb shell getprop ro.product.cpu.abi
  10. Viewing Memory Information

    adb shell cat /proc/meminfo

3. Use ADB to connect devices

Adb [| | – e – d – s] if only one connection, equipment/simulator can omit [| | – e – d – s] part of the direct use of the adb. 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 through USB as the target of the command
-e Specifies the only currently running emulator as the command target
-s <serialNumber> Specify the device/emulator with the corresponding serialNumber as the command target
More commonly used when multiple devices/emulators are connected-sParameter, serialNumber can passadb devicesCommand to obtain. Such as:
$ adb devices
List of devices attached
cfxxxxxx	device
emulator-5554	device
10.xxx.xxx.x:5555	device
Copy the code

CFXXXXXX, emulator-5554 and 10.XXX.xxx. x:5555 in the output are serialNumber. For example, if you want to specify CFXXXXXX to run adb command to get screen resolution:

adb -s cfxxxxxx shell wm size
Copy the code

Install application:

adb -s cfxxxxxx install hello.apk
Copy the code

In the case of multiple devices/emulators, these parameters are used to specify the target device for the command.

4. Use ADB to operate applications on devices

  1. Install the APK

    adb install [-rtsdg] <apk_path>
    Copy the code

    Adb install can be followed by some optional parameters to control the behavior of APK installation. The available parameters and their meanings are as follows:

    parameter meaning
    -r Allow overwrite installation
    -t Allows the application specified in androidmanifest.xml to be installedandroid:testOnly="true"The application of
    -s Install the application to SDCard
    -d Allows degraded overwrite installations
    -g Grant all runtime permissions
  2. Uninstall the application

    adb uninstall [-k] <packagename>
    Copy the code

    indicates the name of the application package. The -k parameter is optional, indicating that the application is uninstalled but the data and cache directory are retained.

    adb uninstall com.vic.dynamicview
    Copy the code
  3. Forcibly Stopping an application

    adb shell am force-stop <packagename>
    Copy the code

    Command example:

    adb shell am force-stop com.vic.dynamicview
    Copy the code
  4. Start the corresponding Activity

    adb shell am start [options] <INTENT>
    Copy the code

    Such as:

    adb shell am start -n com.vic.dynamicview/.MainActivity --es "params" "hello, world"
    Copy the code

    Said tuning up com. Vic. Dynamicview /. MainActivity and string data key value to it for params – hello, world.

  5. View foreground Activities

    adb shell dumpsys activity activities | grep ResumedActivity
    Copy the code

    Adb shell Dumpsys Activity: Adb shell Dumpsys Activity

    ACTIVITY MANAGER PENDING INTENTS (adb shell dumpsys activity intents)
    ...
    ACTIVITY MANAGER BROADCAST STATE (adb shell dumpsys activity broadcasts)
    ...
    ACTIVITY MANAGER CONTENT PROVIDERS (adb shell dumpsys activity providers)
    ...
    ACTIVITY MANAGER SERVICES (adb shell dumpsys activity services)
    ...
    ACTIVITY MANAGER ACTIVITIES (adb shell dumpsys activity activities)
    ...
    ACTIVITY MANAGER RUNNING PROCESSES (adb shell dumpsys activity processes)
    ...
    Copy the code
  6. Open the system Settings: adb shell am start – n com. Android. Settings/com. Android. Settings. The Settings

  7. Open the developers options: adb shell am start – a com. Android. Settings. APPLICATION_DEVELOPMENT_SETTINGS

  8. Enter the WiFi setup adb shell am start – a android Settings. WIRELESS_SETTINGS

  9. Adb reboot The system

5. Use ADB operation logs

  1. Adb logcat –help adb logcat adb logcat adb logcat adb logcat adb logcat adb logcat adb logcat

  2. To output log information to a file, run adb logcat > log and run more log to view the log information. Adb logcat > ~/logdebug.log

  3. “-s” option: Set the default filter. If we want to output “system. out” tag information, we can use adb logcat -s system. out command.

  4. Clearing the log cache: Run the ADB logcat -c command to clear previous logs and output logs again.

  5. Run adb logcat -d to output the command and exit the command without blocking.

  6. Adb logcat -t 5 outputs the latest 5 lines of logs without blocking.

  7. Log filtering: Note: On Windows, you cannot use the grep keyword. You can use findstr instead of grep.

    • Filtering the fixed string: adb logcat | grep logtag adb logcat | grep -i logtag # ignore case. The adb logcat | grep logtag > ~ / result. The log # will filter the log output to file the adb logcat | grep — color = auto – I logtag # set the color matching string.

    • Use a regular expression match the adb logcat | grep “^.. Activity”

ADB Other commands

1. Clear application data and cache

adb shell pm clear <packagename>
Copy the code

indicates the application name package. This command is equivalent to clicking “Clear Cache” and “Clear Data” on the application information screen in Settings.

adb shell pm clear com.xxx.xxx
Copy the code

2. Interoperate with applications

The am command is used. The common commands are as follows:

command use
start [options] <INTENT> Start the<INTENT>The specified Activity
startservice [options] <INTENT> Start the<INTENT>The specified Service
broadcast [options] <INTENT> send<INTENT>Designated broadcast
force-stop <packagename> stop<packagename>Related processes

The

parameter is flexible and corresponds to the INTENT in your Android code.

The options for determining the intent object are as follows:

parameter meaning
-a <ACTION> Specify the action, for exampleandroid.intent.action.VIEW
-c <CATEGORY> Specify a category, for exampleandroid.intent.category.APP_CONTACTS
-n <COMPONENT> Specify the full Component name to explicitly specify which Activity to start, as incom.example.app/.ExampleActivity


can also carry data, just like a Bundle when writing code:

parameter meaning
--esn <EXTRA_KEY> Null value (only key name)
-e –es <EXTRA_KEY> <EXTRA_STRING_VALUE>`
--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> Boolean value
--ei <EXTRA_KEY> <EXTRA_INT_VALUE> An integer value
--el <EXTRA_KEY> <EXTRA_LONG_VALUE> Long value
--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> A float value
--eu <EXTRA_KEY> <EXTRA_URI_VALUE> URI
--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE> component name
--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...] An integer array
--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...] Long array
  1. Tuning up the Activity

    adb shell am start [options] <INTENT>
    Copy the code

    Such as:

    adb shell am start -n com.cc.test/.MainActivity --es "params" "hello, world"
    Copy the code

    Said tuning up com. Cc. The test /. MainActivity and string data key value to it for params – hello, world.

  2. Tuned up Service

    adb shell am startservice [options] <INTENT>
    Copy the code

    Such as:

    adb shell am startservice -n com.tencent.mm/.plugin.accountsync.model.AccountAuthenticatorService
    Copy the code
  3. Send broadcast

    adb shell am broadcast [options] <INTENT>
    Copy the code

    You can broadcast to all components or only to a specified component. For example, to broadcast BOOT_COMPLETED to all components:

    adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
    Copy the code

    Again, for example, only to com. Cc. The test /. BootCompletedReceiver radio BOOT_COMPLETED:

    adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.cc.test/.BootCompletedReceiver
    Copy the code
  4. Revoke the permissions of the application

    1. Grant permissions to applications. Only optional permissions declared by the application can be granted
    adb shell pm grant <packagename> <PACKAGE_PERMISSION>
    Copy the code

    For example, the adb – d shell PM grant packageName android. Permission. BATTERY_STATS

    1. Disabling Application Authorization
    adb shell pm revoke <packagename> <PACKAGE_PERMISSION>
    Copy the code

3. Simulate keystrokes/inputs

Usage: input [<source>] <command> [<arg>...]  The sources are: mouse keyboard joystick touchnavigation touchpad trackball stylus dpad gesture touchscreen gamepad The commands and default sources are: text <string> (Default: touchscreen) keyevent [--longpress] <key code number or name> ... (Default: keyboard) tap <x> <y> (Default: touchscreen) swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) press (Default: trackball) roll <dx> <dy> (Default: trackball)Copy the code

For example, simulate clicking: // On the screen, click x=50 and y=250.

adb shell input tap 50 250

Use ADB with shell aliases

Shell terminal aliases are simply shorthand commands that act like keyboard shortcuts. If you often follow a long command, give it a short alias. Use the alias command to list all defined aliases. . You can be in ~ / bashrc (.) ZSHRC file directly to define an alias as alias logRunActivity = “adb shell dumpsys activity activities | grep ‘Run*'”, you can also create a new file, such as. Byterc, and add source ~/. Byterc to the. Bashrc or. Using aliases can save time and increase productivity.

How do I add an alias

Add an alias in the form of a new file in the MAC environment as follows:

  1. new.bytercfile
    • If it is already created, open it

      open ~/.byterc
    • If no, create a new one and open it

      New:touch ~/.byterc

      Open:open ~/.byterc
  2. Add to.zshrcsource ~/.byterc
  3. In the open.bytercThe alias is defined in the file

    alias logRunActivity="adb shell dumpsys activity activities | grep 'Run*'"

    Android students should know that the function is to view the Activity information currently running on the device
  4. Reconfigure the source to take effect

    $ source ~/.byterc

    If you do not want to create a file, use it directly.bashrcor.zshrc, directly corresponding to the source configuration, such as:$ source ~/.zshrc .
  5. In this case, run the command directly on the command linelogRunActivityYou can view the Activity information of the current device.

Note: You can use $alias to see which alias actions are currently set.