Before we announced Gluon VM a couple of months ago, we had been working on a number of components, experimenting with a number of options, and building up a vision for Gluon VM. In the months since announcing Gluon VM, we can now say that we are confident enough about our approach that we can share a few details.
The goal of Gluon VM is to allow developers to use Java 9 functionality in their applications, and to deploy those applications on mobile iOS and Android devices using the popular app stores. Gluon VM therefore forms a fundamental layer of the Gluon Mobile solution for writing cross-device applications purely in Java while leveraging the platform services in a platform-agnostic manner.
Before Gluon VM, the Java on Mobile solutions offered by Gluon were based on the Android libraries for Android, and on the Harmony classes provided by the deprecated and unmaintained Apache Harmony project for iOS.
Standing on the shoulders of giants: OpenJDK
As we stated from the very beginning, we base our efforts on OpenJDK. By leveraging OpenJDK, we ensure that we are fully aligned with the latest and greatest versions of Java, and that we are 100% compatible with Java Virtual Machines for desktop and server purposes.
The OpenJDK repository contains the code and build steps necessary to create Java JRE’s for the typical desktop platforms. A year ago, Oracle announced the OpenJDK Mobile project. In this project, patches are created and maintained that enable building the VM and core class libraries for iOS and Android. This of course require changes that are dependent on specific CPU/OS configurations.
On iOS, the situation is even more complex, since it is not allowed by Apple to use dynamic libraries or generated code on iOS devices. In a typical Java environment, the HotSpot Just In Time (JIT) Compiler will compile Java bytecode to native code at runtime, in order to speed up execution. Apple does not allow this on iOS devices. As a consequence, more changes are needed to the OpenJDK codebase. The OpenJDK Mobile project fixes this by using the Zero Interpreter instead of the HotSpot JIT. The Zero Interpreter will interpret bytecode instructions and execute them, rather than translate them to native methods at runtime. There is no (zero) assembly code involved in the Zero Interpreter, so it works on different OS/CPU combinations without much porting efforts.
The Gluon VM leverages the work done in the OpenJDK Mobile project.
Gluon VM Status Update
We have an end-to-end demonstration of a few JavaFX Applications that are being executed on iOS devices by the Zero Interpreter. These demos convinced us the choice of OpenJDK is the best approach. From a functional point, nothing changes between a Java 9 desktop application and a Java 9 mobile application.
Although the Zero Interpreter already does a number of optimisations, the difference between interpreted bytecode and native code is significant. In order to overcome this performance issue, we started working on a clean-room Ahead Of Time compiler. This AOT compiler compiles Java bytecode into native code. A Java method is thus translated to a native function. All translated native functions used by an application are packed into a static library, which is bundled with the VM and executed on the client — or, uploaded to the appropriate app store.
We made some minor modifications to the OpenJDK Mobile VM. Before delegating control to the Zero Interpreter, we now check if we have a compiled (native) function for the requested Java method. If so, we execute that native function. If not, we still use the Zero Interpreter for this function. This approach allows us to gradually improve the AOT compiler. At this moment, our AOT compiler translates a number of Java methods into native functions, but not every possible method body is yet supported.
While this is still very much laboratory work, we are making great progress. The combination of the AOT and the OpenJDK Mobile based VM is what we call Gluon VM. We are very excited about this combination, as it allows to run standard Java 9 code in a performant way on mobile devices. At JavaOne, Johan Vos and Eugene Ryzhikov will show this concept in the session CON6466 titled “Java on Mobile Devices”. We look forward to seeing you there.