preface

The Android Debug Bridge (ADB) is a common command line tool for Android. It helps a PC communicate with an emulator instance or connected Android device. It facilitates various device operations, such as installing and debugging applications, and provides access to the Unix shell.

Way to work

ADB is a client-server program consisting of three components:

  1. Client: This component sends commands that run on the development machine. The client can be invoked from the command line terminal by issuing ADB commands.
  2. Daemon: This component runs commands on the device. The daemon runs as a daemon on each emulator or device instance.
  3. Server: This component manages the communication between the client and the daemon. The server runs as a background process on the development machine.

ADB tools are usually added to environment variables in the SDK /platform-tools directory. When an ADB client is started, the client first checks to see if there are already running ADB server processes. If not, it starts the server process. When the server starts, it binds to the local TCP port 5037 and listens for commands sent from ADB clients – all ADB clients use port 5037 to communicate with the ADB server. The server then sets up connections to all running emulator/device instances. It looks for emulator/device instances by scanning odd-numbered ports between 5555 and 5585 (the range used by the emulator/device). Once the server discovers the ADB daemon, it sets up a connection to the port. Note that each simulator/device instance gets a sequential pair of ports, an even-numbered port for console connections and an odd-numbered port for ADB connections.

As shown above, the emulator instance connecting to ADB on port 5555 is the same as the console instance listening on port 5554. When the server has set up connections to all emulator instances, you can access them using ADB commands. Because the server manages the connection to the simulator/device instance and handles commands from multiple ADB clients, any simulator/device instance can be controlled from any client (or from a script).

Common commands

You can view the ADB command help via ADB Help or, of course, Google. This section describes only the commands that are frequently used in daily development and testing.

Connect related

// Outputs a list of all connected simulator/device instances.
// offline - The instance is not connected to ADB or is not responding.
// Device - The instance is now connected to the ADB server.
// No device -- The simulator/device is not connected.
adb devices
// Check whether the ADB server process is running and start it if not.
adb start-server
// Terminate the ADB server process.
adb kill-server
// Start the remote shell in the target emulator/device instance. For multiple devices, add -s Device name.
adb shell
Copy the code

Install the uninstall

/ / install apk
// Add "-s" for multiple devices
// Override installation with "-r"
adb install xxx.apk

// Uninstall the specified APP
// If you want to keep the cache file, add "-k"The adb uninstall the package nameCopy the code

Documents related to

// remote: destination file/directory on the emulator/device instance (remote)
// local: the path of the development computer's (local) target file/directory
// Copy files or directories (and their subdirectories) from the emulator or device
adb pull remote local
// Copy file files or directories (and their subdirectories) to the emulator or device
adb push local remote
Copy the code

Am related

In the ADB shell, you can use the Activity Manager (AM) tool to issue commands to perform various system operations, such as starting an Activity, forcibly stopping a process, broadcasting intents, modifying device screen properties, and more.

// In the shell environment, command is a shell command
am command
// Do not enter the shell
adb shell am command

/ / start the Activity
-a indicates action, -c indicates category, -d indicates data_uri, and -e indicates additional key-value information
// To open activities other than those started by default, add the Android: Exported ="true" attribute to the AndroidManifest file
adb shell am start package/package-activity
/ / start the Service
adb shell am startservice [options] intent
// Send a broadcast
adb shell am broadcast [options] intent
// Use Instrumentation instances to start monitoring. Normally, the target component is the form test_package/runner_class
adb shell am instrument [options] component
// Forcibly stop all applications associated with package
adb shell am force-stop package
// Terminate all processes associated with package (the application's package name). Terminate only processes that can be safely terminated without affecting the user experience.
adb shell am kill [options] package
Copy the code

For example:

// Start MainActivity in APP with package name com.zjx.sample.proguard
// Service is similar to this
adb shell am start com.zjx.sample.proguard/.MainActivity
// Simulates sending a broadcast with the screen open
adb shell am broadcast -a adnroid.intent.action.ACTION_SCREEN_ON
// Forcibly shut down the application
adb shell am force-stop com.zjx.sample.proguard
Copy the code

PM related

In adb shell, you can use the Package Manager (PM) tool to issue commands to manipulate and query application packages installed on the device.

Adb shell is not required if shell is used
(adb shell) pm command

// Outputs all packages, or only packages whose package names contain the text in filter
adb shell pm list packages [options] filter
// Prints the path of the APK for the given package
adb shell pm path package
// Delete all data associated with the package
adb shell pm clear package
Copy the code

For example:

// Lists the application package names that contain com.zjx
adb shell pm list packages com.zjx
// Output com.zjx.sample. proGuard application path
adb shell pm path com.zjx.sample.proguard
Copy the code

Take and record screenshots

Although there are a lot of software for capturing and recording screenshots, using commands is also very convenient and works well.

/ / screenshots
adb shell screencap filename

/ / screen
// Requirements: Android 4.4 (API level 19) or later
// This utility records the on-screen Activity to an MPEG-4 file
// Press Control + C to stop screen recording. Otherwise, recording will stop automatically when three minutes or --time-limit is set.
// Record at any supported resolution and desired bit rate while preserving the aspect ratio of the device's display screen
// Rotation of the screen while recording is not supported. If the screen rotates during recording, part of the screen recording will be cut off
adb shell screenrecord [options] filename
Copy the code

Dumpsys related

Dumpsys is rich in the ability to dump system data onto the screen. You can use ADB shell service list or ADB shell Dumpsys -l to check the services supported by Dumpsys. The commonly used services include: Activity, package, window, input, alarm, meminfo, etc.

// Query AMS service information
adb shell dumpsys activity 
// Query information about the WMS service
adb shell dumpsys window 
// Query CPU information
adb shell dumpsys cpuinfo 
// Query memory information
adb shell dumpsys meminfo
...
Copy the code

For example:

// Prints the Activity information of the com.zjx.sample. proGuard application
adb shell dumpsys activity com.zjx.sample.proguard
Copy the code

Logcat related

Logcat is a command line tool for dumping logs of system messages, including stack traces when devices throw errors and messages written from your application using the Log class.

[adb] logcat [<option>] ... [<filter-spec>] ...
Copy the code

Each Android log message has a tag and priority associated with it, and logs can be filtered. The priority is represented by one of the following character values (in order from lowest to highest priority) :

  • V — Detailed (lowest priority)
  • D – debugging
  • I – information
  • W – warning
  • E – error
  • F – the deadly
  • S — Silent (highest priority, no print)

Filter expressions follow tag:priority… This format, where tag represents the tag of interest and priority represents the lowest priority to report that tag.

adb logcat MainActivity:D *:S
Copy the code

This expression blocks all log messages except those marked “MainActivity” with a priority equal to or higher than “debug.” The last element *:S sets the priority of all tags to “silent,” ensuring that only log messages with the “MainActivity” tag are displayed.

Third-party tools

The ADB plug-in

An ADB Idea plugin can be added to Android Studio to make it easy to invoke ADB commands from the IDE.

logcat

Android Studio already provides the logcat display area. Here we recommend another logcat command line tool: PID Cat.

Connect to devices through WLAN

In addition to USB, ADB can also be used over WLAN, as described in the ADB documentation (this does not count as a third-party tool).

conclusion

ADB commands are very rich, and not just those mentioned above. Adb shell ls /system/bin shows the list of available commands. We don’t have to memorize these commands when we use ADB on a daily basis, as they become familiar over time. Of course, to use complex features, you still have to research, after all, the brain can’t remember that many details.

The resources

  1. The adb document developer.android.com/studio/comm…
  2. Developer.android.com/studio/comm dumpsys document…
  3. Logcat document developer.android.com/studio/comm…
  4. Android adb common commands to collect www.jianshu.com/p/85066854c…
  5. Android adb: blog.csdn.net/Next_Second…
  6. The ADB common commands wl9739. Making. IO / 2017/05/22 /…

Pay attention to wechat public number, the latest technology dry goods real-time push