Skip to content

Instantly share code, notes, and snippets.

@seawolf
Last active December 15, 2015 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seawolf/5343754 to your computer and use it in GitHub Desktop.
Save seawolf/5343754 to your computer and use it in GitHub Desktop.
Building and Deploying an Android app from the Shell
#!/bin/bash
#
# android-compile.bash
# Cleans, compiles, installs and starts your Android project.
#
# Copyright (C) 2013 Ben Arnold
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Visit http://www.gnu.org/licenses for the GNU General Public License.
PACKAGE=$1
ACTIVITY=$2
if [[ -z $PACKAGE || -z $ACTIVITY]]; then
echo " ! If you supply both the package name of your application and your main activity, we can automagically start it."
fi
function progress() {
while true ; do
echo -n "·"
sleep 1
done
}
echo " * Cleaning up..."
progress &
PROGRESS_BAR=$!
ant clean -silent || ( killall sh ; exit $? )
kill $PROGRESS_BAR
wait $PROGRESS_BAR 2>/dev/null
echo " * Build started at $(date)" ; echo -n " "
progress &
PROGRESS_BAR=$!
ant release -quiet || ( killall sh ; exit $? )
kill $PROGRESS_BAR
wait $PROGRESS_BAR 2>/dev/null
echo " · Build finished at $(date)."
APK=$(ls -1 bin/*.apk | head -n1)
echo " · Saved to: $APK"
if [[ ! -z $(adb devices | tail -n+2 | head -n-1) ]] ; then
echo " * Installing..."
adb -d install -r $APK || exit $?
if [[ (! -z "$PACKAGE") && (! -z "$ACTIVITY") ]]; then
echo " * Starting..."
adb shell "am start -n $PACKAGE/.$ACTIVITY" || exit $?
adb -d logcat $PACKAGE:D System.out:I *:S || exit $?
fi
fi
Eclipse is a productive environment for developing an Android application, but I still spend a lot of time at the shell working with Git, so I wondered if I could use Vim and a script to code and build/deploy my app.
I summised that I'd need my Vim to format Java files the same way as Eclipse, which means:
* indenting lines with one, four-character tabstop (Ruby is two spaces)
* using one tabstop instead four spaces (Ruby is spaces instead of tabs)
* ending each file with a blank line (I like Vim to chomp whitespace off of the end of lines and end the file at the last line of text, as Git is whitespace-observant by default)
Adding this into my .vimrc allowed for these rules only in Java files:
au FileType java :setlocal softtabstop=4 tabstop=4 shiftwidth=4 noexpandtab
It's the `setlocal` that tells Vim to use these settings only in the current buffer, and not for the rest of the session (i.e. when you open another file). I also needed the colon in there for some reason.
With my Vim featuring syntax highlighting, autocompletion and the like, I can now code Java without going against the coding style used by Eclipse. The only problem, however, is that coding solely Ruby, JavaScript and Bash/Zsh for the past few years has left my Java skills too weak without the keyword and syntax helpers and code block auto-generation that Eclipse provides. Still, I find Git and this build script a lot quicker to use and run than the equivalent process in Eclipse, even if I'm not coding in Vim.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment