Appendix D. ADB: The Android Debug Bridge

In this book, we’ve focused on using an IDE for all your Android needs. But there are times when using a command-line tool can be plain useful, like those times when Android Studio can’t see your Android device but you just know it’s there. In this chapter, we’ll introduce you to the Android Debug Bridge (or adb), a command-line tool you can use to communicate with the emulator or Android devices.

adb: your command-line pal

Every time your development machine needs to talk to an Android device, whether it’s a real device connected with a USB cable, or a virtual device running in an emulator, it does so by using the Android Debug Bridge (adb). The adb is a process that’s controlled by a command that’s also called adb.

The adb command is stored in the platform-tools directory of the Android System Developer’s Kit on your computer. If you add the platform-tools directory to your PATH, you will be able to run adb from the command line.

In a terminal or at a command prompt, you can use it like this:

The adb devices command means “Tell me which Android devices you are connected to.” The adb command works by talking to an adb server process, which runs in the background. The adb server is sometimes called the adb dæmon or adbd. When you enter an adb command in a terminal, a request is sent to network port 5037 on your machine. The adbd listens for commands to come in on this port. When Android Studio wants to run an app, or check the log output, or do anything else that involves talking to an Android device, it will do it via port 5037.

When the adbd receives a command, it will forward it to a separate adbd process that’s running in the relevant Android device. This process will then be able to make changes to the Android device or return the requested information.

Sometimes, if the adb server isn’t running, the adb command will need to start it:

Likewise, if ever you plug in an Android device and Android Studio can’t see it, you can manually kill the adb server and restart it:

By killing and restarting the server, you force adb to get back in touch with any connected Android devices.

Running a shell

Most of the time you won’t use adb directly; you’ll let an IDE like Android Studio do the work for you. But there are times when it can be useful to go to the command line and interact with your devices directly.

One example is if you want to run a shell on your device:

The adb shell command will open up an interactive shell directly on the Android device. If you have more than one device attached, you can indicate which device you mean with the -s option, followed by the name given by the adb devices command. For example, adb-semulator-5554 shell will open a shell on the emulator.

Once you open a shell to your device, you can run a lot of the standard Linux commands:

Useful shell commands

If you open a shell to Android, you have access to a whole bunch of command line tools. Here are just a few:

CommandDescriptionExample (and what it does)

pm

Package management tool.

pm list packages (this lists all installed apps)

pm path com.hfad.bitzandpizzas (find where an app is installed)

pm –help (show other options)

ps

Process status.

ps (lists all processes and their IDs)

dexdump

Display details of an APK.

dexdump -d /data/app/com.hfad.bitzandpizzas-2/base.apk (disassemble an app)

lsof

List a process’s open files and other connections.

lsof -p 1234 (show what process with id 1234 is doing)

screencap

Take a screenshot.

screencap -p /sdcard/screenshot.png (save current screenshot to /sdcard/screenshot.png, and get it off the device with adb pull /sdcard/screenshot.png)

top

Show busiest processes.

top -m 5 (show the top five processes)

Each of these examples works from an interactive shell prompt, but you can also pass them direct to the shell command from your development machine. For example, this command will show the apps installed on your device:

Kill the adb server

Sometimes the connection can fail between your development machine and your device. If this happens, you can reset the connection by killing the adb server:

The next time you run an adb command, the server will restart and a fresh connection will be made.

Get the output from logcat

All of the apps running on your Android device send their output to a central stream called the logcat. You can see the live output from the logcat by running the adb logcat command:

The logcat output will keep streaming until you stop it. It can be useful to run adb logcat if you want to store the output in a file. The adb logcat command is used by Android Studio to produce the output you see in the Devices/logcat panel.

Copying files to/from your device

The adb pull and adb push commands can be used to transfer files back and forth. For example, here we are copying the /default.prop/ properties file into a local file called 1.txt:

And much, much more...

There are many, many commands that you can run using adb: you can back up and restore databases (very useful if you need to debug a problem with a database app), start the adb server on a different port, reboot machines, or just find out a lot of information about the running devices. To find out all the options available, just type adb on the command line:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.27.178