Thursday, January 6, 2011

Android "Hello World": a Tale of Woe

I consider myself a fairly patient man, but if there's one thing on this good green earth that should be stupid simple to do for a development platform is to find the development tools, install them, and be able to run a well documented "Hello World" example. I've done this any number of times in the past with everything from FORTRAN (on punch cards) to Objective-C, and it's generally been a positive experience.

And then there's Android.

I work every day with Visual Studio and the .NET Framework, and I've dabbled with iPhone development, so perhaps I'm spoiled...the stuff "just works" (at least for simple stuff). But my attempts to install the Android tools were an exercise in frustration.

The Android developer site provides lots of great, well organized I knew that I had to install 3 different items to get up and running:
It started to get interesting when I had to figure out exactly which version of each of these I needed (since they all need to play nicely together), and found that the current version of Eclipse (Helios v3.6) is incompatible with the Android SDK, so I needed to install the previous version (Galileo v3.5). No biggie. Install different version of Eclipse.

And I needed to install the ADT Plugin for Eclipse. OK, done.

And I need to configure Eclipse so it knows where the Android SDK lives. Gotcha. (C:\Program Files (x86)\Android\android-sdk-windows was my location, for those of you who may be playing along at home.)

Things got a little more interesting when fired up the Android SDK and AVD Manager to create my first virtual device (i.e. the phone emulator). Ah, but which of the 5 different available versions of Android should I target? The latest and greatest is Gingerbread (v2.3), so I figured "Cool, I'll go with that!" I gave it the very clever device name of "gingerbread", and all seemed right with the world. But when I tried to start my shiny new virtual device, I got an error: "could not find virtual device named 'gingerbread'".


A little digging, and I found that for some inexplicable reason, the virtual device had been created under a different machine user account, so the AVD Manager couldn't find the files it needed. Well, I can just drag and drop the files to my user directory.

No joy.

Well, I can just change the default location the AVD Manager uses when it creates an AVD. Dig, dig, dig. Ah, I can specify the location of the AVD using a command line utility. Seriously? A command line utility?!? It's 2011...the year AFTER we make contact. WTH?

I find the syntax, and after much frustration (apparently it's both case-sensitive, and the order of the switches must also be important...because the example I copied and pasted from the page didn't work) I get it to work. I also figure that maybe the latest and greatest Android version isn't quite ready for prime time, so I revert back to the previous version (Froyo v2.2). So I now have an AVD (called "froyo") created in the location I wanted using the following:

android create avd –t 4 –n froyo –p c:\users\wloescher\.android\avd\ --force

(And don't get me started about TargetID vs. Version Number...sheesh!)

But of course this AVD doesn't show up in the AVD Manager, so I can't just click a button to fire it up. So by this time I'm calling in some help from my co-worker (who actually owns an Android phone and has gotten all this stuff working just fine on his machine). He comes up with the following command line to actually launch the virtual device:

emulator -data c:\users\wloescher\.android\avd\userdata.img

A minute or two later, and the virtual device is booted up and I have a virtual Android phone screen. Great! Now all I have to do is write a little code, and I'm done...right?

Wrong. There seems to be a disconnect between Eclipse and my AVD. The code seems to run fine (i.e. no errors), but nothing happens. Nothing. Not an electronic sausage. Just this error in the console:

[2011-01-06 10:43:11 - HelloAndroid] ------------------------------
[2011-01-06 10:43:11 - HelloAndroid] Android Launch!
[2011-01-06 10:43:11 - HelloAndroid] adb is running normally.
[2011-01-06 10:43:11 - HelloAndroid] Performing com.example.helloandroid.HelloAndroid activity launch
[2011-01-06 10:43:11 - HelloAndroid] Automatic Target Mode: launching new emulator with compatible AVD 'froyo'
[2011-01-06 10:43:11 - HelloAndroid] Launching a new emulator with Virtual Device 'froyo'
[2011-01-06 10:43:11 - Emulator] emulator: ERROR: unknown virtual device name: 'froyo'
[2011-01-06 10:43:11 - Emulator] emulator: could not find virtual device named 'froyo'

So...what have we learned?

Well, first off I'd like to thank the fine folks at Microsoft and Apple for making development tools that have a low cost of entry. Secondly, I hate the command line. Thirdly, I picked up a few new (to me) command line trick from my (very patient) coworker Michael Sneade. Fourthly, don't get involved in a land war in Asia. (Or was it don't bring a knife to a gunfight...I can never remember. Maybe it's both. Probably both, yeah.)

And fifthly and finally...if you want people to embrace and be excited about your development platform, don't frustrate the hell out of them right out of the gate. Goodbye (at least for now) to the world of Android...let's see how quickly I can get up and running on Windows Phone 7 and XNA Game Studio.


I was finally able to get the "Hello World" sample app to run in the emulator by adding a new Windows user environment variable:
1. Start Menu > Control Panel > System > Advanced System Settings (on the left) > Environment Variables
2. Add a new user variable (at the top):
     Variable name: ANDROID_SDK_HOME
     Variable value: C:\Users\WLoescher

I good friend reminded about Google's App Inventor (still in beta), and I'll do a post about that experience's very cool!