Thursday, January 22, 2009

Developing Android without Eclipse or Ant

The Android documentation recommends using Eclipse for development, but this is sluggish on my laptop, and feels cluttered on compact screens. A stated alternative is to use Ant, but this is also unnecessary as the build commands it invokes can be easily extracted and executed manually. This suits my preference for a minimalistic or practically non-existent IDE, especially on laptops, as well as my distaste for editing XML. As a bonus, on Ubuntu, I install one package:

$ sudo apt-get install sun-java6-jdk

along with the Android SDK and I'm ready to develop for Android. In contrast, the Ant package has several dependencies, and the required version of Eclipse is newer than the currently available Ubuntu package.

Here are the details:

$ SDKDIR=~/android-sdk-linux_x86-1.0_r2 # Where I extracted the SDK.
$ activitycreator -o somepath
$ cd somepath

At this point Ant can build the project, but masochists like me will prefer to shun Ant and run the underlying commands themselves. The following command generates from the resources:

$ aapt p -m -J src -M AndroidManifest.xml -S res -I $SDKDIR/android.jar

Next, the source is compiled to .class files. This is why we need the JDK:

$ mkdir bin/classes
$ javac -encoding ascii -target 1.5 -d bin/classes \
-bootclasspath $SDKDIR/android.jar src/some/project/*
# When libs exist, append -classpath=libs/*.jar

These files contain instructions for the JVM. They are converted to Dalvik bytecode via:

$ dx --dex --output=bin/classes.dex bin/classes # If libs exist, append libs/*.jar

The resources and assets are packaged:

$ aapt p -f -M AndroidManifest.xml -S res -I $SDKDIR/android.jar -F bin/projectname.ap_
# When assets exist, add -A assets

This package is itself packaged with the Dalvik bytecode to make the final product:

$ apkbuilder bin/something.apk -z bin/projectname.ap_ -f bin/classes.dex -rf src -rj libs
# Use -u for unsigned builds.

Now if I could only roll my own Dalvik compiler for a language I like, I could avoid the Java SDK, as well as Java itself.


tmmcken said...

Ben, thanks so much for posting this. It has been extremely helpful and streamlined my coding process greatly.

I was wondering what changes, if any, need to be made to this procedure with the release of the Android SDK 1.5

Thanks again!

Ben Lynn said...

The aapt and dx tools now live in the platforms/android-1.5/tools subdirectory instead of just tools, and android.jar has moved from the root of SDK directory to platforms/android-1.5/.

Also, instead of activitycreator, run android create project.

These changes should be enough to build projects with the 1.5 SDK. However, ideally one should respect the new convention with generated Java files such as they should be kept in the gen subdirectory instead of src. Twiddle a few command-line options to achieve this.

See also the official guide to upgrading the SDK.

Nic said...

Hi Ben,
If possible, can you say a few words about how this procedure changes with Android 2.0.


Nic said...

Ben, I've answered my own question. I thought I would post it just in case anyone else comes across your site. Basically the only change is that in Android 2.0 you no longer have "activitycreator" you now have to use "android". Users can run android --help for how to use it. Everything else works just as you point out in your post.

Bill M said...

Actually, I'm a moron because I can never seem to get Eclipse to work properly for one reason or another. So this has been very helpful. I'm happy to be free of the dependency. Thank you.

Are there many more people like us? Doesn't seem so... I'd like to have a support group - maybe even on IRC.

harpo said...

Many thanks for this post! I couldn't have gotten through this process without your detailed instructions.

To write this as a bash script that would run on cygwin, I had to replace dx.bat and apkbuilder.bat with *their* underlying commands.
So I guess I'm even more masochistic than you are :)