uptoyou
uptoyou

Reputation: 1797

Is there a way to mix Gluon Mobile (javafxports) with native android code?

For example: i want to migrate the core of my javafx desktop app (using Gluon Mobile) to Android, but only core. Other parts (excluding core) of application i want to implement using native android code.

So, is there a way to keep Gluon Mobile and Android native codebase in single project ?

Upvotes: 1

Views: 828

Answers (1)

José Pereda
José Pereda

Reputation: 45456

As a rule of thumb, you shouldn't require specific platform code in your Java project. If you just want to provide a custom Java code or custom styling based on the platform, you can use Platform.getCurrent().name() and then react based on it, for instance using different code or loading a different css file based on platform and type of device.

Nonetheless, there are several reasons to include platform code, and since you don't specify which is your requirement, I'll assume one of the following:

  • make use of a native service, i.e. the device's camera
  • make use of a native API, i.e. Android's Log
  • make use of a native widget/layer, i.e. Android's Toast or SurfaceView

For the first case, accessing a native service, have a look at the Charm Down library, and the native implementation for the different services.

As you'll notice, each service contains both Java and specific platform code. The main plugins (i.e. charm-down-plugin-storage-3.6.0.jar) contain neutral API that can be used later on from your Java project, and it will call the implementation for the platform you are running on (i.e. charm-down-plugin-storage-desktop-3.6.0.jar for Desktop or (i.e. charm-down-plugin-storage-android-3.6.0.jar for Android).

The service will be called from the Java main package:

Services.get(StorageService.class).ifPresent(storage -> {
     storage.getPrivateStorage()
         .ifPresent(file -> System.out.println("Private storage " + file.getName()));
     });

In this way, you can run it in any platform.

If you require a service that is not available yet, you can create a new service. A sample on how to do it can be found here. For instance, the AndroidLogService implementation makes use of android.util.Log.

Even if you don't need a service, but just accessing the Android API, you can follow the design pattern of Charm Down as in the sample above, and provide your Android implementation.

As for Android widgets, you won't be able to use them unless you create a native layer, taking care of laying both the widget and the layer on top of the JavaFX layer. For instance, the Video service for Android does exactly this.

Finally, there is another alternative, in case your project is basically an Android project and you want to call JavaFX from it. A sample of this is the Kokos sample.

Upvotes: 2

Related Questions