JavaFX's Memo

Source: Internet
Author: User
Tags java se

Original address: http://www.infoq.com/cn/articles/Building-JavaFX-Android-Apps/

Eclipse Development JavaFX Plugin version: Http://efxclipse.bestsolution.at/install.html#all-in-one

Tutorial Address: http://code.makery.ch/library/javafx-8-tutorial/zh-cn/part6/

The original goal of the Java platform was to provide a software environment for embedded devices. However, the cycle of history makes Java a preferred language for enterprise software development. In the past, Java's client applications were much less focused than the lucrative server-side market. However, the Java platform now has a powerful client component--javafx that can be used to develop applications on desktops, tablets, mobile, and embedded systems. This article will show readers how to deploy JavaFX applications on Android devices.

Any software platform dedicated to client development requires a set of methods for creating a user interface. The AWT (Abstract Window Toolkit) was once seen as the foundation of the Java Platform user interface. Some of the more advanced toolkits, such as swing, are partly based on AWT. Since the first release of Java in 1995, AWT is part of the Java platform, and it now seems that its design principles are quite old and not compatible with today's hardware and software capabilities.

The new Java client component, JavaFX, has been redesigned with the experience of the Java domain and other UI frameworks fully absorbed. One of the key tenets of JavaFX is to maximize the use of hardware (such as GPU) resources. In fact, the toolkit required for today's user interface must be highly responsive and high-performance.

JavaFX, as the official Java client component, is an important part of the Java SE environment. On the supported systems, it is bound to the JDK and JRE. As a result, Oracle publishes JavaFX as part of the Java SE version on Windows,macos x,linux and embedded arm systems. For iOS and Android platforms, there is no officially announced JavaFX for Oracle. However, the developer community has made up the gap. The ROBOVM team is adding ROBOVM support for JavaFX so that you can use the ROBOVM compiler to compile JavaFX applications and run them on iOS devices.

Enabling JavaFX applications to run on iOS and Android platforms is critical. Today, more and more applications need to be able to run not only on desktops, but also on mobile devices and tablets. The cost of writing three versions of the same app in three languages is expensive: A desktop version, an iOS version, and an Android version. With JavaFX, you can deploy the same app directly to all three platforms. Of course, each platform has its own UI features to follow, but the JavaFX platform provides many ways to achieve this, such as using CSS and customizing skins.

Later in this article we will focus on another reason to be able to run JavaFX apps on iOS and Android platforms. Before that, let's first show you how to run an existing JavaFX application on the Android platform.

JavaFX on Android

The remainder of this article will show you how to deploy JavaFX applications on Android. Detailed instructions on how to compile, package, and deploy JavaFX applications on the Android platform can be accessed from the JavaFX migration team's website.

In general, the steps for deploying JAVAFX applications are as follows:

    1. Download the Android SDK and Javafx-android SDK

    2. Create a JavaFX app

    3. Use the Javafx-android SDK to create Android projects based on the above JavaFX app

    4. Create an Android package using the Ant build system

    5. Uploading packages to the store

First step: Download the Android SDK and Javafx-android SDK

Before compiling and building your application, you first need to install the Android SDK and the Javafx-android SDK.

The Android SDK is a software development kit provided by Google and can be downloaded from the Android Developer Support website. It contains the Android.jar API and the tools to convert Java class files into Dalvik bytecode. The Android SDK also provides tools to communicate with Android devices for log checking and transfer of applications to devices. After the download is complete, it is convenient to point the ANDROID_SDK environment variable to the downloaded Adt-bundle-xxx/sdk folder (XXX is related to the downloaded version and the corresponding operating system. )

The Dalvik javafx-android SDK (provided by the JavaFX Android transplant team) can be downloaded from the JavaFX ports project on the BitBucket website. Download and unzip the latest Dalvik-sdk-version.zip file. (We can point the environment variable DALVIK_SDK setting to the Dalvik-sdk folder that was just unzipped). The Javafx-android SDK includes a JAVAFX implementation running on the Android platform, some tools for building Android packages, and a "hello,android" JavaFX sample program. You can find this sample program in the Dalvik_sdk/samples directory that you just downloaded. In addition, we need an ant program to build the APK file. If you don't have an ant program on your computer, you can download it from the Apache Ant website.

Step two: Create a JavaFX app

Creating JavaFX apps on the Android platform is exactly the same as creating a JavaFX app on your desktop system. We can choose the usual IDE and build tools to create JavaFX apps. This way, the journey to create an Android package starts smoothly. In this step, we do not need to create a specific JavaFX Application Launcher or add any configuration. The Javafx-android SDK has provided us with the tools that we will discuss in the next steps.

While it is generally preferable to maintain the platform independence of the code, in some cases, for example, it is more convenient to take advantage of the Android platform-specific implementation when there is no corresponding JavaFX or Java API available. The Android platform provides a number of services (such as acquiring location information) that provide a variety of features for your app. To use these services, simply add a dependency to Android.jar (located in the Android SDK) and reference the classes it contains. However, it is important to note that the features specific to these Android platforms cannot be used on other systems, such as desktop systems. Also, given that desktop apps have a larger UI area than handheld devices, you need to be able to work with smaller UI areas when creating layouts for Android apps. You can look at the Helloandroid class under the Samples folder to learn how to get the boundaries of the screen and set the boundaries of the stage and scene using screen boundaries.

If you want to use the sample program to perform the above steps, use the CD command to switch to the Samples/helloworld folder under the DALVIK_SDK directory that you previously downloaded.

Package your app as a jar file. To package the sample program, you first need the CD to switch to the Dalvik_sdk/samples/helloworld/javafx directory and then build the application-used on Linux and MacOS./gradlew, Use Gradlew.bat on Windows. (We do not need to download Gradle,gradlew to complete this work for us.) Executing this command creates a jar file (Helloworld.jar) under the Javafx/build/libs folder. In addition to Gradle, we can use maven,ant or any other tool to build the app, as long as the jar file can be generated.

Step three: Use the Javafx-android SDK to build an Android project based on JavaFX applications

The Tools folder in the DALVIK_SDK directory contains a build script named "convertjavafxtoandroid.sh" that you can use to build your Android project. Simply put, the Android project is a folder that contains files and build scripts to build the Android package (APK file).

Before building the project, we needed a series of parameter configurations, including the location of the Android SDK and the Javafx-android SDK, the location of the JavaFX application, and the file name containing the main class.

We just need to make a slight change to the dalvik_sdk/samples/helloworld/convertjavafxtoandroid.sh file to get it working. At the top of the file, point the variable ANDROID_SDK to the previously downloaded Android_sdk folder. (There is currently no Windows version of the build script Convertjavafxtoandroid.bat, but we can copy the. sh file and then modify it to make a build script for a version of Windows.) For more information on building Android projects, see the Dalvik_sdk/samples/helloworld/readme file.

Before calling the convertjavafxtoandroid.sh script, you must include Build.gradle and other Gradle related files in the same folder of the script. Copy all Gradle-related files and the entire Gradle directory under the Samples directory to the root of our own JavaFX project, and make the appropriate modifications to the convertjavafxtoandroid.sh script. Then call the convertjavafxtoandroid.sh script. We can find the newly generated Android project in the Samples/helloworld/javafx/build directory.

When building a project, we can use the same pattern as the build script for the sample program. The relevant parameters are defined as follows:

-pdir: Build the folder path of the Android project. -ppackage:android package name. -pname: Generated android apk file name. -pandroid_sdk:android SDK. -pjfx_sdk:dalvik SDK. -pjfx_app:javafx the directory where the jar package is applied. -pjfx_main:javafx the class name of the main initiator.

Fourth step: Create an Android package using the Ant build system

The Android project folder "Build" that you built in the previous step contains a build.xml file. Switch to the build folder and run "Ant Debug". This command generates an Android "debug package" that can be installed on Android devices.

Use the Android tool in the Android SDK to send the above. apk file to your device. For example, Invoke command

$ANDROID _sdk/platform-tools/adb-r install/path/to/the.apk

If you need to get log information, you can execute the following command:

$ANDROID _sdk/platform-tools/adb Logcat

The perfect combination--javafx app and App Store

Another important reason for JavaFX apps to be able to run on iOS and Android platforms is this model, which is released through the store (Apple Store and Google Play store). In the past, how to publish Java applications to a wide variety of mobile phones was one of the most frustrating tasks for many Java client developers. Although there have been many standardized attempts (for example, MIDP), it is still quite difficult to deploy J2ME applications to a large number of devices without the help of device manufacturers, network operators, or both. Creating and installing apps on mobile devices is now easy. iOS and Android platforms have their own app store for developers to upload apps, and end users to download and run apps. While both the iOS App Store and the Android Play store have a range of requirements and design guidelines for applications, there are already javafx applications in both the iOS App Store and the Google Play store, which means that JavaFX-based apps can be accepted by the App Store. This creates a huge market for Java client application developers.

JavaFX's Android app is no different than any other Android app. The way you upload to the Google Play store is the same. And like other Android apps, there are a number of guidelines that need to be taken into account before uploading an app. These guidelines should not be seen as annoying dogma, but should be seen as providing help to javafx developers to keep their apps consistent with other Android apps, which makes it easier for end users to accept JavaFX applications.

Fifth step: Deploy to the store (work in progress)

Uploading the JavaFX app to the Google Play store is not very difficult, but there are many steps to follow so far. It's still a long way from writing JavaFX apps using the idiomatic IDE to submitting apps to the Play store. And you need to be familiar with all kinds of systems. If you develop JavaFX applications with MAVEN, you need 3 different build systems: Maven for application development, Gradle for creating Android projects, and Ant for building Android packages.

Some of the work aimed at improving the problem is in progress. There are many ways to change this situation, some of which are already in the research phase. For example, Android recently converted Gradle to its preferred build environment. Android's Gradle plugin makes it easier to compile JavaFX applications, but there are still a lot of problems to be solved.

In addition, the Javafxpackager released with the JDK is primarily used to provide the appropriate packages for different target environments. It would be better if this tool could be integrated in the Javafx-android SDK.

In addition, some Ides (NetBeans, Eclipse, and IntelliJ idea) already include some plugins for Android development. If you can integrate support for JavaFX in these Ides, it will be easier and more efficient for developers who are familiar with these Ides to work.

JavaFX's Android porting team chose to provide the developer with an end-to-end toolset to help them upload the app to the play store. This shows that there are no technical or legal issues preventing us from writing JavaFX applications on Android.

Next, the various parts of the end-to-end deployment will continue to evolve.

The bottom of the implementation secrets

The JavaFX platform was developed in OPENJDK sub-project--OPENJFX, and OpenJDK is often seen as the base for Java platform development. All code development and discussion topics are based on an open environment.

The OPENJFX code base contains JavaFX APIs and implementations on multiple platforms. The architecture of JavaFX itself is modular, and the platform-related parts are independent from the common ones. Considering that one of the design principles of JAVAFX is to use hardware acceleration as much as possible, it is quite difficult to achieve such a design.

The Javafx-android SDK is created based on the code in the OPENJFX code base. There are two major challenges in the migration process:

    1. The OPENJFX code contains some native code that needs to be cross-compiled to work with the Android system.

    2. The Dalvik runtime environment on Android contains only a subset of Java 7. However, support for lambda expressions is included.

The first challenge has been completely resolved in the OPENJFX. All the code required to implement JAVAFX native functionality on the Android platform is already included in OPENJFX.

The main reason why the second challenge can be solved is that OPENJFX developers have agreed not to use Java 8-specific features in the JavaFX 8u20 version. However, there are many API calls to Java 7 that still do not work on the Android platform. The good news is that the Javafx-android SDK itself already contains these missing API implementations. The RETROLAMBDA project (already included in the release package) can replace the bytecode that is dynamically called in the class file, so we can use the lambda expression in the JavaFX app on the Android platform. Also, it is important to note that Java.util.Streams is not currently supported.

Mapping of Android Concepts

The life cycle of an Android app differs from the life cycle of a typical desktop application. Android uses the concept of activity. The concept transformation between Android and JavaFX is part of the work done by the Javafx-android SDK. The Javafx-android SDK contains a subclass of activity called Fxactivity, which is instantiated when the JavaFX application starts.

The entire JAVAFX application is part of this activity. When the JavaFX application starts, JavaFX will take over everything. The benefit is that JavaFX's lifecycle events and the organization of the application, including navigation, can be like desktop applications. There is no need to know about the Android platform when it comes to lifecycle management.

In general, developers want apps to be device-independent, but in many cases they want to take advantage of Android's features. For these cases, the Javafx-android SDK provides some hooks for JavaFX developers to access the Android API. The first hook is an Android manifest file generated by the Javafx-android SDK. The default manifest file is for simpler apps, but developers can also configure them. In this file you will complete the permissions request and setup work for the underlying Android configuration parameters.

In addition, the Android platform provides many more services associated with Android devices for the JavaFX platform, such as location services, services that communicate with NFC readers, and more. In standard Android apps, these services can be accessed through the activity and context classes of Android. These Android-specific classes do not exist in the JavaFX platform. However, the Javafx-android SDK provides a static method

Context context = fxactivity.getinstance ();

Used to access a context instance associated with the activity that created and ran the JavaFX app.

The context instance returned by this static method can be used to get Android-specific services. For an example of how to use an NFC reader, refer to here. Another example similar to how to use the GPS service can be found in the open source project openmapfx.

If you have more questions about JavaFX on the Android platform, you can get more useful information from the JavaFX Android forum in Google Group.


JavaFX's Memo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.