简体   繁体   English

有没有办法将Gluon Mobile(javafxports)与本机android代码混合?

[英]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. 例如:我想将我的javafx桌面应用程序的核心(使用Gluon Mobile)迁移到Android,但仅迁移到核心。 Other parts (excluding core) of application i want to implement using native android code. 我要使用本机android代码实现的应用程序的其他部分(核心部分除外)。

So, is there a way to keep Gluon Mobile and Android native codebase in single project ? 因此,有没有办法将Gluon Mobile和Android本机代码库保留在单个项目中?

As a rule of thumb, you shouldn't require specific platform code in your Java project. 根据经验,您在Java项目中不需要特定的平台代码。 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. 如果您只想提供基于平台的自定义Java代码或自定义样式,则可以使用Platform.getCurrent().name() ,然后基于该平台做出反应,例如使用不同的代码或基于以下内容加载不同的CSS文件:平台和设备类型。

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, ie the device's camera 利用本机服务,即设备的摄像头
  • make use of a native API, ie Android's Log 利用本地API,即Android的Log
  • make use of a native widget/layer, ie Android's Toast or SurfaceView 利用本机窗口小部件/图层,即Android的Toast或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. 对于第一种情况,访问本地服务,请看一下Charm Down ,以及不同服务的本地实现。

As you'll notice, each service contains both Java and specific platform code. 您会注意到,每个服务都包含Java和特定的平台代码。 The main plugins (ie 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 (ie charm-down-plugin-storage-desktop-3.6.0.jar for Desktop or (ie charm-down-plugin-storage-android-3.6.0.jar for Android). 主要插件(即charm-down-plugin-storage-3.6.0.jar )包含中性API,可在您的Java项目中稍后使用,并且它将调用您所运行的平台的实现(即charm-down-plugin-storage-desktop-3.6.0.jar适用于台式机)或(例如, charm-down-plugin-storage-android-3.6.0.jar适用于Android)。

The service will be called from the Java main package: 该服务将从Java主程序包中调用:

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 . 例如, AndroidLogService实现使用了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. 即使您不需要服务,而只是访问Android API,也可以按照上面的示例那样遵循Charm Down的设计模式,并提供您的Android实现。

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. 至于Android小部件,除非创建本机层,否则您将无法使用它们,要注意将小部件和该层都放置在JavaFX层的顶部。 For instance, the Video service for Android does exactly this. 例如,适用于Android的视频服务就可以做到这一点。

Finally, there is another alternative, in case your project is basically an Android project and you want to call JavaFX from it. 最后,如果您的项目基本上是一个Android项目,并且您想从中调用JavaFX,则还有另一种选择。 A sample of this is the Kokos sample . Kokos 样本就是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM