adb in Android

Overview of adb in Android:


Three Elements of adb:

  • adb clients
    • executable with subcommand
    • ”adb shell”, ”adb logcat” : the end point of host side
  • adb server
    • running on host on back-ground
    • act as proxy between adb clients and adbd
  • adb daemon (adbd)
    • running on target device
    • started by init, if die, restarted by init again

Sequence chart:


When does adb server start?

  • Explicitly, “adb start-server”
    • It starts adb server as back ground process
  • Usually it does automatically on demand. You don’t have to do “adb start-server”
  • When you want to restart adb server, do “adb kill-server” && “sudo adb start-server
  • Actually, adb clients and adb server shares same executable
    • adb start-server” equals “adb fork-server server &”


ADB Source code:

  • system/core/adb in Android source tree
    • From this directory adb and adbd are built
  • common files between adb and adbd
    • adb.c, fdevent.c, transort.c, transport_local.c, tansport_usb.c, service.c, sockets.c, util.c
  • files only for adbd
    • backup_service.c, file_sync_service.c, jdwp_service.c, framebuffer_service.c, remount_services.c, usb_linux_clients.c, log_service.c
  • files only for adb
    • console.c, adb_clients.c, file_sync_client.c, usb_vendors.c, get_my_path_{linux,darwin,windows,freebsd}.c, usb_{linux,macos,libusb,windows}.c


USB Vendor ID:

  • When connecting USB device, adb checks USB Vendor ID
  • Many USB Vendor IDs are hard coded in adb. (But not enough)
  • To add USB Vendor ID, make ”$HOME/.android/adb_usb.ini” and write one ID in one line or add rules to the file /etc/udev/rules.d/99-adb.rules

For e.g.:

$ adb devices
List of devices attached 
4df15d6e02a55f15    device
????????????    no permissions

First check with lsusb:

$ lsusb
Bus 002 Device 050: ID 04e8:6860 Samsung Electronics Co., Ltd GT-I9100 Phone ...
Bus 002 Device 049: ID 18d1:4e42 Google Inc. 

Checking the permissions on those:

$ ls -l /dev/bus/usb/002/{049,050}
crw-rw-r--  1 root root    189, 176 Oct 10 10:09 /dev/bus/usb/002/049
crw-rw-r--+ 1 root plugdev 189, 177 Oct 10 10:12 /dev/bus/usb/002/050

Where did that “plugdev” group come from?

$ cd /lib/udev/rules.d/
$ grep -R "6860.*plugdev" .
./40-libgphoto2-2.rules:ATTRS{idVendor}=="0bb4", ATTRS{idProduct}=="6860", \
  ENV{ID_GPHOTO2}="1", ENV{GPHOTO2_DRIVER}="proprietary", \
  ENV{ID_MEDIA_PLAYER}="1", MODE="0664", GROUP="plugdev"
./40-libgphoto2-2.rules:ATTRS{idVendor}=="04e8", ATTRS{idProduct}=="6860", \
  ENV{ID_GPHOTO2}="1", ENV{GPHOTO2_DRIVER}="proprietary", \
  ENV{ID_MEDIA_PLAYER}="1", MODE="0664", GROUP="plugdev"

(I’ve wrapped those lines)

Note the GROUP="plugdev" lines. Also note that this doesn’t work for the other device ID:

$ grep -Ri "4e42.*plugdev" .

(nothing is returned)

Create a file /etc/udev/rules.d/99-adb.rules containing the following line:

ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e42", ENV{ID_GPHOTO2}="1",
  ENV{GPHOTO2_DRIVER}="proprietary", ENV{ID_MEDIA_PLAYER}="1",
  MODE="0664", GROUP="plugdev"

This should be a single line, I’ve wrapped it here for readability

Restart udev

$ sudo udevadm control --reload-rules
$ sudo service udev restart

That’s it

Unplug/replug your device.

Try it

$ adb devices
List of devices attached 
4df15d6e02a55f15    device
015d2109ce67fa0c    device


Switching transport mode: (USB->TCP)

Switching USB mode to TCP mode 

$ adb shell netcfg 

lo UP 0x00000049 

eth0 UP 0x00001043 

$ adb tcpip 5555 

restarting in TCP mode port : 5555 

$ adb devices 

List of devices attached 


disconnected from USB. Then restart adb server with specifying target IP address.

$ adb kill-server 

$ ADBHOST= adb devices 

* daemon not running. starting it now on port 5037 * 

* daemon started successfully * 

List of devices attached 

emulator-5554 device 



Other Case:

# adb devices
List of devices attached
4d00f9169907301b        unauthorized
  • Get the public key from the client phone (adb host)

    cat /data/.android/

  • copy the above public key to the target phone’s /data/misc/adb/adb_keys location. (you may need to stop the adb daemon first with stop adbd)

    cat /data/misc/adb/adb_keys

verify both cat outputs match.

try restarting adb daemon on target start adbd or just reboot them.

If you are having problems reading or writing to ADB KEYS in above steps, try setting environment variable ADB_KEYS_PATH with a temporary path (eg: /data/local/tmp). Refer to that link it goes intomore details

    "On the host, the user public/private key pair is automatically generated,
    if it does not exist, when the adb daemon starts and is stored in
    $HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
    the ADB_KEYS_PATH env variable may be set to a :-separated (; under
    Windows) list of private keys, e.g. company-wide or vendor keys.

    On the device, vendors public keys are installed at build time in
    /adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys"


adb from root to user mode:

If you used – “adb root”, you would have got the following message:

$ adb root
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
restarting adbd as root

to get out of the root mode, you can use:

$ adb unroot
restarting adbd as non root

adb kill-server and adb start-server only control the adb daemon on the PC side. You need to restart adbd daemon on the device itself after reverting the service.adb.root property change done by adb root:

~$ adb shell id
uid=2000(shell) gid=2000(shell)

~$ adb root
restarting adbd as root

~$ adb shell id
uid=0(root) gid=0(root)

~$ adb shell 'setprop service.adb.root 0; setprop ctl.restart adbd'

~$ adb shell id
uid=2000(shell) gid=2000(shell)


stop adbd && setprop service.adb.root 0 && start adbd &

This command will stop adbd, then setprop service.adb.root 0 if adbd has been successfully stopped, and finally restart adbd should the .root property have successfully been set to 0. And all this will be done in the background thanks to the last &.




Handy Adb Command for Android

View connected device(s)

Use this to view all connected devices and list their IDs.

adb devices

If multiple devices are attached, use adb -s DEVICE_ID to target a specific device.

Install an application

Use the install command to install an apk, the optional -r argument reinstalls and keeps any data if the application is already installed on the device.

adb install -r APK_FILE

# example
adb install -r ~/application.apk

Uninstall an application

adb uninstall PACKAGE_NAME

# example
adb uninstall com.growingwiththeweb.example

Start an activity


# example
adb shell am start -n com.growingwiththeweb.example/.MainActivity
adb shell am start -n com.growingwiththeweb.example/com.growingwiththeweb.example.MainActivity

Launch the default browser at a URL

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

# example
adb shell am start -a android.intent.action.VIEW -d

Entering the device’s shell

adb shell

Take a screenshot

Sergei Shvetsov came up with a nice one liner that takes a screenshot withshell screencap and outputs it to a local directory using perl. Checkout his blog for an explanation.

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

Power button

This command sends the power button event to turn the device on or off.

adb shell input keyevent 26

Unlock screen

This command sends the event that unlocks the lockscreen on the device. It can be combine with the power button command above to turn on and unlock the device.

adb shell input keyevent 82
adb shell pm list packages -f

Clear application data

adb shell pm clear PACKAGE_NAME

# example
adb shell pm clear com.growingwiththeweb.example


To show the log stream on your command line.

adb logcat

Filter by tagname

adb logcat -s TAG_NAME
adb logcat -s TAG_NAME_1 TAG_NAME_2

adb logcat -s TEST
adb logcat -s TEST MYAPP

Filter by priority

To show logs of a specific priority warning and above.

adb logcat "*:PRIORITY"

# example
adb logcat "*:W"

Here are the priority levels:

  • V – Verbose (lowest priority)
  • D – Debug
  • I – Info
  • W – Warning
  • E – Error
  • F – Fatal
  • S – Silent (highest priority, on which nothing is ever printed)

Filter by tagname and priority

adb logcat -s TAG_NAME:PRIORITY

adb logcat -s TEST: W

Filter using grep

Alternatively the output of logcat can be piped to grep on a system that supports it.

adb logcat | grep "SEARCH_TERM"
adb logcat | grep "SEARCH_TERM_1\|SEARCH_TERM_2"

adb logcat | grep "Exception"
adb logcat | grep "Exception\|Error"

Clearing the logcat buffer

Use this to clear the buffer to remove any old log data.

adb logcat -c

Further reading

See more details on the official adb reference site.


Original Link: