
האם ידעת שאתה יכול להשתמש ב- Java כדי ליצור יישומים ניידים חוצי פלטפורמות? כן, צבטו את עצמכם, קראתם את זה בפעם הראשונה! אלמד אותך את היסודות כיצד להשתמש בידע הקיים שלך ב- Java ליצירת אפליקציות ביצועיות באנדרואיד וב- iOS בשלבים פשוטים. נעשה את כל זה באמצעות JavaFX כערכת הכלים של GUI.
אבל ראשית, עוד קצת חזית. יהיה עליך לעמוד בדרישות הבאות כדי להיות מסוגל לבנות אפליקציה עבור שניהם אנדרואיד ו- iOS. עם זאת, אם אינך מעוניין לבנות יישום iOS, אתה מוזמן לפתח בכל מכונת x64 סיביות שתומכת ב- Java SE 8. פרויקט זה יהיה מאגר Git שנבנה עם דרגה. אך אינך צריך ליצור מאגר Git.
להלן הדרישות :
- JVM 1.8 תואם ל- JDK
- כלים לשורת הפקודה של Android (SDK v.27)
- XCode 9.2
- דרגה 4.2
- Git אחסון קבצים גדול (v.2.5.0) (מיותר אם אינך רוצה ליצור מאגר git)
- רצוי לפחות 4G RAM
חסר סבלנות? רוצים לראות תוצאה סופית? בדוק את הפרויקט שהושלם להלן.
afinlay5 / OnyxFx
מאגר קוד מקור של Gradle עבור OnyxFx, פלטפורמה חוצה פלטפורמות (אנדרואיד / iOS / Linux / macOS / Windows) יישום JavaFX טיוח ... github.com
סביבת הפיתוח שלי תהיה Fedora Linux 28 ו- macOS High Sierra. עכשיו כשישינו את זה מהדרך, בואו נחפור פנימה.
1) צור תיקייה לאכסון הפרויקט
אירחתי את הפרויקט שלי, OnyxFx, באופן הבא: " / home / adriandavid / Projects / OnyxFx". אתה, כמובן, חופשי לארח את הפרויקט בכל מקום שתרצה.

2) אתחל את הדרגה, גיט, הגדר את JAVA_HOME
פתח מסוף בשורש ספריית הפרויקט. אם דרגה מוגדרת כראוי, אתה אמור לראות משהו כזה לאחר ביצוע הפקודה הבאה:
gradle -v

עליכם לוודא כי הדרגה מפרטת את ההתקנה של ערכת הפיתוח של Java (JDK) 8 הסמוכה לסעיף שכותרתו "JVM".
אמנם ישנן דרכים רבות לעשות זאת, הדרך הכי פשוטה היא להבטיח שהמשתנה הסביבתי JAVA_HOME מוגדר כראוי.
בהתאם לסביבתך, ישנן דרכים רבות לעשות זאת. אחת הדרכים לעשות זאת ברוב * סביבות nix היא להגדיר את המשתנה בקובץ /home//.b ash rc או / etc / pro . עיין במדריך למערכת ההפעלה שלך כדי להבטיח שהמשתנה הסביבתי JAVA_HOME מוגדר כהלכה.
תוכל לכלול את השורות הבאות בסוף .bashrc או בפרופיל כדי להבטיח כי JAVA_HOME מוגדר כהלכה.
JAVA_HOME=/home/adriandavid/java/oracle_jdk1.8.0_181/export JAVA_HOME
הערה: תוכל להתקין את JDK 8 של אורקל כאן.
לאחר מכן, ודא שהקליפה משקפת את השינויים שלמעלה על ידי הפעלת אחת מהפקודות הבאות:
source ~/.bashrcsource /etc/profile
הזן את הפקודה הבאה כדי לוודא שהמשתנה מוגדר כהלכה:
echo $JAVA_HOME

אם אתה עדיין נתקל בקשיים או אם אתה משתמש ב- Microsoft Windows, ראה כאן.
ראשית, הפעל git init
בספריית הבסיס של הפרויקט כדי לאתחל את מאגר Git. הערה: אם לא תרצה לארח מאגר git, תוכל לדלג על שלב זה.
שנית, הפעל gradle init
בספריית הבסיס של הפרויקט כדי לאתחל את מאגר הדרגות. שלב זה נדרש.

הערה: תבחין שהדוגמה שלי נראית שונה במקצת. הסיבה לכך היא שכבר דרגתי את דרכי וגיט התחיל את הסביבה המקומית שלי.
3) קבל גרובי! ערוך את gradle.build ו-
אני מקווה שכדור הארץ, הרוח והאש יכולים לעזור לכם להתלהם! הפעל את עורך הטקסט המועדף עליך, וערוך את build.gradle הממוקם בספריית הבסיס של הפרויקט שלך והחלף את התוכן בתוכן של תמצית ה- GitHub הבאה.
הגדרות build.gradle אלה מגדירות את פרויקט הדרגה שלנו לשימוש בתוסף javafxmobile , שהוא סוס העבודה של הפרויקט שלנו. תוכלו ללמוד עוד על התוסף כאן וכאן. בין דברים רבים, התוסף javafxmobile ממכן את תהליך ההורדה (מ Maven Central או jcenter) והוספת ה- SDK של iOS ו- Android לפסי הכיתה של היישום שלך.
אם אתה מכיר דרגה, ערבה או נמלה, נהדר - כנראה שיש לך מושג מה קורה. אם אינך מכיר את הדרגה, אל תדאג . כל מה שאתה צריך להבין הוא כי דרגה היא כלי בנייה המשמש לאוטומציה של משימות רבות הכרוכות בבניית אפליקציות כגון: תופס תלות, ארגון פרויקטים וכו '.
שימו לב שאנחנו מכוונים לאנדרואיד 7.1 נוגט (API גרסה 25) ו- iOS 11 (נראה איפה זה נעשה בקרוב). אתה יכול להתאים את הערכים האלה לפי רצונך. שים לב, עם זאת, במקרה של אנדרואיד, עליך לוודא שגרסת ה- API תואמת לגירסת ה- SDK שהורדת (עוד על כך בהמשך).
לבסוף, לא אדגים את ייצור ההפעלה החתומה במדריך זה. מסיבה זו, iOSSkipSigning מוגדר נכון ואינו לעשות שימוש releaseAndroid משימת gradle. אתה יכול, עם זאת, לספק את ההתאמות המתאימות לייצור אפליקציות חתומות.
4) צור קובץ חדש בשם gradle.properties והגדר אותו
צור קובץ חדש בספריית הבסיס של הפרויקט שנקרא gradle.properties
והוסף את התוכן הבא לקובץ.
robovm.device.name=iPhone-7robovm.sdk.version=11.0org.gradle.jvmargs=-Xms4g -Xmx8g
הגדרות אלה אומרות לתוסף javafxports להשתמש ב- iPhone-7 כאמולטור הלוח, למקד ל- iOS 11 ולהעביר את דגלי ה- Xms ו- Xmx ל- JVM, המציין הן את מאגר הזיכרון הראשוני ל -4 GB והן את זיכרון הערימה המרבי. בריכה ל- 8GB. זה יהיה הכרחי עבור הידור ה- openJDK ופיתוח ה- iOS build.
5) התקן את Homebrew (iOS בלבד)
אם אין לך מקינטוש ואינך מתכוון לייצר גירסת iOS, אל תהסס לדלג על שלב זה.
פתח את המסוף ב- macOS והדבק את הפקודה הבאה.
/usr/bin/ruby -e "$(curl -fsSL //raw.githubusercontent.com/Homebrew/install/master/install)"
6) התקן את שקע ה- USB מרובב (iOS בלבד)
עבור לשלב זה רק אם Homebrew הותקן בהצלחה. אם אין לך מקינטוש ואינך מתכוון לייצר גירסת iOS, אל תהסס לדלג על שלב זה.
פתח את המסוף ב- macOS והדבק את הפקודה הבאה.
brew install usbmuxd
7) תפוס את כלי שורת הפקודה של Android
Grab Android Command Line Tools for your platform here. After the download has finished, unzip the folder and paste the contents in the directory of your choice. For me, this was /home//A
ndroid.

8) Set Android_HOME, Grab necessary Android packages
As with Java, gradle needs to know where to look to find the Android Command Line Tools. There are a few ways to do this. However, in the spirit of simplicity and consistency, we will set the ANDROID_HOME environmental variable in this tutorial. Add the following variable in the same way that we did it for JAVA_HOME. For example:
ANDROID_HOME=/home/adriandavid/Android/ export ANDROID_HOME
Remember to reload the shell by adding source
Now, grab the tools necessary to build the Android build. Execute the following command:
# *.nix./sdkmanager "platform-tools" "build-tools;25.0.3" "platforms;android-25" "extras;android;m2repository" "extras;google;m2repository"
or
#Windowssdkmanager "platform-tools" "build-tools;25.0.3" "platforms;android-25" "extras;android;m2repository" "extras;google;m2repository"
Take careful notice that the SDK and API version we have specified in gradle.build correspond to the version we have specified in this command. Namely, “25”. Should this be misaligned, the build will not succeed.
9) Create the application’s directory structure
To automate the process of creating these directories, execute the following shell script.
Bourne-Again Shell / Korn Shell:
Windows Shell (cmd):
Save the file as mkpdir.bat or mkpdir.sh and execute the file from the project’s root directory as root (or Administrator).
# *.nixchmod +x mkdir.sh-sh ./mkpdir.sh
# Windowsmkpdir
Notice that we created directories for embedded and desktop. We will produce a desktop build, because it takes no additional work to do so. However, we will not produce any builds for embedded devices.
10) Create your JavaFX Application!
Navigate to /src//java and begin developing your JavaFx application! Application resources are stored in /src//resources.
You can start with a simple Hello World application or look at the source code that I have put together here. OnyxFx is an application I made, following these instructions, that makes REST calls over HTTP to the OnyxFxAPI. The API, in turn, is a web scraper that will return the statistical data (points per game, rebounds per game, assists per game) for the NBA® player and season the client specifies. It returns the data as JSON, which is then parsed and displayed to the screen of the mobile app. Feel free to edit it!
Keep in mind that although you can share source code, you should include custom edits in each copy of the source, should you want to make device specific changes.
Also note that the underlying compiler (MobiDevelop’s fork of RoboVM) does not fully support all Java 8 APIs. If you look very closely at my source code, you will notice that in the iOS version of the source code, I have removed unsupported API such as java.util.function.BiConsumer and java.util.Map.replace().
11) Create a RAM disk for iOS builds (iOS only)
The compilation process for iOS is very resource-heavy, as the plugin will compile the entire openJDK and other libraries twice to create a fat JAR that it will use to build your application. Therefore, you should preemptively create a RAM disk to accommodate for the memory requirements.
This step, however, is subject to your judgement of your machine’s capabilities. For context, the macOS machine that I used to compile my iOS app has 4GB of DDR2 RAM. I decided to make an 8GB RAM disk. To do so, execute the following command in the terminal.
SIZE=8192 ; diskutil erasevolume HFS+ ‘RoboVM RAM Disk’ `hdiutil attach -nomount ram://$((SIZE * 8192))`
12) Build and Run your application!
To build your application, execute the gradle wrapper in the root directory from the terminal as follows.
./gradlew clean build
This will produce a desktop application packaged as a JAR with scripts to run the application provided in
/build/distributions/
; and /build/distributions/

Notice that in /bin there are scripts to execute the application. These scripts rely on preserving the current folder structure. Also notice that is not necessary for you to have tree installed. It is used here simply for illustrative purposes.
There is, additionally, a standalone JAR that you can use to execute the application on any desktop environment supporting JavaFX 8. To run the application, execute one of the following:
# Navigate to /build/distributions//
#On *.nixcd bin./
#On Windowscd bin
#Platform agnosticjava -jar OnyxFxMobile.jar (or double click, if jvm is configured to run .jar files)
Note: If the executable providing "java" is not the same vendor and/or version of the Java 8 JDK with which you built this application, the jar may not run. JavaFX 8 builds between the openJDK & Oracle JDK are incompatible.
Otherwise: /location/to/java8/bin/java -jar
View this project’s gradle tasks
You can view this project’s gradle tasks by running the following in the project’s root directory.
./gradlew tasks
To Compile, Run on Desktop
The following command will run your project in the host environment.
./gradlew jar./gradlew run
You will find a standalone jar in build/libs/
t;.jar .
To Compile, Run on Android
./android #Generates a debug Android apk containing the JavaFX application.
./androidInstall #Launch the application on a connected android device.
./androidRelease #Generates a release Android apk containing the JavaFX application.
Note: You will need to configure a valid signingConfig when releasing an APK (javafxports).
You will find two APKs in build/javafxports/android
.
The first will be named
t;.apk.
The second will be named -u
naligned.apk.
To Compile, Run on iOS
./createIpa - Generates an iOS ipa containing the JavaFX app.
./launchIOSDevice - Launches app on a connected ios device.
./launchIPadSimulator - Launches app on an iPad simulator.
./launchIPhoneSimulator - Launches app on an iPhone simulator.
You will find three executables in build/javafxports/ios
.
The first will be named
t;.ipa.
The second will be named
ame>.dSYM.
The third will be named &
lt;AppName>.app.
Some screenshots of my sample app
On Desktop

On Android

On iPhone

On iPad

Splash Screen

My Closing Thoughts
javafxports is a promising project that aims to bring JavaFX and the Java SE platform onto mobile and other devices. In a way, the tool parallels Xamarin in its efforts. However, the project has a lot of work left to be done.
For a start, the plugin currently does not fully support Java 8. On Android, it uses retrolambda to handle Java 8 Lambda Expressions & Method References. It technically is up to Java 6. Additional dependencies make it such that you can use Java 8. However, the process is straightforward, the builds work as expected, and the compilation time is not too long.
On iOS, however, the builds are extremely memory-intensive and the compilation process takes a very long time. The following is a snippet of the log for ./gradlew createIpa task.
:createIpa (Thread[Task worker for ‘:’,5,main]) completed. Took 1 hrs 46 mins 40.198 secs.
In total, the process consumed about over 6GB of RAM on my machine. This is hardly ideal. However, the future is promising. A company called Gluon has developed a high performance, fully modular custom JVM fully supporting Java 9, that you can read more about here.
This article is originally published on the blog section of my homepage, here.
Resources to explore:
- JavaFxMobile Plugin Git Repo: //github.com/javafxports/javafxmobile-plugin
- JavaFxPorts Documentation: //docs.gluonhq.com/javafxports/#_how_it_works
- JavaFxPorts Homepage: //javafxports.org/
- Gluon Documentation: //gluonhq.com/developers/documentation/
- Google Groups Page for JavaFxPorts: //groups.google.com/forum/#!forum/javafxports
- StackOverflow Page for JavaFxPorts: //stackoverflow.com/questions/tagged/javafxports
- Gluon Mobile Pricing/License Options: //gluonhq.com/products/mobile/buy/