Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,843 members, 7,810,254 topics. Date: Saturday, 27 April 2024 at 02:47 AM

Porting Javafx Application To Android [tutorial] - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Porting Javafx Application To Android [tutorial] (3622 Views)

A Simple Tic-tac-toe Game Using Javafx / Get Your Web And Android Tutorial Videos @ Rivers/Port Hacourt / Javafx or Swing? (2) (3) (4)

(1) (Reply) (Go Down)

Porting Javafx Application To Android [tutorial] by ugwum007(m): 9:04pm On Nov 11, 2016
In this post, I will write on one of the ways/methods of porting JavaFX application to Android. If you have a good idea about this topic, feel free to drop some points.
Re: Porting Javafx Application To Android [tutorial] by ugwum007(m): 9:09pm On Nov 11, 2016
To accomplish this goal, you will need the following:

- Java 7 or 8 SDK (JDK7 or JDK8)
- Android SDK
- JavaFX Dalvik SDK
- Gradle
- Ant
- Visual Studio Code
- Android Phone
Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 9:28pm On Nov 11, 2016
my other account was just banned now.

- Download JDK 7 or 8 from oracle website and install.
Add it to your Environment Variables.

- Download Android Studio IDE, Android SDK comes with it
Add the Android Studio SDK path to your Environment Variables.
Add the Android Studio SDK/tools path to your Environment Variables.

-Download JavaFX Dalvik SDK and unpack the Zip file to a good location on your system, preferrably under C:/

- Download Gradle and unpack the Zip file to good location
add the path to your Enivironment Variables.

- Download ant and unzip to a good location
add the path to your Enivironment Variables.
Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 9:32pm On Nov 11, 2016
Run this commands to verify if everything is ok

Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 9:37pm On Nov 11, 2016
-Create a Simple JavaFX application:

package fxandroid;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @author STEINACOZ-PC
*/
public class FXAndroid extends Application {

@Override
public void start(Stage primaryStage) {

Text text = new Text();

TextArea textArea = new TextArea();

Button btn = new Button();

Circle circle = new Circle();

VBox vb = new VBox();
vb.setPadding(new Insets(20,20,20,20));

text.setText("Testing JavaFX port with ant"wink;

circle.setFill(Color.RED);
circle.setRadius(60);

btn.setText("PRINT"wink;

btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
textArea.setText("testing JavaFX application on Android"wink;
circle.setFill(Color.GREEN);
}
});
vb.getChildren().addAll(text,btn,textArea,circle);
ScrollPane root = new ScrollPane();
root.setContent(vb);


Scene scene = new Scene(root, 350, 550);

primaryStage.setTitle("JAVAFX PORT"wink;
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

Clean and build to create the jar file in the dist folder.


We will be using only JavaFX Main class for this tutorial. FXML and FXML Controller will be added in the next tutorial.

From the code and the Image, the application should explain itself.

Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 9:53pm On Nov 11, 2016
Open the Visual Code Studio (Am using it as an Editor). Add this code to build an Android gradle project from the JavaFX project (FXANDROID).


set ANDROID_SDK=C:/Users/STEINACOZ-PC/AppData/Local/Android/sdk
set WORKDIR=C:/Users/STEINACOZ-PC/Documents/portingFX
set JFX_SDK=C:/dalvik-sdk-latest/dalvik-sdk
gradlew.bat --info --stacktrace createProject -PDEBUG -PDIR=%WORKDIR% -PPACKAGE=com.fxandroid -PNAME=FXANDROIDTUT -PANDROID_SDK=%ANDROID_SDK% -PJFX_SDK=%JFX_SDK% -PJFX_APP=C:/Users/STEINACOZ-PC/Documents/NetBeansProjects/FXAndroid/dist -PJFX_MAIN=fxandroid.FXAndroid

Name the file createFXANDROID.bat



it is actually a windows batch file.

-the first line sets the path to the android SDK installed in the system
-second line sets the output folder where the android gradle project will be created
-third line sets the path to the JavaFX dalvix SDK (where you unpacked it)
-the fourth line is the calls gradlew.bat shipped with JavaFX dalvix SDK, createProject command is called with the following parameters passed to it:
-PNAME = name of the android project and name to be created
-PPACKAGE = name of the android package to be created
-PJFX_APP = location of the JavaFX application jar file
-PJFX_MAIN = name of the JavaFX Main Class prefixed by the package name

Save the createFXANDROID.bat file in the same location JavaFX dalvix SDK, under android tools. Double click to run.


go to the output folder to see the new android gradle project

Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 10:10pm On Nov 11, 2016
open command prompt, use cd command to enter into the new JavaFX android project.

use the command: ant debug

or

ant clean debug

This will create a debug-version (not signed) of the Android application in the bin subdirectory of the directory.

The Android application can now be transferred to a real Android device connected to your development system,
by calling $ANDROID_SDK/platform-tools/adb install -r bin/FXANDROIDTUT-debug.apk

if adb is part of the android sdk you downloaded.

Re: Porting Javafx Application To Android [tutorial] by steinalb(m): 10:15pm On Nov 11, 2016
if your usb port is broken like mine, transfer the apk to the android phone and install.

Be mindful of the footprint. JavaFX Desktop Applications have bigger footprints than their Android counterparts.

Re: Porting Javafx Application To Android [tutorial] by lekropasky(m): 6:38am On Nov 12, 2016
Wow, interesting. I have a question, What are the limitations? Why does the native development with Android SDK is better?
Re: Porting Javafx Application To Android [tutorial] by ugwum007(m): 3:11pm On Nov 13, 2016
lekropasky:
Wow, interesting. I have a question, What are the limitations? Why does the native development with Android SDK is better?

Am back from my usual ban. Although am finding it difficult to understand your question but from the little I got.
One of the main limitations from my own point of view is the footprint i.e that is the changing from desktop size to mobile size. JavaFX yet doesn't support different screen responsiveness like html and co. It becomes a headache getting the right dimension for Android screens (mobile 720*1280 is not the same with desktop 720*1280).

Another one is that Android doesn't support Java 8 yet so you have to stay away from Java 8 specific API like lambda, stream etc but a plugin retrolambda takes care of lambda during the built.

Apart from that, other limitations should have work around like using Android specific API, you can import Android.jar from Android sdk into your project.

(1) (Reply)

Beginner At Web Development / Let's Talk About OpenAI ChatGPT. What's The Future of Developers ? / MATLAB: The Language Of Technical Computing

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.