Skip to content

Instantly share code, notes, and snippets.

@krazylearner
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save krazylearner/51225bca4de6aa52b115 to your computer and use it in GitHub Desktop.

Select an option

Save krazylearner/51225bca4de6aa52b115 to your computer and use it in GitHub Desktop.
Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:

Android Debug Bridge (adb) is a command line tool that lets you communicate with an emulator instance or connected Android-powered device. It is a client-server program that includes three components:


A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device. A daemon, which runs as a background process on each emulator or device instance. You can find the adb tool in /platform-tools/.

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. he server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires 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...

Syntax You can issue adb commands from a command line on your development machine or from a script. The usage is:

adb [-d|-e|-s ] If there's only one emulator running or only one device connected, the adb command is sent to that device by default. If multiple emulators are running and/or multiple devices are attached, you need to use the -d, -e, or -s option to specify the target device to which the command should be directed.

Commands

http://developer.android.com/tools/help/adb.html#commandsummary

Directing Commands to a Specific Emulator/Device Instance

If multiple emulator/device instances are running, you must specify a target instance when issuing adb commands. To do so, use the -s option in the commands.

adb -s <serialNumber> <command> 

you specify the target instance for a command using its adb-assigned serial number. You can use the 'devices' command to obtain the serial numbers of running emulator/device instances. For example:

adb -s emulator-5556 install helloWorld.apk

Note that, if you issue a command without specifying a target emulator/device instance while multiple devices are available, 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>

Issuing Shell Commands

Adb provides a Unix shell that you can use to run a variety of commands on an emulator or connected device. The command binaries are stored in the file system of the emulator or device, at /system/bin/...

Two of the most common command tools are activity manager (am) and package manager (pm).

You can use the shell command to issue commands, with or without entering the adb remote shell on the emulator/device. To issue a single command without entering a remote shell, use the shell command like this:

adb [-d|-e|-s <serialNumber>] shell <shell_command>

Or enter a remote shell on an emulator/device like this:

adb [-d|-e|-s <serialNumber>] shell

Using activity manager (am)

Within an adb shell, you can issue commands with the activity manager (am) tool to perform various system actions, such as start an activity, force-stop a process, broadcast an intent, modify the device screen properties, and more. While in a shell, the syntax is:

am <command>

You can also issue an activity manager command directly from adb without entering a remote shell. For example:

adb shell am start -a android.intent.action.VIEW

Using package manager (pm)

Within an adb shell, you can issue commands with the package manager (pm) tool to perform actions and queries on application packages installed on the device. While in a shell, the syntax is:

pm <command>

You can also issue a package manager command directly from adb without entering a remote shell. For example:

adb shell pm uninstall com.example.MyApp

UI/Application Exerciser Monkey

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events.

http://developer.android.com/tools/help/monkey.html

Other shell commands

For a list of all the available shell programs, use the following command:

adb shell ls /system/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment