BT

InfoQ Homepage News Google's Recipe to Code Sharing across Android, iOS, and the Web

Google's Recipe to Code Sharing across Android, iOS, and the Web

Bookmarks

Garrick Toubassi, Google Inbox engineering director, has recently explained how his team could get to "sharing roughly two-thirds of their client code" across three platforms: iOS, Android, and the web. The key is a clear separation of concerns between UI code and UI-independent logic, and a couple of tools that Google developed through the years.

Google's approach aims to build "separate native apps that fit seamlessly into each of our respective target platforms," says Toubassi, in an efficient way. Indeed, in an app like Inbox there are a number of components, such as for network communication management, caching, local persistent storage, etc., that is not reasonable having to implement and maintain separately on all three platforms.

What Google does is implementing just once the data model and the application logic in Java, and on top of that a native UI layer for each platform. The data layer and application logic have a twofold responsibility:

  • Modelling concepts specific to Inbox, such as Conversations, Reminders, Contacts, and Labels.

  • Providing a fully observable data model allowing binding to the user interface (UI) layer.

Those foundations are then "transpiled" to native code for each platform using a couple of Google's own tools:

  • Google Web Toolkit (GWT) to translate the Java data model into JavaScript for the web platform;
  • J2ObjC to translate the Java data model into JavaScript for the iOS platform.

GWT is a long-standing open source set of tools that allow developers to develop and debug Ajax applications in Java. When the application is deployed, the GWT cross-compiler translates the Java code to standalone JavaScript. GWT also allows to embed JavaScript code directly into Java code, using Java comments.

J2ObjC is a more recent tool that Google describes as being "between alpha and beta quality". It does not include any cross-platform UI toolkit. As Toubassi remarks, a tricky point when going from Java to Objective-C is going from garbage collection to reference counting. This is handled in J2ObjC through autorelease pools: the equivalent of garbage-collecting a set of objects in Java is draining the pool they were allocated to in Objective-C. One specific problem with this, says Toubassi, is related to the possibility of reference cycles, which are effectively handled through specific Java annotations mapping to weak Objective-C properties.

Talking to Ars Technica's Ron Amadeo, Toubassi stated that this approach has no detectable impact on performance and that the J2ObjC-generated code has "roughly the same number of objects and the same object graph complexity" as a hand-built Objective-C version.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Your message is awaiting moderation. Thank you for participating in the discussion.

    test

  • J2ObjC does Java to Objective-C

    by Igor Kolomiets /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Correction: "J2ObjC to translate the Java data model into JavaScript for the iOS platform" should be: "J2ObjC to translate the Java data model into Objective-C for the iOS platform."

  • Java to iOS

    by Mirko Nasato /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    An interesting alternative to J2ObjC seems to be RoboVM.

    While J2ObjC translates Java source code to Objective C, RoboVM translates JVM bytecode.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.