Category Archives: Android adb Commands

adb devices reporting Nexus 7 offline

Occasionally run into a problem when switching devices (unplugging one device and plugging in a different one to collect logcat information), “adb devices” reports my Nexus 7 (running 4.2.2) as offline.  Bouncing the adb service (adb kill-server, adb start-sever) doesn’t fix it, either use unplugging the device and plugging it back in.  So far, one method (discovered by accident seems to work the best) – putting my mac to sleep (this often happens due to having to attend meetings) when I return and run “adb devices” again,the Nexus 7 is no longer offline – wacky but it least it works.  Will try putting the mac to sleep on purpose next time to see if it works.

Advertisements

Finding shell command options and memory usage

Filesystem disk space usage

  • adb shell df

 

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

  • adb shell ls /system/bin
Which returned:

  • adb shell ls /system/bin
    • abcc
    • adb
    • am
    • app_process
    • applypatch
    • atrace
    • bmgr
    • bootanimation
    • btmacreader
    • bu
    • bugmailer.sh
    • bugreport
    • cat
    • chmod
    • chown
    • cmp
    • content
    • cp
    • dalvikvm
    • date
    • dbus-daemon
    • dd
    • debuggerd
    • dexopt
    • df
    • dhcpcd
    • dmesg
    • dnsmasq
    • drmserver
    • du
    • dumpstate
    • dumpsys
    • fsck_msdos
    • getevent
    • getprop
    • glgps
    • grep
    • gzip
    • hd
    • hostapd
    • id
    • ifconfig
    • iftop
    • ime
    • input
    • insmod
    • installd
    • ioctl
    • ionice
    • ip
    • ip6tables
    • iptables
    • keystore
    • kill
    • linker
    • ln
    • log
    • logcat
    • logwrapper
    • ls
    • lsmod
    • lsof
    • make_ext4fs
    • md5
    • mdnsd
    • mediaserver
    • mkdir
    • mksh
    • monkey
    • mount
    • mtpd
    • mv
    • nandread
    • ndc
    • netcfg
    • netd
    • netstat
    • newfs_msdos
    • notify
    • ping
    • pm
    • pppd
    • printenv
    • ps
    • racoon
    • reboot
    • renice
    • requestsync
    • rm
    • rmdir
    • rmmod
    • route
    • run-as
    • schedtest
    • schedtop
    • screencap
    • screenshot
    • sdcard
    • send_bug
    • sendevent
    • sensors-config
    • sensorservice
    • service
    • servicemanager
    • setconsole
    • setprop
    • settings
    • setup_fs
    • sh
    • sleep
    • smd
    • start
    • stop
    • surfaceflinger
    • svc
    • sync
    • system_server
    • tc
    • tf_daemon
    • toolbox
    • top
    • touch
    • uiautomator
    • umount
    • uptime
    • vdc
    • vmstat
    • vold
    • watchprops
    • wipe
    • wpa_supplicant

 

Doing it all on a Mac (osx)

Mobile testing at a new company, the catch is I am not on a Mac, I have the commands (adb) work the same as before, but getting the Android SDK stuff to run is a bit different than I am used too (since I configured everything on windows before) at least I shouldn’t have to worry about Windows USB drivers this time around.

I downloaded the latest Android SDK from the Android site and made a point to ignore the eclipse instructions, I am not a developer and I dislike eclipse.  I unzipped the osx and then moved it to a different directory /Users/me/Android/…..  (use what ever works for you)

Now to run things like the SDK or the AVD you can type the command “android sdk” – if the path is setup, the android “tool” is in this directory: /sdk/tools, if your path isn’t setup you can cd to that directory and run the command locally:

  • ./android sdk – to download drivers and such
  • ./android avd – to create new virtual devices

If you set your path (so the system can find the commands) you can run this and other commands easier.

To check what your path is currently set too, you can use the command (you have to run this in a terminal, the linus/osx equivalent to a command prompt:

  • echo $PATH

You can also see what is configured in the paths file, or in the paths.d directory

  • more /etc/paths
  • ls -l /etc/paths.d

Some info about managing paths is here – though it might vary on some older osx installs.

For myself I added a file to paths.d directory for my android stuff (the sudo command will only work if you have those privileges on your mac):

  • sudo sh -c ‘echo “/Users/me/Android/sdk/tools” >> /etc/paths.d/android_paths
  • sudo sh -c ‘echo “/Users/me/Android/sdk/platform-tools” >> /etc/paths.d/android_paths’
    • Note/Reminder “>>” appends to the existing file use “>” to create a new file

Then I ran ls -l /etc/paths.d to verify the file was there:

Me:platform-tools me$ ls -l /etc/paths.d/
total 8
-rw-r–r–  1 root  wheel  75 Dec 1o 11:44 android_paths

To “refresh” your terminal to start working with the new path once you updated it:

  • . /etc/profile

Backing up the Android

WIP – I think this would be straight forward since one of the “adb” subcommand options is “backup” however it doesn’t seem to be that simple (at least for me).

First the command:

adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|nosystem] [<packages…>]

Note: If a file isn’t specified than it defaults to backup.ab

However apparently the other items aren’t optional it appears, but before I got to specifying the command correctly (if there is a syntax error adb responds by dumping the “help” text to the screen:

adb backup -f C:\Android\huawei_backup.db -all

I was getting the error:  adb: unable to connect for backup

Haven’t found a solution yet

Turning the output from getevent into something something that can be used

My end goal was to to be able to “doodle” on the android screen, with a script.  I can’t do the math to generate the image I want, so I ran the adb shell command “getevent” then using the mouse, I drew on the screen.

However the output isn’t immediately usable, first there was the minor problem of it containing a colon which would throw an error when I tried to run it.  The second and bigger issue, is that the getevent command captures everything in hexadecimal, but the command sendevent, requires everything to be in decimal.  Since monkeyrunner is basically python (the docs say jython, but I haven’t used anything that advance yet), I wrote a python script, that takes an input file containing a bunch of lines that look like this:

/dev/input/event0: 0003 0000 00000073
/dev/input/event0: 0003 0001 000000a8
/dev/input/event0: 0001 014a 00000001
/dev/input/event0: 0000 0000 00000000
/dev/input/event0: 0003 0001 000000a4

And created a new output file with adb shell commands that look like this:

adb shell sendevent /dev/input/event0 3 0 115
adb shell sendevent /dev/input/event0 3 1 168
adb shell sendevent /dev/input/event0 1 330 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 3 1 164

Which can be run and do the drawing for me on the screen.  This worked, but running a bunch of adb shell commands is really, really slow.  So I tweaked my python script, and instead have to create something like:

#!/bin/sh
echo Running – signature function
sendevent /dev/input/event0 3 0 115
sendevent /dev/input/event0 3 1 168
sendevent /dev/input/event0 1 330 1
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 3 1 164
sendevent /dev/input/event0 0 0 0
……..

This is copied to the device, and run as a local script and runs at an ok speed (not really fast, but much faster than adb shell commands) – the python script prints out instructions with the exact command to run, to copy the file to the device and what to execute it run it.  I will post the python separately, I am new to this code so the code isn’t prefect, but it works and meets my needs.ew

The steps are

  1. Run the app (I did it in the emulator)
  2. Run the command adb shell getevent, om whatever screen you want to doodle/draw on – the output is dumped to the screen
  3. Capture that output when you are done (copy it, usually by right clicking and choosing mark, highlight the area and then hit enter)
    1. Paste the info into notepad, and save it to any file name you want (i.e. captured.dat)
  4. Run the script
    1. hex_to_dec.py captured.dat
      1. catpured.bat is the input data for the script
  5. The script runs pretty fast and kicks out a file captured.scr (the code can be updated to name the file anything)
    1. The script prints out instructions for coping the script to the device and for running the script, that looks something like this

C:\>hex_to_dec.py raw_captured_sig.dat
Processing complete
File created: raw_captured_sig.scr

Copy file to the device
adb push raw_captured_sig.scr /sdcard/raw_captured_sig.scr

Run the script
adb shell sh /sdcard/raw_captured_sig.scr

adb shell sendevent, sending touch like events

Oh joy – output in hex but I need decimal

Running my test app in the emulator, to figure out where I was “touching” on the screen, I used the command:

adb shell getevent

Which returns something like:

C:\>adb shell getevent
add device 1: /dev/input/event0
name: “qwerty2”
could not get driver version for /dev/input/mouse0, Not a typewriter
could not get driver version for /dev/input/mice, Not a typewriter
/dev/input/event0: 0003 0000 00000073
/dev/input/event0: 0003 0001 000000a8
/dev/input/event0: 0001 014a 00000001

Which returns everything in hexidecimal, but you can’t send these commands directly back, you had to convert them to decimal, so this

adb shell sendevent /dev/input/event0 0003 0000 00000073
adb shell sendevent /dev/input/event0 0003 0001 000000a8
adb shell sendevent /dev/input/event0 0001 014a 00000001

Becomes this:

adb shell sendevent /dev/input/event0 3 0 115
adb shell sendevent /dev/input/event0 3 1 168
adb shell sendevent /dev/input/event0 1 330 1

However my capture for what I need to doodle on the screen is like 905 lines, that need to the colon removed from “event0:” and have the values converted to decimal to actually work.  Working on a script to take care of this formatting for me.

 

Another great post about this can be found here:  http://softteco.blogspot.com/2011/03/android-writing-events-low-level-touch.html

adb shell sendevent (could not open /dev/input/event0:, No such file or directory)

This is still a work in progress to get monkeyrunner to interact with a screen that people need to basically draw on.

I found this command to get back coordinates of what is being touched on the screen:

adb shell getevent

Which returns something like this

add device 1: /dev/input/event0
name: “qwerty2”
could not get driver version for /dev/input/mouse0, Not a typewriter
could not get driver version for /dev/input/mice, Not a typewriter
/dev/input/event0: 0003 0000 00000073
/dev/input/event0: 0003 0001 000000a8
/dev/input/event0: 0001 014a 00000001
/dev/input/event0: 0000 0000 00000000

….

Trying to send this command back, failed

adb shell sendevent /dev/input/event0: 0003 0000 00000073
adb shell sendevent /dev/input/event0: 0003 0001 000000a8

with this error:

could not open /dev/input/event0:, No such file or directory

After playing around for a bit, I found that the colon was the problem (after event0), removed that the error went away, I don’t see anything on the screen yet, but at least the error is gone.

Correct sytax:

adb shell sendevent /dev/input/event0 0003 0000 00000073
adb shell sendevent /dev/input/event0 0003 0001 000000a8

v

 

Updating the Hostfile for testing on the android

For a new test I had to update the hostfile on my droid to point to a new environment, this task turned out be to be tougher than it should be – considering a droid is basically a computer.

The hostfile is protected and in order to be able to modify it you need to be able to “root” the phone  (android version of iPhone’s jailbrake, basically break into the phone to override defaults).  This was easier said than done, I found many references to z4root.  Many of the links didn’t work, or went to a site siezed by the US government, or my local AV software would prevent the download due to it triggering virus alerts.  When I finally got the file, I found the that the OS that came installed default on my phone (Huawei U8500) had been fixed and I couldn’t use z4root.

So in the end I ended up using the Emulator that is installed with the android SDK (note you do not need eclipse for this, I don’t even have eclipse installed).  I ran the AVD Manager, and created a virtual device that matched the specs of my test Android Huawei U8500 (I called the device huawei_u8500).  Then I ran the emulator (launching it command line) with that single device

emulator -wipe-dataavd huawei_u8500

-wipe-date has the emulator start with a clean image.

Once it was running (takes a few minutes to launch, knowing the hot keys to naviagate helps, since I the emulator lacks a back button.

Android Emulator: Keyboard Commands (http://developer.android.com/guide/developing/tools/emulator.html)

Then came updating the host file, one thing you need to do is remount the device image as writable, then pull the file, update it, and copy it back.  Fortunately that is actually easy compared to everything else.

remount the device image as writable

adb remount

Pull the host file from the device

adb pull /system/etc/hosts c:\Android\hosts

You can put the file anywhere and call it anything you want, I called it “hosts” and put it in my android directory

Next open the file in your favorite test editor (I use notepad++, but notepad is fine).  Add in the host file entries you need, for me this was the change:

127.0.0.1     localhost  (this was the original entry)
10.16.1.11  www.newsite.com  (what I added for my test)

Copy the edited hosts file back to your “device”

adb push c:\Android\hosts /system/etc

Now you are done and ready to test, every time in the emulator you go to “www,newsite.com” it will now be directed to 10.16.1.11.

 

 

Get the version from the phone using adb

Using adb you can get the version of software running on your android, if you grep for a certain string (piece of data).  Windows is my test machine, yet I like grep for command line searches, so I found a version I can run on my machine and installed it and updated the path so ir works from anywhere.

The abd command:

adb bugreport |grep build.version.release

Results (2.2.2 is the version running on my test android):

[ro.build.version.release]: [2.2.2]

As for the adb command, “bugreport” produces a lot of information (usually way too much), I took the output and piped it “using the pipe operator (shift \) on windows keyboards to route the output of the bugreport command through grep to get just the piece of information I needed.

I have a write up about getting grep and updating your windows path here.

Get a file listing from the Android Device (sdcard)

You can push or pull files from the device, but to get a file listing just shell to the device (for me I needed a listing of what I had pushed to the sdcard, since it has been a while since I worked with a particular device)

adb shell <enter>

prompt changes to $

cd sdcard <enter>

then ls <enter>

results:

ls
LOST.DIR
Android
DCIM
download
media
upload_avatar.jpg
login_5304301725.txt
testMonkey.txt

To nuke (delete/remove) a file rm <filename>

Note:

Tab completion doesn’t work (at least on my windows machine with just the Android SDK installed)

And there is confirmation when you hit enter the file is gone

Then you can get out of the shell by typing exit