Friday 27 May 2011

shortcuts script for common debug tasks

I have been using this script for a while, it allows me to make shortcuts for common android debug tasks (e.g. logcat, shell, installing/overwriting/removing apks). There are probably better ways of doing it for the shell script experts out there, but it suits my purposes... and it's pretty streamlined.

There are aliases for common devices and common apps so i can start stuff with a few characters

So the first parameter denotes the function.
l = logcat
r = replace app (keeping data)
s = shell
i = install
u = uninstall
o = overwrite (uninstall then install)
ks = kill-server
ss = start-server

The second parameter denots the device (which are configured in the first if statement):
a = all connected devices (from 'adb devices')
f = first device (in 'adb devices' list)
e1 = emulator-5554
(the others are my hardware devices)

Then there are app package name shortcuts ... (the 3rd if statement)

To start logcat on ny nexus 1, I type:
ad l n1
And to overwrite an apk on all connected usb devices - it type (the 2nd 'a' param stands for 'all connected devices'), really useful when testing before a release ...
ad r a /path/to/apkfile.apk
Basically it's nothing too high tech, it just makes things much quicker and easier when i am debugging. If you have any suggestions then i am keen to hear them...

So without further ado, here is the script:
#!/bin/bash

# toggle this to test command output without execution 0=no exec
EXEC=1;

DEVICES='f'

if [ $2 ] && [ $2 = 'g1' ] ; then
 DEVICES=("HT848KV04600")
elif [ $2 ] && [ $2 = 'hero' ] ; then
 DEVICES=("HT9BSL901147")
elif [ $2 ] && [ $2 = 'arch' ]  ; then
 DEVICES=("A10-4BE40002-9FF80000-015F2F44-0D01601E")
elif [ $2 ]  && [ $2 = 'n1' ]  ; then
 DEVICES=("HT018P805702")
elif [ $2 ] && [ $2 = 'e1' ]  ; then
 DEVICES=("emulator-5554")
elif [ $2 ] && [ $2 = 's' ]  ; then
 DEVICES=("I5500b2e40b3c")
elif  [ ! $2 ] || [ $2 = 'a' ] ; then
 DEVLIST=`adb devices`
 DEVICES=()
 for D in $DEVLIST; do 
  if  [ $D = "device" ] ; then 
   DEVICES=(${DEVICES[@]} $PREV)
  fi
  PREV=$D
 done
# take the first device if not specified otherwise take all
 if [ ! $2 ] ; then
  DEVICES=${DEVICES[0]} 
 else 
  DEVICES=${DEVICES[@]}
 fi
fi
echo "using devices:" $DEVICES

OP=''
if [ $1 ] ; then
 if [ $1 = 'u' ] ; then
  OP="uninstall"
 elif [ $1 = 'i' ] ; then
  OP="install"
 elif [ $1 = 'r' ] ; then
  OP="install -r"
 elif [ $1 = 'l' ] ; then
  OP="logcat"
 elif [ $1 = 's' ] ; then
  OP="shell"
 elif [ $1 = 'ks' ] ; then
  OP="kill-server"
 elif [ $1 = 'ss' ] ; then
  OP="start-server"
 elif [ $1 = 'z' ] ; then
  OP=""
 elif [ $1 = 'o' ] ; then
  OP=""
 fi
fi
PRG=$3;
if [ $3 ] ; then
 if [ $3 = 'mpp' ] ; then
  PRG="package.name.1"
 elif [ $3 = 'st' ] ; then
  PRG="package.name.2"
 elif [ $3 = 'stp' ] ; then
  PRG="package.name.3"
 elif [ $3 = 'mp' ] ; then
  PRG="package.name.4"
 fi
fi

if [ ! $1 ] ; then
 echo "ad    extra"
 echo "CMD = o (overwrite)  extra=apk | l (logcat) | r (reinstall) extra=apk | i (install) extra=apk | s (shell) | u (uninstall) |  ks (kill-server) | ss (start-server)"
 echo "CMD = z (zipalign )"
 echo "DEV = g1 hero arch n1 s a(all)"
 echo "PRG = mp mpp st stp"
 echo "------devices---------"
 CMD="adb devices"
 echo $CMD
 if [ $EXEC = 1 ] ; then 
  $CMD
 fi 
else 
 if [ $1 = 'z' ] ; then
  NEWFILE=`echo $2 | sed "s/.apk/_z.apk/"`
  rm -fr $NEWFILE
  CMD="zipalign -v 4 $2 $NEWFILE"
  echo $CMD
  if [ $EXEC = 1 ] ; then 
   $CMD
  fi
 else 
  for DEV in $DEVICES ; do
   echo $DEV
   if [ $1 = 'o' ] ; then
    CMD="adb -s $DEV uninstall $PRG"
    echo $CMD
    if [ $EXEC = 1 ] ; then 
     $CMD
    fi
    CMD="adb -s $DEV install $4"
    echo $CMD
    if [ $EXEC = 1 ] ; then 
     $CMD
    fi
   else 
       CMD="adb -s $DEV $OP $PRG $4"
    echo $CMD
    if [ $EXEC = 1 ] ; then 
     $CMD
    fi
   fi
  done;
 fi
fi

No comments:

Post a Comment