STATIC.x

Kunal Dawn

Building and deploying JavaFX Applications on Android

Leave a comment

Prerequisites

What do you need in order to build and deploy JavaFX Applications on Android?

  1. You need the Android SDK. You can download this here. The SDK is sufficient if you only want to develop applications (in the .apk format). If you want to install the applications on devices, or run emulators, you need to download the ADT bundle. Make sure to remember where you download it into.
  2. You need a JavaFX-Dalvik Runtime. You can either download it here (easy) or build this yourself (less trivial). Always download the latest version of the runtime. The latest version is currently dalvik-sdk-b4.
  3. You need a regular JavaFX Application, compiled with Java 7.
  4. You need the gradle buildsystem (on Linux, Gradle 1.4 is reported to be successful, on Windows, Gradle 1.9 has been used with success)

Android application development using the The downloadable version of the Runtime has been reported to be successful on Linux, MacOS and Windows.

On Windows, make sure you have include gradle\bin, adt\sdk\tools, jdk7\bin on your path

Create an Android project

The JavaFX-Android-Runtime has a directory “android-tools”. This directory contains a gradle build file (build.gradle), that allows you to create an Android Project based on an existing, regular JavaFX Application. You don’t need to write any code in this project. Everything will be created for you. You need to supply a number of parameters to gradle:

  • NAME is the name you choose for your Android project.
  • ANDROID_SDK is the location of the downloaded Android SDK. If you downloaded the whole ADT, you should point here to the sdk directory inside the adt
  • DIR is the output directory, the .apk will be in this place. A directory named NAME will be created in this outpur directory
  • PACKAGE is package name required by Android. It has nothing to do with a packaging of javafx application, but you can use the same name
  • JFX_SDK points to the JavaFX-Android Runtime you created or downloaded
  • JFX_APP points to dist directory of your regular javafx application. (where all application jars sit)
    You must use Android API level 19 or higher from the latest Android SDK, if any of your class files have been compiled for java 7. (see ANDROID_SDK parameter above; the build script automatically picks the highest installed API level). Otherwise your class files must be compiled for java 6.
  • JFX_MAIN is fully qualified name of a main class, the starting class for your JavaFX Application
  • DEBUG can be provided as well, if you want your application with debugging enabled (useful for inspecting stacktraces)

Also, the android command, which can be found in the Android SDK must be on your path.

Run gradle createProject with the specified parameters, e.g.

gradle -PDEBUG -PDIR=/home/user/work -PNAME=HelloAndroidWorld -PPACKAGE=com.helloworld \
-PJFX_SDK=/home/user/android/android-sdk -PJFX_APP=/home/user/NetBeansProjects/HelloAndroidWorld/dist \
-PJFX_MAIN=com.helloworld.HelloAndroidWorld createProject

On Windows platforms, make sure to use forward slashes / in ANDROID_SDK and the gradle build command instead of backslash \

For clarity, an example script (createproject.sh) has been added in the creator directory.

Build the Android Package

cd into the directory containing the project, which is in the DIR/NAME directory. In that directory, simply run

ant debug

The Android packages will be created in the bin directory

Install the application on your device

The application currently will not work on virtual devices, because some opengl extensions required by openjfx are not supported by the emulator. You must use a real device.
The Android ADT bundle contains a tool called adb that allows you to communicate with real devices or android emulators. Installing a package on a device is done by

adb install -r path/to/package-debug.apk

If you want to have a look at what is happening, you can read debug statements via

adb logcat

Leave a comment