Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JavaFX app with external libraries in Android?

I just developed a JavaFx application which use RXTX library for serial communication and in the future will use Bluecove for bluetooth. It was my understanding that there are some ways to run a JavaFx app on Android devices, so I began my research.

The first option was running the "jar" packaged app with the Android app "Java Manager". The problem here is that I wouldn't know what to do with the external libraries, since there isn't a standard JVM on the android devices where I could place them. I found this project "http://v-lad.org/projects/gnu.io.android/", but it seems it's oriented to Android applications.

So when I found "http://javafxports.org/", I tried to make my first steps with it. This project seems to be what I need, but I'm a newbie in Android and I find documentation a little bit confusing, so I'm not sure where to start. Moreover, I'm still not sure that I could use those java libraries in Android with this approach.

Does anyone know if what I pretend is doable?? In that case, which steps should I follow??

like image 273
Juan Avatar asked May 12 '15 10:05

Juan


People also ask

Can you use JavaFX in Android?

Yes you can run JavaFX application on iOS, android, desktop, RaspberryPI (no windows8 mobile yet). Work in Action : We did it!

Can JavaFX be used for mobile applications?

JavaFX on Mobile and EmbeddedJavaFXPorts is the open source project that brings Java and JavaFX to mobile and embedded hardware, including iPhone, iPad, Android devices, and the Raspberry Pi. JavaFXPorts can be used on its own, but it's even better combined with Gluon Mobile.


1 Answers

You can already try JavaFXPorts plugin. It's in continuos development, but the recent versions are mature enough to use it without problems.

Have a look at the basic requirements to get started here. You will need JDK8u40+, Gradle 2.2+, Android SDK and Android Build tools.

Once you have everything ready, you can try the samples.

Also I suggest you have a look at Gluon-plugin for NetBeans. Basically it will create a new empty JavaFX project for you, and you will be able to build it and deploy it on your desktop, on Android and on iOS platforms.

Have a look at the build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b4'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
}

mainClassName = 'org.test.javafxports.TestJavaFX'

jfxmobile {
    ios {
        forceLinkClasses = [ 'org.test.javafxports.**.*' ]
    }
}

First of all, you just need to update the plugin version. Check here for the last one: 1.0.0-b8.

Build and run on your desktop, or run the tasks like androidInstall to generate the apk and deploy it on your Android mobile.

Once you have tested it, and everything is working properly, you can start adding the code of your project.

And back to your question, yes, you can add any third party jar to the project.

Basically you just need to add the dependency on the gradle file. You can use compile or runtime with local files or from external repositories:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b8'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
}

dependencies {
    compile files('lib/<your local jar>.jar')
    compile 'org.glassfish:javax.json:1.0.4'
    androidCompile 'org.glassfish:javax.json:1.0.4'
}

mainClassName = 'org.test.javafxports.TestJavaFX'

jfxmobile {
    ios {
        forceLinkClasses = [ 'org.test.javafxports.**.*' ]
    }
}

Note you can add dependencies that only are added to one single platform, like androidCompile or androidRuntime.

Since the apk will run on Dalvik VM, only Java 7 features are allowed. You can use lambdas, though, since the plugin uses Retrolambda project internally on your code. Be aware that it is not applied on the added jars.

As an example of using jars on your project, you can use Gluon-Charm-Down open source library, that already provides access to some native services on your device, like local storage or GPS.

dependencies {
    compile 'com.gluonhq:charm-down-common:0.0.1'
    androidRuntime 'com.gluonhq:charm-down-android:0.0.1'
    desktopRuntime 'com.gluonhq:charm-down-desktop:0.0.1'
}

In fact, with jfxmobile plugin and this library, the 2048FX game has been successfully ported to Android (Google Play) and iOS (Apple Store).

like image 57
José Pereda Avatar answered Oct 01 '22 19:10

José Pereda