Ray Cromwell, explained us how the application “Google Inbox” is built with GWT, and how that decision is made. To develop a multi platform application, we must face a multi programming language environment :

JavaScript is used for desktop web applications;

Java is used for Android applications;

Objective-C is used for iOS applications.

To deal with it:

Solution 1 : developing one native application per platform from scratch, but must code 3 times, test 3 times and potentially get 3 times the same bug. It is not only a productivity lost. It also makes the maintenance more difficult.

C/C++, could be compiled for various platforms, but not easy to debug/test, and there are more java/javascript developers resources;

JAVA, which run natively in Android, compiles to web with GWT and translates to Objective-C with J2ObjC. But one UI render on multi platform is still not very well for user experience;

Solution 3 (the middle ground) : sharing codes as much as possible by using a single language to reduce the “3 times” penalty. Obviously the best choice is to separate no-UI dependent code as a module for all platforms, then just one UI part per platform. To achieve this :

Serve the shared code in the clouds. Generally, the server is fast and could have a security enhance. But a stable and high quality network will be necessary, which could not be guaranteed by any telecom operator. And the offline feature won’t be available;

Use a hybrid model. UI will be built with native toolchain for each platform and the shared code will be translated from Java code and be packaged into the client side.

The “Google Inbox” application is a great example who used the hybrid model:

For Web applications : Java code was optimized and translated to Javascript by GW;

For iOs applications : Java code was translated to Objective-C by J2ObjC;

For Android applications : Java code is naturally reused.

Which proved about 60-70% of code (with tests) shared between these 3 platforms. You could find another proof of the concept on Github : https://github.com/Sfeir/jhybrid.

“Singular – Reimagining AngularJS in Java” by Daniel Kurka

Daniel Kurka showed us a new coming framework “Singular” which makes AngularJS’s features available in GWT application, and even more, and without its defeats. At first he explained AngularJS is good at developer productivity, express intend, elimination of boilerplate, application structure and easy testing. But it could have serious performance issues caused by the runtime DOM directive searching, the dirty state checking loop and no compiler optimization. To get all the good points without the bad ones, we need to:

Parse templates at compile time;

Resolve directives at compile time;

Understand the scope sufficient enough to not require dirty checking;

Make downloads smaller.

Singular, the Angular in Java is just the solution. Java gives us the great tools support, its static typing helps to understand the scope and to avoid dirty state checking. With GWT, the code will be the smallest and fastest and runs efficiently on mobile.

But there is more : Singular introduced a new inspiration of the Angular’s application structure for cross platform applications development. With Singular, we can have different view templates for different platforms:

HTML is used for Web Applications;

XML files are used for Android applications;

UIBuilder is used for iOS application;

Custom template files are used for other platforms.

And all these views share controllers across platforms. Normally, application logics are always held by controllers, and whatever the platform is, they should not change. With this design, application logics will be automatically synchronized across platforms : this is a real productivity enhancement. We will also be able to:

Use dependency injections to wire up different HTML5 APIs;

Easy integrate into classic GWT application;

Use pure Java unit test;

Work with any Java mocking framework for test.

This new templating way will greatly help us to build cross platform applications. Can’t wait to test it !

J2ObjC is a transpiler and a runtime for iOS application, who translates Java to Objective-C during build process and only focused on shared data models and business logic, not the UI code. Java class file will be compiled to object files which can be linked into iOS applications. And all comments will be removed and output content will be formatted. J2Objc is a command line tool, easy to be integrated in different build tools, such as ant, gradle and make. There has even an Eclipse plugin. To run it:

If you know Java command line style, this won’t be strange for you. J2Objc supports full Java 7 syntax and all common Java types, even Junit 3 & 4. And we can also embed Objective-C native code by using OCNI (Objective C Native Interface) style comment.

With this micro difference, one method can be comment with both JSNI and OCNI without any compilation conflict. Such a GWT compiler friendly feature, isn’t it ? Since there is no GC support on iOS, for better memory control, J2ObjC could generate ARC or manual reference counting code to release all useless objects. Typically for retention cycles, we could use @Weak and @WeakOuter to manually manage it. And there is a cycle finder tool in J2ObjC, which can help to find all cycle dependencies in source path.This project is currently used by Goolge Inbox and Google Spreadsheets. Like GWT, it is a part of the strategy to share Java code across platforms.

Conclusion

GWT is not a UI framework, but a development toolkit, which allows us to build and optimize complex browser-based applications in pure Java. It makes applications more faster and more smaller, easily integrate with any other Javascript frameworks. With the support of many existing Java tools, it makes the development and the maintenance process easier. It could be a cross platform application development tools for Web technology based solution, but it also helps to make Java become a common language for cross platform applications development.