paint-brush
Android Debug Bridge Fundamentals [A Beginner's Guide]by@budhdi-sharma
1,128 reads
1,128 reads

Android Debug Bridge Fundamentals [A Beginner's Guide]

by Budhdi SharmaJanuary 17th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Android Debug Bridge (ADB) is a versatile command-line tool that lets you communicate with a device. It’s a “bridge” for developers to work out bugs in their Android applications. ADB is used to keep track of all Android devices and emulators instances connected to or running on a given host developer machine. The adb command facilitates a variety of device actions, such as installing and. installing apps, and it provides access to a Unix shell that you can use to run a. variety of commands on a. device.

Coin Mentioned

Mention Thumbnail
featured image - Android Debug Bridge Fundamentals [A Beginner's Guide]
Budhdi Sharma HackerNoon profile picture

ADB or Android Debug Bridge is a term that you’ve come across quite often. Every time you connect your device to your computer via cable, or if you browse around the ‘Developer Options,’ you will come across the term Android Debug Bridge or ADB for short, but what does it mean? And what is its purpose? It is a versatile tool that helps your Android device function as it is, so let us tell you more about ADB.

The Android Debug Bridge (ADB) is used to:-

keep track of all Android devices and emulators instances connected to or running on a given host developer machine.implement various control commands (e.g. “adb shell”, “adb pull”, etc.) for the benefit of clients (command-line users, or helper programs like DDMS). These commands are called ‘services’ in ADB.

What Is adb?

Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device

This is the literal meaning of ADB. Though at first glance it doesn’t appear to tell you anything, it actually does. It’s a “bridge” for developers to work out bugs in their Android applications. This is done by connecting a device that runs the software through a PC and feeding its terminal commands. ADB lets you modify your device (or device’s software) via a PC command line.

So in short “Android Debug Bridge (ADB) is a versatile tool to let you manage the state of an emulator instance or Android-powered device.”

How Does adb Work?

When you start an adb client, the client first checks whether there is an adb server process already running. If there isn’t, it starts the server process. When the server starts, it binds to 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 devices. It locates emulators by scanning odd-numbered ports in the range 5555 to 5585, the range used by the first 16 emulators. Where the server finds an adb daemon (adbd), it sets up a connection to that port. Note that each emulator uses a pair of sequential ports — an even-numbered port for console connections and an odd-numbered port for adb connections. For example:

Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557
and so on…

As shown, the emulator connected to adb on port 5555 is the same as the emulator whose console listens on port 5554.

Once the server has set up connections to all devices, you can use adb commands to access those devices. Because the server manages connections to devices and handles commands from multiple adb clients, you can control any device from any client (or from a script).

adb Components

adb is an application that consists of four major components:

A client that sends commands to the connected Android device. This is the shell command adb that runs on your dev machine.A daemon (adbd) that runs as a background process on a device and handles the execution of received commands.A server that handles communication between the client and the daemon. The server runs as a background process on your dev machine and is started the first time you use the adb command.A service that runs in the background.

As a whole, everything works through the following components:

The ADB server:- This is a background process that runs on the host machine. Its purpose is to sense the USB ports to know when devices are attached/removed, as well as when emulator instances start/stop. It thus maintains a list of “connected devices” and assigns a ‘state’ to each one of them: OFFLINE, BOOTLOADER, RECOVERY or ONLINE (more on this below).

The ADB server is really one giant multiplexing loop whose purpose is to orchestrate the exchange of data (packets, really) between clients, services and devices.

The ADB daemon (adbd):- The ‘adbd’ program runs as a background process within an Android device or emulated system. Its purpose is to connect to the ADB server (through USB for devices, through TCP for emulators) and provide a few services for clients that run on the host.

The ADB server considers that a device is ONLINE when it has successfully connected to the adbd program within it. Otherwise, the device is OFFLINE, meaning that the ADB server detected a new device/emulator, but could not connect to the adbd daemon.

The BOOTLOADER and RECOVERY states correspond to alternate states of devices when they are in the bootloader or recovery mode.

The ADB command-line client:- The ‘adb’ command-line program is used to run adb commands from a shell or a script. It first tries to locate the ADB server on the host machine, and will start one automatically if none is found.

Then, the client sends its service requests to the ADB server. Currently, a single ‘adb’ binary is used for both the server and client. this makes distribution and starting the server easier.

Services:- There are essentially two kinds of services that a client can talk to.

1)Host Services: These services run within the ADB Server and thus do not need to communicate with a device at all. A typical example is “adb devices” which is used to return the list of currently known devices and their states. They are a few other services though.

2)Local Services: These services either run within the adbd daemon, or are started by it on the device. The ADB server is used to multiplex streams between the client and the service running in adbd. In this case its role is to initiate the connection, then of being a pass-through for the data.

adb Protocols

These components communicate via TCP/IP protocol. When you invoke the adb command, the client first checks to see if the adb server is already running. If not, the server is started. After starting, the server sets up connections to adbd daemons on all connected devices. Once the connection is started, you can use adb commands to control these devices.

adb uses two major protocols:

Client <-> Server protocol: This details the protocol used between ADB clients and the ADB server itself. The ADB server listens on TCP:localhost:5037.

A client sends a request using the following format:

A 4-byte hexadecimal string giving the length of the payloadFollowed by the payload itself.

For example, to query the ADB server for its internal version number, the client will do the following:

Connect to tcp:localhost:50372. Send the string “000Chost:version” to the corresponding socket

The ‘host:’ prefix is used to indicate that the request is addressed to the server itself (we will talk about other kinds of requests later). The content length is encoded in ASCII for easier debugging.

The server should answer a request with one of the following:

For success, the 4-byte “OKAY” string.For failure, the 4-byte “FAIL” string, followed by a 4-byte hex length, followed by a string giving the reason for failure.

Note that the connection is still alive after an OKAY, which allows the client to make other requests. But in certain cases, an OKAY will even change the state of the connection.

For example, the case of the ‘host:transport:<serialnumber>’ request, where ‘<serialnumber>’ is used to identify a given device/emulator; after the “OKAY” answer, all further requests made by the client will go directly to the corresponding adbd daemon.

The file SERVICES.TXT lists all services currently implemented by ADB.

2. Transports: An ADB transport model a connection between the ADB server and one device or emulator. There are currently two kinds of transports:

— USB transports, for physical devices through USB

— Local transports, for emulators running on the host, connected to the server through TCP

In theory, it should be possible to write a local transport that proxies a connection between an ADB server and a device/emulator connected to/ running on another machine. This hasn’t been done yet though.

Each transport can carry one or more multiplexed streams between clients and the device/emulator they point to. The ADB server must handle unexpected transport disconnections (e.g. when a device is physically unplugged) properly.

adb Installation

adb is included in the Android SDK Platform-Tools package, which you can download with the SDK Manager. You can find it in android_sdk_directory/platform-tools/ after installation.

Note: If you don’t need/want the entire Android SDK, you can also use the standalone SDK Platform Tools package to install adb.

Enable adb Debugging

To use ADB with your Android device, you must enable a feature called USB debugging. Open your phone’s app drawer, tap the Settings icon, and select “About Phone”. Scroll all the way down and tap the “Build Number” item seven times. You should get a message saying you are now a developer.

Head back to the main Settings page, and you should see a new option near the bottom called “Developer Options”. Open that, and enable “USB Debugging”.
Later on, when you connect your phone to your computer, you’ll see a popup entitled “Allow USB Debugging?” on your phone. Check the “Always allow from this computer” box and tap OK.

In this article, I have mentioned about adb fundamentals. But there are lot more about the adb. I will write some more articles on adb so to know about that please keep following.

I hope you enjoyed this introduction to ADB. If you have any comments or questions, please join the forum discussion below!

Thanks for the support :)