Command Line Android Development: Basics

Although the Android ADT plugin in Eclipse provides some of the fanciest and most convenient development tools, with its graphical interfaces for resource editing, for one, and the million and one Eclipse IDE features on top of it all, I prefer to handle things at a lower level. There’s much more to learn from hitting the docs than to hit “Import missing packages” and “List override methods”, isn’t there?

Command Line Android Development

Fortunately, and obviously, the Android SDK comes with all the tools needed to develop from the command line, without having to resort to bulky IDEs and to get close and personal with Android development.

Creating a project

Easy. The tools directory has an android binary, use this like so:

android create project \
--target <target_ID> \
--name <your_project_name> \
--path path/to/your/project \
--activity <your_activity_name> \
--package <your_package_namespace>

More information: Managing Projects From The Command Line.

Your project will be initialized with all the necessary structure and files.

Building

You’ll have to have Apache ant for this one:

ant debug

This will build and sign the necessary .apk files. Building From The Command Line contains more detailed information.

Running

Grab the .apk files from the project bin directory and install them on your device manually. Or use the adb tool inside platform-tools, the Debug Bridge.

Install to a device: adb -d install -r bin/your-application-debug.apk
The emulator: adb -e install -r bin/your-application-debug.apk

The -r flag allows you to do a re-installation instead of an installation. When trying to install to a device that already has the application installed an error will occur.

To force the device run the application without tapping through the menu: adb -d shell "am start -a android.intent.action.MAIN -n com.your.package/.YourActivity"

Streamlining

One quick and dirty thing I use is a simple shell script:

#!/bin/bash
if [ $1 == "clean" ]
then ant clean; # force rebuild
fi

ant debug && adb -d install -r bin/myproject-debug.apk
adb -d shell "am start -a android.intent.action.MAIN -n com.my.project/.MyActivity"

Of course, with such a powerful build tool like Apache ant there is probably no excuse for not fiddling with the the build.xml files. And that’s what I’m going to look into very soon.

Additional tips

Add the platform-tools and tools to your PATH environment variable to be able to access the tools from any directory without typing full paths.

How do you streamline your Android development without the use of an IDE?