Friday, September 26, 2014

Port JavaFX application fo Android APK, with javafxports

javafxports provide tools to build a Java(FX) runtime for Android devices. You can follow the instructions to port JavaFX Application to Android APK.

Here show how to port JavaFX Hello World to Android APK, on Ubuntu.

JavaFXAndroid
- It's assumed Android SDK is install on your system, at /home/eric/Android in my case.

- Gradle is need to create Android project from JavaFX application. To install Gradle (currently 1.4) on Ubuntu, run the command:
$ sudo apt-get install gradle


- 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: dalvik-sdk-latest. /home/eric/dalvik-sdk in my case.


- Create a JavaFX Hello World project compiled with Java 7. To specify compiled with Java 7 in Netbeans, click File (or right click the project), -> Project Properties, select Category of Sources, select Source/Binary Format of JDK 7.


- To run Gradle to generate Android project, and ant to generate APK, the following environment setting have to be set.

path to Android SDK:
$ export ANDROID_SDK=/home/eric/Android/sdk
$ export PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$PATH

Android and Java Home:
$ export ANDROID_HOME=/home/eric/Android/sdk
$ export JAVA_HOME=/home/eric/jdk1.8.0_20

- Switch to /home/eric/dalvik-sdk/android-tools, run the command to generate Android Project:

gradle -PDEBUG -PDIR=/home/eric/JavaFXAndroid -PNAME=JavaFXAndroid -PPACKAGE=com.JavaFXAndroid -PJFX_SDK=/home/eric/dalvik-sdk -PJFX_APP=/home/eric/NetBeansProjects/com.JavaFXAndroid/dist -PJFX_MAIN=com.javafxandroid.ComJavaFXAndroid -PANDROID_SDK=/home/eric/Android/sdk createProject

Where:

DIR=/home/eric/JavaFXAndroid - the output directory.
NAME=JavaFXAndroid - Name of the target Android project.
PACKAGE=com.JavaFXAndroid - package name of Java application
JFX_SDK=/home/eric/dalvik-sdk - location of downloaded JavaFX-Dalvik Runtime.
JFX_APP=/home/eric/NetBeansProjects/com.JavaFXAndroid/dist - The location of the Netbeans JavaFX project.
JFX_MAIN=com.javafxandroid.ComJavaFXAndroid - fully name of the main class of your JavaFX Application
ANDROID_SDK=/home/eric/Android/sdk createProject - location of your Android SDK.

Once successfully finished, switch to the the directory of the generated Android Project,  /home/eric/JavaFXAndroid/JavaFXAndroid in my case.

Run the command to generate the APK in its bin directory.
$ ant debug

Then you can run the adb command in bin directory to install the APK to real Android devices.
$ adb install -r -debug.apk


Run the ported Android app on real device:


4 comments:

freeforall said...

hi,
1) can you show us how it should be done in windows?
2) for gradle can we do it by using export from eclipse IDE as gradle.

Garbani MOZ said...

How To Solve Bellow Android.bat Start Error
C:\Users\Administrator\Desktop\dalvik-sdk-b10\dalvik-sdk\android-tools>gradlew -
PDEBUG -PDIR=C:\demo -PNAME=demo -PPACKAGE=com.demo -PJFX_SDK=C:\Users\Administr
ator\Desktop\dalvik-sdk-b10\dalvik-sdk\android-tools -PJFX_APP=C:\Users\Administ
rator\Documents\NetBeansProjects\com.demo\dist -PJFX_MAIN=com.demo.comdemo -PAND
ROID_SDK=C:\Users\Administrator\Desktop\adt-bundle-windows-x86-20130917\ createP
roject
:conf

====================================================
Android SDK: [C:\Users\Administrator\Desktop\adt-bundle-windows-x86-2
0130917\]
Target: [android-18]
Project name: [demo]
Package: [com.demo]
JavaFX application: [C:\Users\Administrator\Documents\NetBeansProjects\com.d
emo\dist]
JavaFX sdk: [C:\Users\Administrator\Desktop\dalvik-sdk-b10\dalvik-sd
k\android-tools]
JavaFX main.class: [com.demo.comdemo]
Workdir: [C:\demo]
debug: [true]
===================================================

:androidCreateProject FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':androidCreateProject'.
> A problem occurred starting process 'command 'android.bat''

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.

BUILD FAILED

Total time: 7.536 secs

sampletest said...

same thing here too. I tried it as u said and followed the same but i get the following error. please help me out to resolve it. i tried in windows7 with jdk8u40 gradle 2.2.



====================================================
Android SDK: [C:/SDK]
Target: [android-22]
Project name: [andyTest]
Package: [main.java.andytest]
JavaFX application: [C:/Users/Vss/Documents/NetBeansProjects/andyTest/dist]
JavaFX sdk: [D:/testProject/dalvik-sdk]
JavaFX main.class: [main.java.andytest.AndyTest]
Workdir: [C:/Users/Vss/Documents/NetBeansProjects/andyTest]
debug: [true]
===================================================

:androidCreateProject FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':androidCreateProject'.
> A problem occurred starting process 'command 'android.bat''

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get
put.

BUILD FAILED

Total time: 9.376 secs

Anonymous said...

:androidCreateProject FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':androidCreateProject'.
> A problem occurred starting process 'command 'android''

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED