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)
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;
+ (void) main___java_lang_String_ARRAYTYPE :(NSMutableArray*)n1
int _sp = 0;
XMLVMElem _op1, _op2, _op3;
for (_i = 0; _i <1; _i++) _locals[_i].o = nil;
NSAutoreleasePool* _pool = [[NSAutoreleasePool alloc] init];
_locals.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];
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);
UILabel title = new UILabel(rect);
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!
Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62