【发布时间】:2017-06-30 11:59:39
【问题描述】:
我正在尝试使用 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文件夹可供您使用,但您应该将xml 文件放在src/main/resources下,然后使用getClass().getResources("/config.xml").toExternalForm()加载文件。 -
你好 José,你能告诉我应该把代码放在哪里吗:getClass().getResources("/config.xml").toExternalForm()
-
如果你只需要文件的url作为字符串,你可以
String configFile = AppConfig.class.getResources("/config.xml").toExternalForm(),其中config.xml放在/src/main/resources/config.xml。 -
谢谢!它工作得很好:)
-
好的,我会发布它作为答案然后
标签: java android javafx config javafxports