15 December 2008

Hello Android

... or not.

Trying to get going on Android Development, and everything's going fine. But I'm not able to get to my coveted "Hello Android" due to this message:
"Connection Failure when starting to monitor device 'emulator-5554' : device (emulator-5554) request rejected: device offline"
So my emulator is offline. Hmm. It's a file in a directory, seems unlikely that it would be offline. Impossible really. Well the emulator did fire up once, so it can't be too broken. But my first impressions of Eclipse are quite good, it's a darn nice app for being free.

I thought this part of the tutorial was pretty funny (in reference to installing a driver file for the G1, on Windows this is the standard "Found New Device" wizard):
If you're developing on Mac OS X, it just works. Skip this step.
Vs.
If you're developing on Ubuntu Linux, you need to add a rules file:
Login as root and create this file:
/etc/udev/rules.d/50-android.rules.
For Gusty/Hardy, edit the file to read:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
For Dapper, edit the file to read:
SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0bb4", MODE="0666"
Now execute:
chmod a+rx /etc/udev/rules.d/50-android.rules
I guess that's the Mac gestalt in action. Wonder what it's called on Linux? It's a sort of gestalt that's for sure. Shudder. Would watching someone type that in would give me schadenfreude? Look at all the big German words getting tossed around. Yes I had to look up the spelling. No I'm not putting in any umlauts.

Now compare the Android version of Hello World to the iPhone version:

Android:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
iPhone:
- (void) applicationDidFinishLaunching: (id) unused
{
UIWindow *window;
struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
rect.origin.x = rect.origin.y = 0.0f;

window = [[UIWindow alloc] initWithContentRect: rect];
mainView = [[UIView alloc] initWithFrame: rect];
textView = [[UITextView alloc]
initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[textView setEditable:YES];
[textView setTextSize:14];

[window orderFront: self];
[window makeKey: self];
[window _setHidden: NO];
[window setContentView: mainView];
[mainView addSubview:textView];

[textView setText:@"Hello World"];
}

Wow, just looking at the iPhone version makes my carpal tunnel flare up. Coming from a C# background, the Android version is a cakewalk. I'll reserve my "learn weird code" miles for F#.

Hello Android will have to wait, not sure what the deal is with that emulator. Google search was no help there.

9 comments:

  1. Well whaddya know: there it is on my G1: Hello Android. I just unlocked it and there's my text. I never saw an option for sending the app over to the G1 after I installed the adapter so I assumed that part wasn't working. Nifty.

    ReplyDelete
  2. You know what it takes from a MAC... "Hello Android." ta da

    ReplyDelete
  3. No question that Objective C code is funky looking as hell. Though I have to admit it kind of grows on your after awhile.

    It's been said it's more work for the coder, but easier to read and understand for someone looking at the code.

    ReplyDelete
  4. Most Mac and iPhone developers will set up the initial user interface in their application's main nib file, which will dramatically reduce the amount of code needed:

    - (void)applicationDidFinishLaunching:(NSNotification *)notification {
    [textView setText:@"Hello, world!"];
    }

    Most Mac and iPhone developers use Interface Builder rather than create their user interfaces in code. IB isn't just a newbie tool that generates bad code; it generates object archives that get deserialized and wired up at runtime, making them much more maintainable.

    ReplyDelete
  5. Your iPhone code has many issues that mislead the casual reader:

    1) Why do you set all kinds of values like text size and editable state, when you set none of them for the Android app? Does the Android TextView class not have these abilities (unlikely) or did you just decide to make the iPhone code look worse by adding random lines of configuration code?

    2) See all those lines that say "window" and "UIWindow"? In a real iPhone app, those all boil down to two lines:

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

    Of course I had to copy them since I could not type them from memory - for you see every iPhone project template includes something like them by default.

    Not to mention that those are also things only done once per application, not across multiple files.

    3) You go to all the trouble to get the hardware sized CGRect (which you didn't need) and then create a whole new CGRect with the same size by hand (480x320, which you also did not need).

    4) Last but not least the iPhone only ever has one UIWindow, but for some reason you add yet another line of code to send it to front. In front of what exactly?

    Your code is a great example of why "Hello World" examples are such a poor indication of what real development is like in any language. No modern language or platform is really simple enough anymore that doing a text box you can type in tells you anything about what you will really encounter during development.

    ReplyDelete
  6. @kgelner:

    Thanks for the input. I don't know much about iphone development so any input from an experienced developer is helpful.

    I did a google search for "hello world iphone" to get this code. I looked at a few results and picked the one which seemed simpler. The one that I picked actually said that it was a simplification of existing versions. So to your first 3 questions I can only shrug and say it was like that when I found it :)

    On your last comment (using Hello World) I totally disagree. Everyone does a Hello World when they're introducing a new language or platform. It's the first baseline that people look at. Yeah, it's not much information, but it's the "kick the tires" inspection of a new language.

    I suppose that the crappy (apparently) Hello World example that I found is a result of the absurdly restrictive NDA that the iphone launched with. Luckily, some time later, they relaxed the NDA so people could discuss development on the platform. I'll go look for a more "official" version of hello world and post a new entry.

    ReplyDelete
  7. Just wanted to post a reply letting everyone know a fix for this will not connect problem that just worked for me, especially since this was one of the first posts with the problem in it that comes up on google.
    Just ctrl-alt-del assuming its windows that breaks and stop adb.exe, its just stuck, restart eclipse and it should work fine.

    ReplyDelete
  8. what skills should i develop to be a android developer. I am a .net c# developer

    ReplyDelete
  9. How we can easily developed Android application nicely you describe in this post.By using C,C++ and JAVA we can develop this application easily.
    Android app developers

    ReplyDelete