ADB: Android Debug Bridge
The ADB application is one of two applications used to system hack an Android device from your computer. It enables you to send / receive data directly from the Android OS to your computer. You can use to it copy files, install files and open a terminal. Since at that level Android is fundamentally Linux, it should be relatively easy to poke around in and learn something useful. ADB operates the same with physical and virtual Android devices, and it can support multiple concurrently connected devices. ADB is how the Android SDK and Developer Studio install / execute apps in development.
Issuing adb Commands
You can issue adb commands from a command line on your development machine or from a script. The usage is:
adb [-d|-e|-s <serialNumber>] <command>
When you issue a command, the program invokes an adb client. The client is not specifically associated with any emulator instance, so if multiple emulators/devices are running, you need to use the -d option to specify the target instance to which the command should be directed
Querying for Emulator/Device Instances
Before issuing adb commands, it is helpful to know what emulator/device instances are connected to the adb server. You can generate a list of attached emulators/devices using the devices command:
In response, adb prints this status information for each instance:
- Serial number — A string created by adb to uniquely identify an emulator/device instance by its console port number. The format of the serial number is
<type>-<consolePort>. Here’s an example serial number:
- State — The connection state of the instance may be one of the following:
offline— the instance is not connected to adb or is not responding.
device— the instance is now connected to the adb server. Note that this state does not imply that the Android system is fully booted and operational, since the instance connects to adb while the system is still booting. However, after boot-up, this is the normal operational state of an emulator/device instance.
no device— there is no emulator/device connected.
The output for each instance is formatted like this:
Here’s an example showing the devices command and its output:
$ adb devices List of devices attached emulator-5554 device emulator-5556 device emulator-5558 device
Directing Commands to a Specific Emulator/Device Instance
If multiple emulator/device instances are running, you need to specify a target instance when issuing adb commands. To so so, use the -s option in the commands. The usage for the -s option is:
adb -s <serialNumber> <command>
As shown, you specify the target instance for a command using its adb-assigned serial number. You can use the devicescommand to obtain the serial numbers of running emulator/device instances.
Here is an example:
adb -s emulator-5556 install helloWorld.apk
Note that, if you issue a command without specifying a target emulator/device instance using -s, adb generates an error.
Installing an Application
You can use adb to copy an application from your development computer and install it on an emulator/device instance. To do so, use the install command. With the command, you must specify the path to the .apk file that you want to install:
adb install <path_to_apk>
For more information about how to create an .apk file that you can install on an emulator/device instance, see Building and Running
Note that, if you are using the Eclipse IDE and have the ADT plugin installed, you do not need to use adb (or aapt) directly to install your application on the emulator/device. Instead, the ADT plugin handles the packaging and installation of the application for you.
||Issues a shell command in the target emulator/device instance and then exits the remote shell.|
Fastboot is the second highly necessary Android hackery tool, and it is installed as part of the Android SDK, and as part of Android Studio. Where ADB provides a direct terminal level interface into the Android OS, Fastboot provides an interface into the boot manager providing a way to update the OS partitions on the device. Updating to new ROMS, flashing alternative recovery partitions, or flashing your own ROM image are functions that require the fastboot interface / client.
Technically – Fastboot is the protocol used to update the flash filesystem in Android devices from a host over USB. It also allows flashing of unsigned partition images – useful for flashing any non-official ROM images.
Here are the commands you can run on your host after fastboot has been started on a device connected via USB. This is a paste directly from the fastboot client “–help” command:
usage: fastboot [ <option> ] <command> commands: update <filename> reflash device from update.zip flashall 'flash boot' + 'flash system' flash <partition> [<filename>] write a file to a flash partition erase <partition> erase a flash partition getvar <variable> display a bootloader variable boot <kernel> [ <ramdisk> ] download and boot kernel flash:raw boot <kernel> [<ramdisk>] create bootimage and flash it devices list all connected devices reboot reboot device normally reboot-bootloader reboot device into bootloader options: -w erase userdata and cache -s <serial number> specify device serial number -p <product> specify product name -c <cmdline> override kernel commandline
Booting into Fastboot
There is a handy table Google provides with the fastboot shortcuts for Nexus devices, which is duplicated here:
“During a cold boot, the following key combinations can be used to boot into fastboot mode, which is a mode in the bootloader that can be used to flash the devices:
|hammerhead||Press and hold both Volume Up and Volume Down, then press and hold Power|
|flo||Press and hold Volume Down, then press and hold Power|
|deb||Press and hold Volume Down, then press and hold Power|
|manta||Press and hold both Volume Up and Volume Down, then press and hold Power|
|mako||Press and hold Volume Down, then press and hold Power|
|grouper||Press and hold Volume Down, then press and hold Power|
|tilapia||Press and hold Volume Down, then press and hold Power|
|phantasm||Power the device, cover it with one hand after the LEDs light up and until they turn red|
|maguro||Press and hold both Volume Up and Volume Down, then press and hold Power|
|toro||Press and hold both Volume Up and Volume Down, then press and hold Power|
|toroplus||Press and hold both Volume Up and Volume Down, then press and hold Power|
|panda||Press and hold Input, then press Power|
|wingray||Press and hold Volume Down, then press and hold Power|
|crespo||Press and hold Volume Up, then press and hold Power|
|crespo4g||Press and hold Volume Up, then press and hold Power|
Also, the command
adb reboot bootloader can be used to reboot from Android directly into the bootloader with no key combinations.”
Of course for this to be useful, it is necessary to know the code name of your device. The most consistent and reliable source of this information is at the Nexus Firmware page, which provides a complete cross reference between commercial product name and Google Nexus codename.
Forcing fastboot to load on every boot
Developers who are creating new images to try out very often can remove their boot and recovery images which will force the phone to enter bootloader mode every time you boot. To fix this, you would reflash the boot and recovery images back allowing the phone to boot normally.
$ fastboot erase boot $ fastboot erase recovery