dcsimg

Open Source iPhone Dev: XML to the Rescue?

Can a project originating from San Francisco State University bridge the digital divide and bring true open source development to the iPhone platform? It looks promising, but the climb is steep and the gap is wide.

Last week I ventured into a discussion of iPhone development and open source, initiated by a specific set of tasks I need to accomplish for a project. My initial research lead me to an open source graphics and gaming library, cocos2d-iphone. Very cool functionality, but unfortunately we were still stuck in Mac land, working in X-Code. I personally didn’t mind too much because I already had the gear from an iPhone project I worked on last year for a time-tracking application.

This new project was requiring some graphics programming that I wasn’t in the mood to write from scratch, so finding some open source code libraries to help bring home the project without having to reinvent the wheel was a huge relief. However, using Objective-C and X-Code is non-trivial for the uninitiated, not to mention the cost. It would be nice to develop iPhone applications without the Mac and the Objective-C investments.

Well, spurred on by some folks who made it known that they were unhappy with the prospect of having to purchase (and learn to use) an expensive Mac in order to develop for iPhone, I set out to determine if in fact one can develop iPhone applications in Eclipse and the Java programming language. After all, who wants to learn a new language if they can get the job done with an old and trusted friend, namely Java.

I would love to be able to say, yes, we can develop full-featured iPhone applications in Eclipse and Java. On the other hand, a simple “no” might bring some relief also — you know, it just can’t be done so don’t lose sleep over it and move on with your life. However, like so many things in life, the answer is a bit of a shade of gray. The answer is more no, than yes at present, but the topic is worth exploring and perhaps some folks will be inspired enough to lend a hand and take this open source iPhone thing from a “sort-of” to a “yes”.

What is an iPhone application anyway?

Before jumping in any further in our quest to conduct open source iPhone development, we need to make some distinctions about what how we define iPhone applications. Some iPhone applications can be written as “web applications” — meaning web apps that are happily rendered under Safari Mobile, iPhone’s powerful browser based on (open source) WebKit. Prior to the iPhone SDK being released, this was all the craze amongst developers pining away to have their application run on the iPhone. Lots of articles and tutorials have been written about this, not to mention a few books.

For the most part, these resources are discussing web development skills, with particular attention given to style sheets and how to code for an environment that has no notion of “mouse-over” events, among other nuances. All of this is good and useful, and without wanting to take anything away from these intrepid (web) developers, that’s just not what we’re talking about here. When we discuss iPhone applications, we mean native, installed on the device, needs-no-Internet-connection-to-get-there kind of applications. OK, I feel better now, let’s roll up our sleeves and see what we can do to create native iPhone applications with Java.

Objective-C from Java. Can we get there from here?

iPhone employs the Cocoa Touch framework for its run-time environment — that is all of the user interface relies on classes in Cocoa Touch and a whole host of classes derived from “NSObject”. NSObject is an Objectice-C “thing” and at the end of the day, iPhone simply does not run a Java VM. So no matter how clever your Java code is written, it just is not going to run on iPhone. That is, you cannot run Java byte-codes on an iPhone. So the question is, can we create Objective-C binaries from Java source code so we can run our applications on iPhone? Well, it can be done with the help of an open source tool-chain. XMLVM to the rescue!

An XML based Virtual Machine

XMLVM stands for XML Virtual Machine. This project, the brainchild of Arno Puder and his team at San Francisco State University, is one of those clever, “why didn’t I think of that” kind of projects. XMLVM is an open source project whose aim is to provide a bridge between disparate compile-time and run-time environments — in a word cross-compiling. Okay, so that is my interpretation of things, but I think fairly accurate. XMLVM’s approach is to let your favorite Java compiler (i.e. javac) compile your source code into Java class files (i.e. byte codes). Once the class file is available, the XMLVM tool-chain goes to work by parsing the class file and representing the code in a textual xml file. Once the code is written out as an xml document, it is transformed into a specific target language via an XSLT transform. Pretty cool. So we can write a program in Java, compile it to a class file and then convert it to Objective-C! Here is the classic Hello World application written in Java and converted to a language neutral xml representation:

public class HelloWorld
{
  static public void main(String[] args)
  {
    System.out.println("Hello World");
  }
}

So what does this look like in Objective-C? Buckle up – here it is:

@interface org_xmlvm_test_HelloWorld : java_lang_Object
+ (void) main___java_lang_String_ARRAYTYPE :(NSMutableArray*)n1;
@end

@implementation org_xmlvm_test_HelloWorld;
+ (void) main___java_lang_String_ARRAYTYPE :(NSMutableArray*)n1
{
    XMLVMElem _stack[2];
    XMLVMElem _locals[1];
    int _sp = 0;
    XMLVMElem _op1, _op2, _op3;
    int _i;
    for (_i = 0; _i <1; _i++) _locals[_i].o = nil;
    NSAutoreleasePool* _pool = [[NSAutoreleasePool alloc] init];
    _locals[0].o = n1;
    _op1.o = [java_lang_System _GET_STATIC_java_lang_System_out];
    _stack[_sp++].o = _op1.o;
    _stack[_sp++].o = @"Hello World";
    _sp -= 2;
    [((java_io_PrintStream*) _stack[_sp].o) println___java_lang_String:_stack[_sp + 1].o];
    [_pool release];
    return;
}
@end

Oh my! So that version of HelloWorld doesn’t do much for us. In fact, println in general doesn’t help for an iPhone application. Let’s see what a graphical version of HelloWorld looks like, again taken directly from the examples shown at the xmlvm.org website.

public class HelloWorld extends UIApplication
{
  public void applicationDidFinishLaunching(NSNotification n)
  {
    UIScreen screen = UIScreen.mainScreen();
    CGRect rect = screen.applicationFrame();
    UIWindow window = new UIWindow(rect);

    rect.origin.x = rect.origin.y = 0;
    UIView mainView = new UIView(rect);
    window.addSubview(mainView);

    UILabel title = new UILabel(rect);
    title.setText("Hello World!");
    title.setTextAlignment(UITextAlignment.UITextAlignmentCenter);
    mainView.addSubview(title);

    window.makeKeyAndVisible();
  }
}

Now for those who are familiar with Cocoa Touch, you will recognize a whole bunch of familiar class names such as UIApplication, UIScreen, UIWindow, UIView, etc. XMLVM’s team has ported a subset of Cocoa Touch to Java so we can write applications in Java, using the CocoaTouch classes. This is all pure Java – these applications will build and run directly from Eclipse. What’s more, they run in a simulated iPhone/iPod touch framework. Now, before you get too excited, this is not the real iPhone emulator, but rather a mock-up designed to assist in testing your application. It is possible to run your applications on the iPhone emulator, but for that you would need a Mac…

The good, the bad and you

So the good news is that we can write applications for iPhone in Java using Eclipse! The bad news is that in order to get the applications to run on a real device, you still require a Mac and X-Code in order to properly sign the applications using Apple’s code-signing tools. Without the code-signing step, applications will not run on normally provisioned (i.e. non jail-break) devices. Running applications on jail-broken devices is possible, but not discussed here. So where do you fit in? The Cocoa Touch/Objective C library ports are not complete. There is absolutely no shame in that for the XMLVM team — that is a huge code base to port and they have already done quite a bit of heavy lifting. They have done a great job of trailblazing a path for using Java for iPhone applications and I am aware of at least one other programmer who has spent some time extending their work.

If this XMLVM project is of interest, I would encourage you to check out the sources from sourceforge and contact the XMLVM team. Go ahead and check the code out via svn — it is worth the look just to get an appreciation for the effort involved.

The bigger picture

Here is the really exciting part of their work — we’re not talking about just iPhone, but Android, Ajax and more! So far we have discussed iPhone and Java. The XMLVM team has a broader ambition than just iPhone development — they are really working towards a general cross-compilation capability. If you are at all familiar with the tool-chain used by Android, you will know that the Android tool-chain has a similar approach to XMLVM — that is, code is written in Java and then the byte code is converted from java class files into byte codes for the Dalvik Virtual Machine. Sounds similar to XMLVM’s approach, right? Well, another element of the XMLVM tool-chain is a cross-compiler to convert Java based Android applications to iPhone.

XMLVM provides an Android compatibility library which allows Android applications to be compiled into Objective-C to create native iPhone applications. Let me say that another way — imagine if you could write an application for Android and deploy it to both the Google App Market and Apple’s iTunes AppStore — just one code base. Not only can the application be run on both an Android and iPhone device, but because the application compiles as pure Java, it can also be run as an applet. Write once, run in at least three places. Thank you XMLVM for bringing that old line of “Java – write once, run everywhere” closer to reality. Check out the image below:

XMLVM is an innovative, open source project worthy of further exploration. Perhaps the best way to embark on that path is to listen to Arno Puder himself talk about the project via a video available on their website (http://www.xmlvm.org).

If this topic is of interest to you, please let me know. Linux Magazine is going to be issuing a developer survey in the near future to learn more about what is of interest to our readers, so please be on the look out for it and take the thirty seconds it takes to answer the questions — we look forward to your feedback!

Comments on "Open Source iPhone Dev: XML to the Rescue?"

dford

Very interesting article – thank you
David

biglinuxguy

Nice article, but it brings up one of the issues with software development these days, i.e., the unwillingness to learn new tools or languages. While I understand that people become comfortable with a language or development environment, it\’s a sad state of affairs that they\’ll spend more time porting and munging things so that they don\’t have to learn a new language or toolchain than it would take to learn the language or toolchain. That being said, I guess some folks just have a lot of time on their hands to fiddle around with that sort of thing.

fableson

Good point, biglinuxguy. I just thought the project was clever and the idea of write once, publish multiple places is very appealing. You see this with ports of games for PC, game consoles, etc. I have never done anything in that world, but I have assumed it is always at the source level. Source level portability and mobile development just don\’t fit on the same page — so this is really a novel (?) approach to translate the byte codes. What makes this a tough sell is the effort involved in porting the native libraries into compatible apis. Android (presentl) is probably lower hanging fruit than iPhone (Cocoa Touch).

I personally love learning new platforms – its probably more a pathology than anything else on my part. The second project is always fun – the first is just pure pain.

Thanks for your comments.

curtiscu

@biglinuxguy: \”it\’s a sad state of affairs that they\’ll spend more time porting and munging things so that they don\’t have to learn a new language or toolchain than it would take to learn the language or toolchain\”

errrr, Java?

softweyr

@curtiscu: errrr, Java?

Nice for the platforms Java runs on, useless for platforms that don\’t support Java. Yes, Apple should have a JVM of some sort on the iPhone, and Java class libraries for the iPhone interfaces, but they don\’t. Ditto for Palm, Windows Mobile, etc.

One of the reasons Android is such a revolution is because the Android SDK just works, and runs on just about anything. Xcode for the iPhone isn\’t a barrier for me, since I have 2 more Macs that people in my household, but it\’s a lot easier to find programmers that know Java, or want to know Java, than Objective-C. Amazing as the App Store is, imagine how much more they would have if Apple lowered the barrier to entry another step or two.

jameskoch

Don\’t like Java? See the PhoneGap project; HTML, JavaScript, and CSS. Similar goals; alternate technology choice.

jillemash

With iSpectrum, you get access to a complete set of iPhone APIs in Java : UIKit, CoreLocation,…
You can not only develop in Eclipse but also debug your app in it, while launching the iPhone SDK simulator. It\’s super easy to port your pre existing java code to iPhone (e.g. MIDlets).
Plus iSpectrum is free for open source project.
-> http://www.flexycore.com

Hello there, You have performed an excellent job. I will definitely digg it and in my view suggest to my friends. I am confident they will be benefited from this web site.

Woah this blog is excellent i really like studying your posts. Stay up the good paintings! You realize, many persons are hunting around for this information, you can aid them greatly.

Fantastic web site. Plenty of useful information here. I am sending it to a few buddies ans also sharing in delicious. And certainly, thank you in your effort!

Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your blog posts. Can you suggest any other blogs/websites/forums that go over the same topics? Thanks!

I am just commenting to let you understand what a useful experience our princess experienced reading through yuor web blog. She mastered too many pieces, not to mention how it is like to possess a wonderful coaching character to have many people with no trouble know just exactly a variety of hard to do matters. You truly exceeded our expected results. Many thanks for producing those invaluable, healthy, informative and even fun guidance on this topic to Julie.

Hello there! I know this is kinda off topic but I was wondering which blog platform are you using for this website? I’m getting fed up of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

You got a very fantastic website, Glad I observed it through yahoo.

I see something genuinely interesting about your web site so I saved to bookmarks.

obviously like your website however you need to check the spelling on quite a few of your posts. A number of them are rife with spelling issues and I in finding it very troublesome to inform the truth on the other hand I’ll surely come back again.

Hi baby
The article is very helpful for me,i like it,thanks a lot!
Wholesale 68204a Oakley Sunglasses ID8210185 http://www.fleetsale.ru/new-arrival-oakleys-202.html

My brother recommended I might like this website. He was once entirely right. This post actually made my day. You can not imagine just how a lot time I had spent for this info! Thanks!

Leave a Reply