Here's a short recipe for baking JavaFX for Android dalvik. We will need just a few ingredients but each one requires special care. So let's get down to the business.
The first ingredient is an open JavaFX repository. This should be piece of cake. As always there's a catch. You probably know that dalvik is jdk6 compatible and also that certain APIs are missing comparing to good old java vm from Oracle. Fortunately there is a repository which is a backport of regular OpenJFX to jdk7 and going from jdk7 to jdk6 is possible. The first thing to do is to clone or download the repository from https://bitbucket.org/narya/jfx78
. Main page of the project says "It works in some cases" so we will presume that it will work in most cases
As I've said dalvik vm misses some APIs which would lead to a build failures. To get them use another compatibility repository which is available on GitHub https://github.com/robovm/robovm-jfx78-compat
. Download the zip and unzip sources into jfx78/modules/base.
We need also a javafx binary stubs. Use jfxrt.jar from jdk8.
The last thing to download are freetype sources from http://freetype.org
. These will be necessary for native font rendering.
I have to point out that these instructions were tested only on linux. I suppose they will work with minimal changes also on Mac OS. I also presume that you were able to build open JavaFX. That means all tools like ant, gradle, gcc and jdk8 have been installed and are working all right. In addition to this you will need to download and install jdk7, Android SDK and Android NDK for native code compilation. Installing all of them will take some time. Don't forget to put them in your path.
Unzip freetype release sources first. We will have to cross compile them for arm. Firstly we will create a standalone toolchain for cross compiling installed in ~/work/ndk-standalone-19.
After the standalone toolchain has been created cross compile freetype with following script:
It will compile and install freetype library into $FREETYPE/install. We will link to this install dir later on. It would be possible also to link openjfx font support dynamically against skia library available on Android which already contains freetype. It creates smaller result but can have compatibility problems.
Download patches javafx-android-compat.patch + android-tools.patch and patch jfx78 repository. I recommend to have look at patches. First one android-compat.patch updates openjfx build script, removes dependency on SharedSecret classes and updates LensLogger to remove dependency on jdk specific PlatformLogger. Second one android-tools.patch creates helper script in android-tools. The script helps to setup javaFX Android projects.
Now is time to try the build. Run following script:
If everything went all right the output is in build/android-sdk
UPDATE: If you want to build without jfxrt.jar binary stub comment out jmx and sample apps from settings.gradle.
Create first JavaFX Android project
Use gradle script int android-tools. The script sets the project structure for you. Following command creates Android HelloWorld project which links to a freshly built javafx runtime and to a HelloWorld application.
- NAME is a name of Android project.
- DIR where to create our first project.
- PACKAGE is package name required by Android. It has nothing to do with a packaging of javafx application.
- JFX_SDK points to our recently built runtime.
- JFX_APP points to dist directory of javafx application. (where all application jars sit)
- JFX_MAIN is fully qualified name of a main class.
Now cd to the created project and use it like any other android project. ant clean, debug, uninstall, installd will work. I haven't tried it from any IDE Eclipse nor Netbeans.
Special thanks to Stefan Fuchs and Daniel Zwolenski for the repositories used in this blog post.