在 javafxports android 项目中发布查找配置文件

Issue finding configuration file in javafxports android project

我正在尝试使用 javafxports 从 javaFX 项目构建一个 android 应用程序。目前我在移动设备上安装并启动它时遇到问题,它抛出 ConfigurationException。 这是使用文件的代码:

public class AppConfig extends XMLConfiguration {

/**
 * 
 */
private static final long serialVersionUID = 550015250032006413L;
private static AppConfig instance;
private static String configFile = "src/main/assets/config.xml";

// Singleton initialiser
static {
    instance = new AppConfig(configFile);
}

/**
 * Constructor
 *
 * @param fileName Configuration file name.
 */
private AppConfig(String fileName) {
    init(fileName);
}

/**
 * Initialize the class.
 *
 * @param fileName Configuration file name.
 */
private void init(String fileName) {
    setFileName(fileName);
    try {
        load();
    } catch (ConfigurationException configEx) {
        configEx.printStackTrace();
    }
}

这是 gradle 文件:

    apply plugin: 'org.javafxports.jfxmobile'



buildscript {
    repositories {
        jcenter()
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases' }
    }

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



repositories {
    jcenter()
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
}

mainClassName = "com.example.amladzhov.irisandroid.sample.Client.StartApplication"

jfxmobile {
    android {
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }

        dependencies {

            compile 'commons-configuration:commons-configuration:1.6'
            compile 'org.apache.commons:commons-exec:1.3'
            compile 'commons-discovery:commons-discovery:0.5'
            compile 'org.apache.axis:axis:1.4'
            compile 'javax.xml.rpc:javax.xml.rpc-api:1.1.1'
            //compile 'wsdl4j:wsdl4j:1.6.2'
            compile 'org.jdom:jdom:2.0.2'
            compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.1'
            compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'
        }
    }
}

文件位于

src/main/assets/config.xml

您不能指望 xml 文件会在运行时放置在 src 文件夹下。

但请注意,main 包下的 assets 文件夹未导出到 Android,因此该文件将无法以任何方式使用。

虽然有一个 src/android/assets 文件夹可用于放置 Android 所需的资源,但需要 Android API 才能阅读它。

最好的选择是将 xml 文件放在 src/main/resources 下,然后像在 Java 中一样使用 getResources().

加载文件

所以将文件放在 src/main/resources/config.xml 下,并替换为:

private static String configFile = "src/main/assets/config.xml";

有了这个:

private static String configFile = AppConfig.class.getResources("/config.xml").toExternalForm();