Android Test Setup

05/12/20152 Min Read — In Testing

Most of this is to simplify handling apps across multiple devices at once. Sourced from my pal @jackbrewster and messed up and mangled by me.

Add these to your ~/.bash_profile

Setup your environment: Add path for Android tools These commands require having the Android sdk tools in your path. You should be doing this anyway to use Android Studio.

export PATH=$PATH:/path/to/Android/sdk/platform-tools:/path/to/Android/sdk/tools:/path/to/Android/sdk/build-tools/{latest build tools version directory}/

Setup ANDROID_HOME value Depending on your setup, you might've had to set this up already.

# Setup ANDROID_HOME value
ANDROID_HOME=/Users/you/Library/Android/sdk/
export ANDROID_HOME

Assign static IPs to your devices so they're easy to identify when using adb over wifi, make a note of them in your .bash_profile for quick reference

# - Samsung S3: c80bbf97, 10.0.0.255:5555

adb magic: Script to run an adb command against every connected Android device This is where the effort really matter. It powers all the the batch operations.

# Get list of connected Android devices and run the passed adb command line parameter on them.
# Accepts one parameter: command line parameters to pass to adb
# Android Debug Bridge (adb) command documentation: http://developer.android.com/tools/help/adb.html
function adbbatch ()
{
local device
local command
local devicelist
local phone
# get list of connected devices
devicelist=(`adb devices | grep 'device$'`)
command=$@
for phone in ${!devicelist[*]}
do
device=${devicelist[$phone]}
if [ $device != "device" ]; then
echo \> Running command: adb -s $device $command
adb -s $device $command
echo
fi
done
echo \> Task Complete
}

Install an .apk file from the local computer onto all connected devices Just download it once, then feed the full path to this function.

Won't work when installing over an existing build (e.g., installed from the Google Play store).

# Install .apk file from local path (e.g., after downloading from Hockey or your favorite developer)
# Accepts one parameter: full path to .apk file
# Uses adbbatch() to install on all connected devices.
# Uses aapt (part of Android build tools, must be in PATH)
function adbinst ()
{
local pkgname
# Determine package name to launch
# Requires aapt be in PATH
pkgname=`aapt dump badging $@ | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
adbbatch "install $@"
adbbatch "shell am start -n \"$pkgname/$pkgname.LaunchActivity\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
}

adb bash aliases Generic aliases (don't require app package name) all start with 'adb'. App-specific require passing the app's package name, (e.g com.domainname). You can give your aliases any name you want, but for consistency, I'm using a naming convention of 'sh' at the beginning of each app specific shell command. Substitute your app's package name where appropriate.

Generic aliases

List the connected devices

Set up as an alias because you'll be doing this a lot

# List Android devices
alias adbd='adb devices'

Kill the adb server If devices aren't showing in the device list, or Android Studio, as expected, it can help to restart the adb server.

# Kill and restart adb server, list connected devices
alias adbk='
adb kill-server
adb devices'

Changing adb connections Enable adb over wifi This is handy when you have multiple devices, or when keeping a device tethered over USB is incovenient.

IMPORTANT: If you are using adb over wifi on a carry device, be sure to turn that off when you're done with a testing session. Leaving it enabled could allow anyone on any network you connect to reenable adb over wifi and connect to your phone.

Enabling adb over wifi can only be done with one device tethered at a time.

# Enable adb over wifi for USB-tethered device
alias adbew='
echo \> Enabling adb over wifi for connected device ;
adb tcpip 5555 ;
echo ;
echo \> Task Complete'

Disable adb over wifi for all USB-tethered devices Disabling adb over wifi appears to work with all tethered devices.

# Turn off adb over wifi for USB-tethered device
alias adbdw='
echo \> Disabling adb over wifi for connected device ;
adb usb ;
echo ;
echo \> Task Complete'

Turn on the adb over wifi connection After enabling adb over wifi on your device(s), you need to tell adb to connect to them. The format of the adbconnectcommand is different than the other adb commands, and I didn't get around to updating adbbatch() (or creating a new batch command) to handle it. So for now, I've hard-coded in the static IPs of each device.

# Connect adb over wifi
# Requires the IP of each device. This is why I recommend static IPs.
# Uses different parameter order than adbbatch, so hard-coded with static IPs for now.
alias adbcx='
clear
echo Connecting wifi devices
echo ;
echo \> Connecting to Samsung S3 ;
adb connect 10.0.0.60:5555 ;
echo ;
echo \> Connecting to HTC One X ;
adb connect 10.0.0.61:5555 ;
echo ;
echo \> Connecting to HTC One ;
adb connect 10.0.0.62:5555 ;
echo ;
echo \> Connecting to Moto G ;
adb connect 10.0.0.63:5555 ;
echo ;
echo \> Connecting to \(ML\) Samsung S3 ;
adb connect 10.0.0.64:5555 ;
echo ;
adb devices ;
echo \> Task Complete'

Disconnect adb wireless devices

# Disconnect adb from all wifi devices.
# Does _not_ turn off adb over wifi on the device. You will still need to tether each device and run `adb usb` to do that.
alias adbdx='
clear
echo \> Disconnecting wifi devices ;
adb disconnect ;
echo ;
adb devices ;
echo \> Task Complete'

Reload SD card After copying files to device or emulator, you may need to run this command before they appear in File Manager, Gallery, etc.

# Reload SD Card
# Uses adbbatch() to refresh all connected devices.
alias adbsd='adbbatch "shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard/"'

App-specific aliases Uninstall app

# Uninstall app from all connected devices
alias shun='adbbatch "uninstall com.domainname"'

Clear cached app data

# Clear data from all connected devices, keep app installed. (Similar to a clean install.)
alias shclear='adbbatch "shell pm clear com.domainname"'

Uninstall app from all connected devices

# Uninstall app Beta from active devices
# Beta build has a different package name
alias shbun='adbbatch "uninstall com.domainname.beta"'
# Stop app on all connected devices.
# Never really use this, but good example of things that can be done.
alias shstop='adbbatch "shell am force-stop "com.domainname""'
# Start app on all connected devices
# Haven't used this as a stand-alone command, but it is included in adbinst() to launch the app
# after installing
alias shstart='adbbatch "shell am start -n "com.domainname/com.domainname.LaunchActivity" \
-a android.intent.action.MAIN -c android.intent.category.LAUNCHER"'