Setting up the Shell for Android Development

Developing for Android through Android Studio can be cumbersome at times, especially if you just want to build your application and test it on an emulator. These common development tasks can be a little faster by running commands in a terminal!

First, you'll want to set an environment variable that points to the Android SDK installed by Android Studio. I've called it ANDROID_HOME:

export ANDROID_HOME="$HOME/Library/Android/sdk"

Next, add the SDK's tools to your PATH:

export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH"

This adds useful Android binaries (adb, emulator, and other tools).

Launching Android Studio can be slow, especially if you just want to start an existing emulator. This helpful shell function uses select to provide an interactive shell menu for launching an emulator from any existing avd:

start_emulator() {
  select AVD in $(emulator -list-avds); do
    if [ -n "$AVD" ]; then
      emulator -avd "$AVD" $@
      break
    fi
  done
}

Android Studio's build/run functionality can be slow, especially for large applications. After building your app from the command line (./gradlew assembleWhatever) you can install the resulting .apk file in a running emulator:

install_apk() {
  select APK in $(find . -type f -name "*.apk"); do
    if [ -n "$APK" ]; then
      adb install -r "$APK"
      break
    fi
  done
}