JavaFXPorts jfxmobile-plugin没有找到特定于平台的目录

问题描述 投票:1回答:1

我有一个基于JavaFX的界面,我正在尝试使用JavaFXPorts在多个平台上构建。大多数代码都是独立于平台的,但我有一些实用程序可以调用特定于平台的代码,例如适用于Android与桌面上的文件系统位置。例如,在下面的MCVE中:使用gradle desktopgradle android构建,我得到:

src/main/java/com/example/project/MyApp.java:30: error: cannot find symbol
        platformGreeting = new Label("hello, " + Util.getPlatform());
                                                 ^
  symbol:   variable Util
  location: class MyScene

但是如果我将两个源文件(MyApp.javaUtil.java)放在任何源目录(maindesktopandroid)中并运行相应的任务,它将构建。

在我看过的例子中,他们总是在src/main下实现一个独立于平台的接口,然后在每个特定于平台的目录中实现它,但我不明白为什么这是必要的。

├── build.gradle
├── settings.gradle
└── src
    ├── android
    │   └── java
    │       └── com
    │           └── example
    │               └── project
    │                   └── Util.java
    ├── desktop
    │   └── java
    │       └── com
    │           └── example
    │               └── project
    │                   └── Util.java
    └── main
        └── java
            └── com
                └── example
                    └── project
                        └── MyApp.java

的build.gradle:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.3.10'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

mainClassName = 'com.example.project.MyApp'

repositories {
    jcenter()
}

dependencies {
}

jfxmobile {
    ios {
        forceLinkClasses = [ 'com.example.project.**.*' ]
    }

    android {
        applicationPackage = 'com.example.project'
    }
}

settings.gradle:

rootProject.name = 'project'

的src / main / JAVA / COM /例子/项目/ MyApp.java:

package com.example.project;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;

public class MyApp extends Application {
    private Scene scene;

    @Override public void start(Stage stage) {
        stage.setTitle("MyApp");
        scene = new Scene(new MyScene(), 750, 500);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class MyScene extends Region {
    final Label platformGreeting = new Label("hello, " + Util.getPlatform());

    public MyScene() {
        getChildren().add(platformGreeting);
    }

    @Override protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(platformGreeting, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
    }

    @Override protected double computePrefWidth(double height) {
        return 750;
    }

    @Override protected double computePrefHeight(double width) {
        return 500;
    }
}

SRC /桌面/ JAVA / COM /例子/项目/ Util.java:

package com.example.project;

public final class Util {
    private Util() {
        throw new UnsupportedOperationException();
    }

    public static final String getPlatform() {
        return "desktop";
    }
}

SRC /安卓/ JAVA / COM /例子/项目/ Util.java:

package com.example.project;

public final class Util {
    private Util() {
        throw new UnsupportedOperationException();
    }

    public static final String getPlatform() {
        return "android";
    }
}
java gradle javafxports
1个回答
1
投票

顺便说一下,源集在JavaFXPorts中定义,main由所有平台共享,然后每个特定包由给定平台使用,即仅在桌面上使用desktop

由于main代码由所有平台共享,因此它不能包含特定的平台代码,并且它并不真正了解添加到任何平台的平台代码。

这就是为什么在尝试将Util添加到main时得到编译异常的原因。

解决方案1

第一种方法可以用Class.forName(className)完成。在运行时,它将尝试在类路径中查找给定的类。

所以这最初应该工作:

    try {
        Class<?> util = Class.forName("com.example.project.Util");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(BasicView.class.getName()).log(Level.SEVERE, null, ex);
    }

但是你有一个问题:你不能真正在主包中投射到Util

一种可能的解决方案是使用反射:

    try {
        Class<?> util = Class.forName("com.example.project.Util");
        Object newInstance = util.newInstance();
        Method method = util.getDeclaredMethod("getPlatform");
        String platform = (String) method.invoke(newInstance);
        System.out.println("Platform: " + platform));
    } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        Logger.getLogger(BasicView.class.getName()).log(Level.SEVERE, null, ex);
    }

为方便起见,我修改了Util

public class Util {

    public Util() {
    }

    public final String getPlatform() {
        return "desktop";
    }
}

虽然这种方法非常好,但使用起来并不容易。

解决方案2

所以让我们在main包中添加一些中性API,我们可以更容易地调用它:一个与我们感兴趣的方法的接口:

public interface Platform {

    String getPlatform();
}

现在让我们在我们的平台类中实现它:

public class Util implements Platform {

    public Util() {
    }

    @Override
    public final String getPlatform() {
        return "desktop";
    }
}

现在事情变得容易了,因为现在你可以得到一个Platform实例,并调用getPlatform()

    try {
        Class<?> util = Class.forName("com.gluonhq.forname.Util");
        Platform platform = (Platform) util.newInstance();
        System.out.println("Platform: " + platform.getPlatform());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(BasicView.class.getName()).log(Level.SEVERE, null, ex);
    }

解决方案3:魅力下降

Charm Down是一个open source library,提供不同的本机服务,中间的API可以在main包中使用,基于所需的平台特定代码(Java for Desktop,Android或Objective-C for iOS)。

使用您的IDE的Gluon插件,并创建一个使用jfxmobile插件的项目将包括Charm Down中包含的一些服务,您可以在build.gradle文件中看到:

jfxmobile {
    downConfig {
        version = '3.7.0'
        plugins 'display', 'lifecycle', 'statusbar', 'storage'
    }
    ...
}

如果你看看如何实现这些服务,有一个核心插件有几个classes

  • Platform
  • Services
  • ServicesFactory ...

现在,在您的项目中,如果包含Charm Down依赖项,则可以使用Platform.getCurrent().name()

System.out.println("Platform: " + Platform.getCurrent().name());

不仅如此,您还可以通过以下方式获得任何服务:

Services.get(DisplayService.class)
            .ifPresent(display -> System.out.println("Resolution: " + display.getScreenResolution()));

我建议你看看这些类是如何实现的。并检查不同的服务available,以便您不再执行它们。许多可用的样本here使用这些服务中的一个或多个。

如果你想实现一个新的,我建议你看看Go Native样本(codetutorial)。

© www.soinside.com 2019 - 2024. All rights reserved.