Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Writing JavaFX Applications for Mobile Devices

Yolande Poirier-OracleJul 23 2015 — edited Oct 6 2015

by Johan Vos

With recent JavaFX ports, you can now create Java client applications for mobile devices.

Published March/April 2015

Java developers can use JavaFX to create great client applications that have a compelling user interface. By leveraging the power of the Java platform, developers can create client applications that address today’s requirements for performance, as well as look and feel. This article explores the state of JavaFX on mobile devices, with a specific focus on the Android platform.

In the past, Java client applications focused on desktop and laptop environments. That made sense, because most of the transactions started from those environments. Also, the mobile landscape was so fragmented that creating applications capable of running on most mobile devices was difficult and expensive.

Today, the situation has changed. End users often use mobile devices (smartphones and tablets) instead of laptop and desktop systems, and an increasing number of business transactions originate from those mobile devices. On mobile devices, consumers clearly prefer native applications over browser-based applications.

The mobile landscape has changed significantly over the last few years. Today, more than 90 percent of mobile devices use either the Google Android operating system or the Apple iOS operating system. Both platforms provide a convenient way for developers to distribute their applications through an “app store.” The Apple App Store accepts applications created for iOS; the Google Play Store is where Android applications should be uploaded.

The combination of a new mobile landscape and JavaFX provides a huge opportunity to Java developers. With JavaFX on mobile devices, Java developers can now use their favorite language to create powerful, modern-looking applications that work on desktops, laptops, mobile devices, and embedded systems.

Porting JavaFX to Mobile Devices

One of the key benefits of Java is the “write once, run anywhere” paradigm. JavaFX is no exception to this rule. If you write a JavaFX application, the runtime environments handle the platform-specific issues so you can focus on application-specific needs.

On desktop systems with either Microsoft Windows, Linux, or Mac OS X, the situation is as shown in Figure 1.

richclient-vos-f1.jpg

Oracle provides JavaFX implementations for Windows, Mac OS X, and Linux. The JavaFX porting efforts for mobile devices and embedded ARM systems are coordinated by JavaFXPorts, an initiative from the Java community. The codebase for JavaFXPorts is based on the code for OpenJFX, which is the official repository for the JavaFX code available at https://wiki.openjdk.java.net/display/OpenJFX/Main. One goal of JavaFXPorts is to send all the changes that are made to build JavaFX on mobile devices back to OpenJFX. This ensures convergence rather than divergence among all implementations of JavaFX.

The Android platform comes with its own virtual machine, named the Dalvik Virtual Machine (or Dalvik for short). While Dalvik is by no means a fully functional Java SE 8 implementation, it has many similarities to a Java SE 7 runtime.

As part of the JavaFX Android port, the platform-specific JavaFX implementation leverages Dalvik. Furthermore, the native parts of the JavaFX runtime are compiled for Android, as shown in Figure 2.

richclient-vos-f2.jpg

It is important to note that although Java SE 8 is not available on Android, the JavaFX 8 APIs are available. Also, the internal implementation of JavaFX 8 on Android and iOS does not use Java SE 8–specific APIs.

Moreover, it is possible to use lambda expressions, because a great tool called RetroLambda can convert them (at the bytecode level). RetroLambda inspects class files, and it replaces all invokedynamic calls with invoke calls that are supported in Java SE 7.

On iOS, the situation is a bit more difficult, because no Java runtime is available on the iOS platform. Worse, Apple has restricted the just-in-time (JIT) options that are used by typical Java Virtual Machines (JVMs) and that compile bytecode to native code at runtime.

However, RoboVM provides a smart, ahead-of-time compiler that translates Java code to native iOS code, eliminating the need for a JIT processor running on the devices. RoboVM offers more or less the same base classes as the Java classes in the Dalvik Virtual Machine, which means that at this moment, it offers a significant subset of the Java SE 7 platform. As a consequence, JavaFX 8 applications that use Java SE 7 APIs can be created on desktop environments and subsequently converted into Android or iOS applications.

In Figure 1 and Figure 2, it is clear that your JavaFX application can be executed on desktops and laptops, as well as on Android or iOS devices. One restriction: The Java runtime environments on Android and iOS do not support Java 8–specific APIs at the moment. Therefore, avoid using them in code that you want to run on mobile platforms.

Creating a JavaFX Application on Android

Writing Java code is one thing; deploying that code on mobile devices is another thing. Android applications are created by performing operations that convert Java classes and resources into an Android package (a .apk file). This Android package can be uploaded to the Google Play Store, where it can be downloaded by all Android users. While doing development, you can also attach an Android device to your development system with a USB cable and transfer the Android package to your device.

Next, see how you can create an Android package based on your JavaFX code. Note that this build process is under continuous improvement. See the JavaFXPorts website for the latest (and simplest) build instructions.

Prerequisites. Before you can build Android packages, you need to have the Android SDK. Install this SDK in a directory—for example, in /opt/android-sdk.

You also need to install the additional packages that provide the latest SDK tools, platform tools, and build tools, as explained here.

In addition, you need to install the latest javafx-for-dalvik runtime (also called the dalvik-sdk), which can be downloaded from this Bitbucket page. Unpack the zip file in a directory—for example, in /opt/dalvik-sdk.

The Dalvik SDK contains a samples directory that includes a sample JavaFX HelloWorld application, along with the required build scripts to create an Android package. You might also want to download the samples that are part of the JavaFXPorts project, which show specific scenarios. These samples are available at this Bitbucket page.

The build system. Now that you have all the required tools, you can create a JavaFX application. This article uses the Android-Gradle plugin, so I recommend using the Gradle build system. However, the Android-Gradle plugin cannot be combined with the Java-Gradle plugin in a single project. Because developers often want to run their applications on a desktop or a laptop first, it’s a good idea to use a multiproject Gradle setup.

The full article is available in Java Magazine at oracle.com/javamagazine

Comments

Hi Andreas, This is a very informative article thanks for posting! Ive been curious on what the best practices are for consuming a REST service that requires basic or other types of authentication. If the REST service is backed by SOA then we typically use OWSM policy to secure this. Also most REST services are hosted on a server different from the consuming client so there are CORS issues as well. I have not seen clear examples of these two issues being resolved in JET. thanks, Vikram

Parikshit Kumar Singh

Really Great.

Looking for more stuff like this. I have OJET in my Project.

Parikshit Kumar Singh

GITHUB URL is not working. Kindly provide us the updated one. It will improve your content for sure.

Karthik Naidu

Hey Andreas,

Couple of questions -

  1. Are you using RequireJS + KnockoutJS for client side binding(with API’s)?

    2. Can we have a custom development of the same with something like Webpack + HandlebarsJS / ReactJS / AngularJS?

1 - 4

Post Details

Added on Jul 23 2015
4 comments
31,318 views