Android Debug Bridge Tricks

Google Android LogoOver the last couple of months I have been doing a lot of work with Cordova and the Ionic Framework creating my field guide of NSW Marine Life for the Android platform.

When I started out working with Android my work flow was pretty simple:

  • Make the required changes to the application code
  • Test in a browser using the simple web server provided with Python package
  • Use Cordova build command to build an apk package for testing on an actual Android device
  • Push the compiled package to the test device using adb to install the package on a device with developer model enable

Over time as my familiarity with the Android tool chain progressed. I found the Android Debug Bridge ( adb ) tool that makes up part of the Android SDK has a lot more to offer. Below are some of handy tips I have found with adb that can make a developers life a little bit easier.

Wake / Unlock / Lock a Device

Using adb you can wake a device from a sleep state without touching the device.

[codesyntax lang=”bash”] adb shell input keyevent 82[/codesyntax]

Alternately you can also send an awake device to sleep with the command.

[codesyntax lang=”bash”] adb shell input keyevent 26[/codesyntax]

Packages & Performance

The android debug bridge can also return a list of the installed packages installed on the connected device.

[codesyntax lang=”bash”] adb shell pm list packages -f[/codesyntax]

To see the CPU usage of the processes running on the device the following command can be used.

[codesyntax lang=”bash”] adb shell dumpsys cpuinfo[/codesyntax]

Since adb shell is essentially just executing shell commands on the device you can run a lot of common UNIX commands.

Capture a Screenshot

Still not impressed? You can export a screen shot from a device to the current directory if your using Linux.

[codesyntax lang=”bash”]  adb shell screencap -p | perl -pe ‘s/x0Dx0A/x0A/g’ > screen.png [/codesyntax]

This feature is pretty handy when taking screen shots for use in the Google Play store. As it saves taking the screen shot manually and then transferring to a computer for use with the device art generator.

Record a Screencast

On devices running Android Kit Kat (4.4+) and above we can even take things a step further and record a screen-cast from the device! This function is extremely useful for creating promotional videos and tutorials for an application.

To get started simply execute:

[codesyntax lang=”bash”]adb shell screenrecord /sdcard/example.mp4 –time-limit 5 [/codesyntax]

This will record 5 seconds of video from the device and save to the file example.mp4 in /sdcard/. Using adb we can now pull the video off the device to the current directory on the computer.

[codesyntax lang=”bash”] adb pull /sdcard/example.mp4 [/codesyntax]

The video file can then be deleted on the device by using adb shell with the rm command.

[codesyntax lang=”bash”]adb shell “rm /sdcard/example.mp4″[/codesyntax]

Some other options available for use with the screen recording functionality are:

–size The dimensions of the recorded video, by default this will mimic the display resolution of the device. Using this feature to make low res videos in conjunction the “show touches” option enabled in the developer settings on the device for super helpful bug reports.

–bit-rate The bit rate of the video in bits per second (bps). When not specified the default video bit rate is 4Mbps.

–time-limit The length of the video in time to record in seconds. The maximum length video that can be recorded is 3 minutes.

–verbose By default the screen recording process will not display any information when recording. To see any information output by the process in the terminal you need to use this option.

–rotate Rotates the screen 90 degrees.

WARNING: At the time of writing the documentation for the Android Debug Bridge have the rotate function marked as experimental. So you mileage may vary with this feature.

Related Reading:

Google Documentation for the Android Debug Bridge