Lost in translation

Google tool J2ObjC translates Java to Objective-C

Elliot Bentley

Android manufacturer releases open-source tool for translating Java code to Apple-friendly Objective-C.

Has hell frozen over? Last night, Google released an open-source command-line tool for translating chunks of Java code to Objective-C, in effect making it easier to port Android apps to iOS.

Currently “between alpha and beta quality,” J2ObjC enables Java code to be part of an iOS application’s build, without any additional editing of the generated files – well, in theory. It’s apparently already in use in several Google apps, though they warn that each new app tends to bring to light “new bugs to be fixed”.

While primarily a tool designed to help port Android apps to iOS platforms like the iPhone and iPad, there’s no reason J2ObjC couldn’t be used to translate Java written for non-Android apps to Apple platforms.

Objective-C is, of course, the language of choice for Mac OS X apps as well as iOS apps – though this is probably less interesting, considering that Java apps run pretty well on OS X without any extra tinkering.

It’s worth noting this is no magical translator for porting Android apps directly to iOS. The project’s Wiki page emphasises that J2ObjC “does not provide any sort of platform-independent UI toolkit,” and that “iOS UI code needs to be written in Objective-C or Objective-C++ using Apple’s iOS SDK,” meaning a basic level of Objective-C knowledge is still required.

J2ObjC “supports most Java language and runtime features required by client-side application developers, including exceptions, inner and anonymous classes, generic types, threads and reflection”, but the most interesting technical detail is how it handles memory management: after all, Objective-C lacks Java’s handy garbage collection.

The tool utilises one of three native OS X methods of memory management: reference counting, garbage collection, or the newer Automatic Resource Counting (ARC). A page in the wiki goes into further detail:

J2ObjC generates different memory management code, depending on which method is chosen. By default, it currently uses reference counting with autorelease pools. Translating with the -use-gc flag, though, and code that uses garbage collection is created instead. Finally, there is a –use-arc option to generate code which uses ARC; when that becomes more standard, it will become the default.

The project’s source is hosted on Google Code under an Apache License 2.0. If you have a go with it, drop a comment below with your thoughts.

Inline Feedbacks
View all comments