Skip to content

Instantly share code, notes, and snippets.

@calebsmith
Last active August 29, 2015 14:06
Show Gist options
  • Save calebsmith/c80b221b12b0cba354d2 to your computer and use it in GitHub Desktop.
Save calebsmith/c80b221b12b0cba354d2 to your computer and use it in GitHub Desktop.
Command-line Android Build Sanity
#!/bin/bash
function android_get_pkg() {
# Gets the name of the package
echo $(aapt dump badging $1 |
awk -F" " '/package/ {print $2}' |
awk -F"'" '/name=/ {print $2}'
)
}
function android_get_main() {
# Gets the name of main launchable activity
echo $(aapt dump badging $1 |
awk -F" " '/launchable-activity/ {print $2}' |
awk -F"'" '/name=/ {print $2}'
)
}
function android_get_project_name() {
# Gets the name of the project
echo $(
cat build.xml |
awk -F"\"" '/project name=/ {print $2}'
)
}
function android_get_build_filename() {
# Gets the filename of the build
echo "bin/"$(android_get_project_name)"-"${1:-debug}".apk"
}
function android_build() {
# Create a clean build
ant clean ${1:-debug}
}
function android_install() {
# Install the build onto the connected device (must be built)
adb install -r $(android_get_build_filename)
}
function android_run() {
# Run the main activity of the project on the phone (must be installed)
local build_filename=$(android_get_build_filename)
local pkg=$(android_get_pkg $build_filename)
local main=$(android_get_main $build_filename)
adb shell am start -n $pkg/$main
}
function android_go() {
# Do a build, install, and run, stopping on failures
echo "Building target" ${1:-debug}
android_build
if [[ $? -eq 0 ]] ; then
echo "(Re)Installing APK"
android_install
if [[ $? -eq 0 ]] ; then
echo "Running APK"
android_run
fi
fi
}
It took me way too long to use the official docs and stackoverflow to figure
out a sane command-line workflow for Android development. The following is a
breakdown of the most important tools and how to automate them a little.
A lot of this info is taken from the following StackOverflow posts:
http://stackoverflow.com/questions/4893953/android-run-install-debug-applications-over-wifi
http://stackoverflow.com/questions/4567904/how-to-start-an-application-using-android-adb-tools
A big thanks to the contributors there!
-------
The most important command line tools are your choice of build tool, likely
Ant or Gradle, and the Android Debug Bridge "adb".
It is assumed that all android build and platform tools are on your path for
programs such as ant, adb, and aapt.
The following outlines the four most common uses of these:
1. Connect over WIFI to a real phone
The emulator is a neat curiosity but it is painfully slow to actually use.
A USB cable is probably a *little* faster and more reliable to transfer apk's
and commands with but I find it frustrating to leave the phone plugged in
while trying to play test.
Connect to your phone over local WIFI (Probably best for home use as opposed
to a public network. I.E. - Use a USB cable in the coffee shop)
Use your actual IP from the WIFI settings "advanced" settings menu.
# Connect USB Cable
adb tcpip 5555
adb connect 192.168.1.120:5555
# Disconnect USB
You only need to connect once, but the rest of these steps can be repeated
as needed.
The following will list the connected devices that adb can find
adb devices -l
2. Build
Clean up from last build and do a debug build.
Obviously, this is for using ant, otherwise do the usual gradle build command
ant clean debug
3. Install the apk onto the phone
Install the new build, reinstalling on top of what is already there.
Ant will build the apk file as bin/{projectname}-{build}.apk
adb install -r bin/ProjectName-debug.apk
4. Run the app from the command line of the computer
(Re-launching the program with my dumb human thumbs is slow and annoying)
Tells the remote shell to launch the main activity of the project.
Can also pass in other activities and specify intents as needed.
adb shell am start -n com.project/com.project.MainActivity
-----------------
The above should familiarize you with adb and ant a little. Regardless of the
IDE/Text-editor you use, digging deep into adb is useful for many debugging
and installation scenarios. For instance, `adb shell` can be used to access a
remote shell on the phone. However, typing the above commands repeatidly for
each test build gets pretty crazy.
Attached as "android_helpers.sh" are some bash functions to help with that.
You can put them wherever you store user shell scripts, possibly ~/bin:
If you source this in your ~/.bash_profile or ~/.bashrc, you'll have these
available. From here, you can just `android_go` from the project's root
directory to do everything, or do each of `android_build`, `android_install`,
and `android_run` as needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment