Tuesday, October 23, 2012

A Reason to Write Android Apps First

If you work on a mobile app and you're planning on developing it on Android and iOS, it may be less work for you to write it on Android first. Google recently released a new tool that makes porting Java code to iOS much easier.
The project (j2objc) can be found here.

To give the program a quick test run, I ran it using this Java file:

public class hello {

public static void main(String[] args) {

System.out.println("To Objective C we go!");

}

}

After running the above code using j2objc, I received two files (as expected): the header file, hello.h, and the source file, hello.m. The source file looks like this:

//

//  Generated by the J2ObjC translator.  DO NOT EDIT!

//  source: hello.java

//

//  Created by Isaac on 10/18/12.

//

 

#import "IOSObjectArray.h"

#import "hello.h"

 

@implementation hello

 

@end

 

 

int main( int argc, const char *argv[] ) {

int exitCode = 0;

NSAutoreleasePool *pool = [[NSAutoreleasePool         alloc] init];

IOSObjectArray *args = J                               reEmulationMainArguments(argc, argv);

 

NSLog(@"%@", @"To Objective C we go!");

 

[pool release];

return exitCode;

}

J2Objc certainly has to make a few decisions when converting between the two languages. One is seen above, as System.out.printLn is converted to NSLog. Currently J2Objc can convert much of Java, including things like reflection and anonymous classes in Java. I imagine these kinds of decisions are made through the program.

Although the library can't touch any specific Java code related to the Android APIs, it can handle anything written in pure Java. The tool certainly has potential, especially for converting standard java classes directly to their Objective C equivalent.

No comments:

Post a Comment