Skip to content

Month: August 2017

ADB(Android Debug Bridge)

ADB:

Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an emulator instance or connected Android device. It facilitates a variety of device actions, such as installing and debugging apps, and it provides access a Unix shell that you can use to run a variety of commands on an emulator or connected device. It is a client-server program that includes three components:

  • A client, which sends commands. The client runs on your development machine. You can invoke a client from a command-line terminal by issuing an adb command.
  • A daemon, which runs commands on a device. The daemon runs as a background process on each emulator or device instance.
  • A server, which manages communication between the client and the daemon. The server runs as a background process on your development machine.

adb is included in the Android SDK Platform-Tools package. You can download this package with the SDK Manager, which installs it at android_sdk/platform-tools/. Or if you want the standalone Android SDK Platform-Tools package, you can download it here.

How adb works –

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 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…

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

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

Enable adb debugging on your device –

To use adb with a device connected over USB, you must enable USB debugging in the device system settings, under Developer options.

On Android 4.2 and higher, the Developer options screen is hidden by default. To make it visible, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options at the bottom.

On some devices, the Developer options screen may be located or named differently.

You can now connect your device with USB. You can verify that your device is connected by executing adb devices from the android_sdk/platform-tools/ directory. If connected, you’ll see the device name listed as a “device.”

Note: When you connect a device running Android 4.2.2 or higher, the system shows a dialog asking whether to accept an RSA key that allows debugging through this computer. This security mechanism protects user devices because it ensures that USB debugging and other adb commands cannot be executed unless you’re able to unlock the device and acknowledge the dialog.

For more information about connecting to a device over USB, read Run Apps on a Hardware Device.

Connect to a device over Wi-Fi –

adb is usually used over USB. However, it is also possible to use over Wi-Fi, as described here.

  1. Connect your Android device and adb host computer to a common Wi-Fi network accessible to both. Beware that not all access points are suitable; you may need to use an access point whose firewall is configured properly to support adb.

Note: If you are attempting to connect to an Android Wear device, force it to connect to Wi-Fi by shutting off Bluetooth on the phone connected to it.

  1. Connect the device to the host computer with a USB cable.
  2. Set the target device to listen for a TCP/IP connection on port 5555.
  3. $ adb tcpip 5555
  4. Disconnect the USB cable from the target device.
  5. Find the IP address of the Android device. For example, on a Nexus device, you can find the IP address at SettingsAbout tablet (or About phone) > Status > IP address. Or, on an Android Wear device, you can find the IP address at Settings > Wi-Fi Settings > Advanced > IP address.
  6. Connect to the device, identifying it by IP address.
  7. $ adb connect device_ip_address
  8. Confirm that your host computer is connected to the target device:
  9. $ adb devices
  10. List of devices attached
  11. device_ip_address:5555 device

You’re now good to go!

If the adb connection is ever lost:

  1. Make sure that your host is still connected to the same Wi-Fi network your Android device is.
  2. Reconnect by executing the adb connectstep again.
  3. Or if that doesn’t work, reset your adb host:
  4. adb kill-server

Then start over from the beginning.

 

Below are some of the adb commands:

  1. adb root – This shows if the connected device is running as root or no. If not it will display as cannot run in production builds.
  2. adb reboot sideload <update.zip> – ADB sideload is a different ADB mode that you can use to push and install a zip using one command from your computer. Most likely ADB sideload won’t be very useful for your average recovery user, but ADB sideload can be a huge time-saver for a ROM developer.
  3. adb shell ps [options] – Prints the process status.

Step by step:

Enter adb shell command while a device (or emulator) is connected.
(Command line prefix will be shell@android:/ $ after executing this command.)

Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google)

C:>adb shell
shell@android:/ $ ps | grep com.google
ps | grep com.google
u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps

  1. adb shell top -m 10 | grep<packagename> – This top command is refreshing periodically, so one can get CPU usage through various stages of an app.
  2. adb shell getprop [options] > <filename>.txt – Gets the properties via the android property service or so called as build.prop file.
  3. adb shell setprop <key> <value> – Sets the property value for the given key.

Example: ro.product.device will be the key and you can change it by giving any value. Restart the device and you are good to go.

  1. adb shell screencap /sdcard/<filename>.png – This will capture the current screen and saves in to the file system of android. To save the screenshot taken to the system use the command adb pull /sdcard/<filename>.png
  2. adb shell screenrecord /sdcard/<filename>.mp4 – This command is used to record the screen.

 

To download the file from the device use the below command –

adb pull /sdcard/<filename>.mp4

Note: To stop the screen recording, press Ctrl-C, otherwise the recording stops automatically at three minutes or the time limit set by –time-limit.

adb shell screenrecord –size 1280*720 /sdcard/<filename>.mp4

The above command sets the video size to 1280×720.

adb shell screenrecord –bit-rate 6000000 /sdcard/<filename>.mp4

The above command sets the video bit rate for the video in 6Mbps. The default value is 4Mbps. You can increase the bit rate to improve video quality, but doing so results in larger video files.

adb shell screenrecord –time-limit <TIME>

The above command sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes).

adb shell screenrecord –rotate

The above command rotates the output to 90 degrees. This feature is experimental.

adb shell screenrecord –verbose

The above command displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running.

 

  1. adb logcat > <filename>.txt – Saves logs to a text file.

9a.adb logcat -v <format>

adb logcat -v brief Display priority/tag and PID of the process issuing the message (default format).adb logcat -v process Display PID only.)adb logcat -v tag Display the priority/tag only.adb logcat -v raw Display the raw log message, with no other metadata fields.adb logcat -v time Display the date, invocation time, priority/tag, and PID of the process issuing the message.adb logcat -v threadtime Display the date, invocation time, priority, tag, and the PID and TID of the thread issuing the message.adb logcat -v long Display all metadata fields and separate messages with blank lin

9b. adb logcat –b <buffer>

adb logcat -b radio View the buffer that contains radio/telephony related messages.

adb logcat -b event View the buffer containing events-related messages.

adb logcat -b main default

adb logcat *:V lowest priority, filter to only show Verbose level

adb logcat *:D filter to only show Debug level

adb logcat *:I filter to only show Info level

adb logcat *:W filter to only show Warning level

adb logcat *:E filter to only show Error level

adb logcat *:F filter to only show Fatal level

adb logcat *:S Silent, highest priority, on which nothing is ever printed

 

  1. adb shell dumpsys

 

dumpsys is an android tool that runs on the device and dumps interesting information about the status of system services.

 

Obvious benefits:

  1. Possibility to easily get system information in a simple string representation.
  2. Possibility to use dumped CPU, RAM, Battery, storage stats for a pretty charts, which will allow you to check how your application affects the overall device!

 

If you run dumpsys you would see a ton of system information. But you can use only separate parts of this big dump.

To see all of the “subcommands” of dumpsys do:

dumpsys | grep “DUMP OF SERVICE”

 

Output:

 

DUMP OF SERVICE SurfaceFlinger:

DUMP OF SERVICE accessibility:

DUMP OF SERVICE account:

DUMP OF SERVICE activity:

DUMP OF SERVICE alarm:

DUMP OF SERVICE appwidget:

DUMP OF SERVICE audio:

DUMP OF SERVICE backup:

DUMP OF SERVICE battery:

DUMP OF SERVICE batteryinfo:

DUMP OF SERVICE clipboard:

DUMP OF SERVICE connectivity:

DUMP OF SERVICE content:

DUMP OF SERVICE cpuinfo:

DUMP OF SERVICE device_policy:

DUMP OF SERVICE devicestoragemonitor:

DUMP OF SERVICE diskstats:

DUMP OF SERVICE dropbox:

DUMP OF SERVICE entropy:

DUMP OF SERVICE hardware:

DUMP OF SERVICE input_method:

DUMP OF SERVICE iphonesubinfo:

DUMP OF SERVICE isms:

DUMP OF SERVICE location:

DUMP OF SERVICE media.audio_flinger:

DUMP OF SERVICE media.audio_policy:

DUMP OF SERVICE media.player:

DUMP OF SERVICE meminfo:

DUMP OF SERVICE mount:

DUMP OF SERVICE netstat:

DUMP OF SERVICE network_management:

DUMP OF SERVICE notification:

DUMP OF SERVICE package:

DUMP OF SERVICE permission:

DUMP OF SERVICE phone:

DUMP OF SERVICE power:

DUMP OF SERVICE reboot:

DUMP OF SERVICE screenshot:

DUMP OF SERVICE search:

DUMP OF SERVICE sensor:

DUMP OF SERVICE simphonebook:

DUMP OF SERVICE statusbar:

DUMP OF SERVICE telephony.registry:

DUMP OF SERVICE throttle:

DUMP OF SERVICE usagestats:

DUMP OF SERVICE vibrator:

DUMP OF SERVICE wallpaper:

DUMP OF SERVICE wifi:

DUMP OF SERVICE window:

Some Dumping examples and output

1) Getting all possible battery statistic:

$~ adb shell dumpsys battery

 

You will get output:

Current Battery Service state:

AC powered: false

AC capacity: 500000

USB powered: true

status: 5

health: 2

present: true

level: 100

scale: 100

voltage:4201

temperature: 271 <———- Battery temperature! %)

technology: Li-poly <———- Battery technology! %)

 

2)Getting wifi informations

~$ adb shell dumpsys wifi

 

Output:

Wi-Fi is enabled

Stay-awake conditions: 3

 

Internal state:

interface tiwlan0 runState=Running

SSID: XXXXXXX BSSID: xx:xx:xx:xx:xx:xx, MAC: xx:xx:xx:xx:xx:xx, Supplicant state: COMPLETED, RSSI: -60, Link speed: 54, Net ID: 2, security: 0, idStr: null

ipaddr 192.168.1.xxx gateway 192.168.x.x netmask 255.255.255.0 dns1 192.168.x.x dns2 8.8.8.8 DHCP server 192.168.x.x lease 604800 seconds

haveIpAddress=true, obtainingIpAddress=false, scanModeActive=false

lastSignalLevel=2, explicitlyDisabled=false

 

Latest scan results:

 

Locks acquired: 28 full, 0 scan

Locks released: 28 full, 0 scan

 

Locks held:

 

3) Getting CPU info

~$ adb shell dumpsys cpuinfo

 

Output:

Load: 0.08 / 0.4 / 0.64

CPU usage from 42816ms to 34683ms ago:

system_server: 1% = 1% user + 0% kernel / faults: 16 minor

kdebuglog.sh: 0% = 0% user + 0% kernel / faults: 160 minor

tiwlan_wq: 0% = 0% user + 0% kernel

usb_mass_storag: 0% = 0% user + 0% kernel

pvr_workqueue: 0% = 0% user + 0% kernel

+sleep: 0% = 0% user + 0% kernel

+sleep: 0% = 0% user + 0% kernel

TOTAL: 6% = 1% user + 3% kernel + 0% irq

 

4)Getting memory usage information’s

~$ adb shell dumpsys meminfo ‘your apps package name’

 

Output:

** MEMINFO in pid 5527 [com.sec.android.widgetapp.weatherclock] **

native   dalvik    other    total

size:     2868     5767      N/A     8635

allocated:     2861     2891      N/A     5752

free:        6     2876      N/A     2882

(Pss):      532       80     2479     3091

(shared dirty):      932     2004     6060     8996

(priv dirty):      512       36     1872     2420

 

Objects

Views:        0        ViewRoots:        0

AppContexts:        0       Activities:        0

Assets:        3    AssetManagers:        3

Local Binders:        2    Proxy Binders:        8

Death Recipients:        0

OpenSSL Sockets:        0

 

 

SQL

heap:        0         MEMORY_USED:        0

PAGECACHE_OVERFLOW:        0         MALLOC_SIZE:        0

 

If you want see the info for all processes, use ~$ adb shell dumpsys meminfo

 

  1. adb shell netstatUsing the above command we can get the current network statistics of a system or can be used to get details of a desired application. More details.
  2. adb pull <path of the from device> <local machine path>

Using the above command we can pull specific file from the connected device to the local machine (PC).

Example: adb pull /sdcard/test.txt e:\

 

  1. adb push <local> <remote>Using this command we can push a specific file to the device.
    Example: adb push d:\test.apk /sdcard/
  2. adb shell lsThe above command is used to list out all the files.
    Example: adb shell ls /system/bin
  3. adb shell cdThe above command helps to change the directory.
    Example: adb shell cd /system.
  4. adb shell rmRemoves the desired file from the android system.
    Example: adb shell rm /sdcard/filename.txt

    Use rm -i /sdcard/<file name>.txt to get a prompt before deletion.

  5. adb shell mkdirThe above command is used to make directories.
    Example: adb mkdir /sdcard/test
  6. adb shell pwdPrints the current working directory.
    Example: If current dir is system/bin, the same will be displayed.
  7. adb shell cpCopies files to the destination from source given.
  8. adb shell mvTo move or rename files.
    Example: adb shell mv /sdcard/test.txt /sdcard/test_renamed.txt
    The above example will rename the selected file.
    Example: adb shell mv /system/bin /system/tmp
    The above example will move the files from bin folder to tmp.
  9. adb installUse the above command to install apk on an emulator or connected device.
    Example: adb install <path of apk>

    adb install -l test.apk to forward lock application

adb install -r test.apk to replace existing application

adb install -t test.apk to allow test packages

adb install -s test.apk to install application on sdcard

adb install -d test.apk to allow version code downgrade

adb install -p test.apk  to partial application install

 

  1. adb uninstallUse the above command to uninstall an apk installed on emulator or connected device.
    Example: adb uninstall <Package name>
    adb uninstall -k com.test.app to Keep the data and cache directories around after package removal.

     

  2. adb shell pm list packagesPrints all packages, optionally only those whose package name contains the text in <FILTER>.

    adb shell pm list packages -f See their associated file.

adb shell pm list packages -d Filter to only show disabled packages.

adb shell pm list packages -e Filter to only show enabled packages.

adb shell pm list packages -s Filter to only show system packages.

adb shell pm list packages -3 Filter to only show third party packages.

adb shell pm list packages -i See the installer for the packages.

adb shell pm list packages -u Also include uninstalled packages.

adb shell pm list packages –user <USER_ID> The user space to query.

 

  1. adb shell pm pathPrints the path to the APK of the given package.
    Example: adb shell pm path com.android.phone
    Output: package:/system/priv-app/TeleService/TeleService.apk
Leave a Comment