在 android 上使用 jackson-dataformat-xml [英] Using jackson-dataformat-xml on android

查看:22
本文介绍了在 android 上使用 jackson-dataformat-xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在 android 上使用 jackson-dataformat-xml

我有一些非常基本的代码可以在 oracle jre 上正常运行

JacksonXmlModule 模块 = new JacksonXmlModule();module.setDefaultUseWrapper(false);XmlMapper xmlMapper = new XmlMapper(module);

首先我尝试了官方文档适用于gradle(由我,不确定是否正确):

编译'com.fasterxml.jackson.core:jackson-core:2.5.4'编译'com.fasterxml.jackson.core:jackson-annotations:2.5.4'编译'com.fasterxml.jackson.core:jackson-databind:2.5.4'编译'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.5.4'编译'org.codehaus.woodstox:woodstox-core-asl:4.4.1'编译'javax.xml.stream:stax-api:1.0-2'

结果:关于将核心库捆绑到应用程序中的构建时间,gradle 失败

<代码>...:app:preDexDebug处理javax/xml/stream/EventFilter.class"的问题:不明智或错误地使用核心类(java.* 或 javax.*)不构建核心库时....

第二次尝试关注肖恩的answer(基本上他用前缀名称重新打包corelibs并重建jackson-dataformat-xml以使用前缀名称)

编译'com.fasterxml.jackson.core:jackson-core:2.1.2'编译'com.fasterxml.jackson.core:jackson-annotations:2.1.2'编译'com.fasterxml.jackson.core:jackson-databind:2.1.2'//重新打包的特定于 XML 的库编译'edu.usf.cutr.android.xml:jackson-dataformat-xml-android:2.1.2'编译'edu.usf.cutr.android.xml:stax2-api-android:3.1.1'编译'edu.usf.cutr.android.xml:stax-api-android:1.0-2'编译'edu.usf.cutr.android.xml:aalto-xml-android:0.9.8'

并且构建时间因重复而失败

APK META-INF/services/com.fasterxml.jackson.core.ObjectCodec中复制的重复文件

所以补充:

packagingOptions {...排除'META-INF/services/com.fasterxml.jackson.core.JsonFactory'排除'META-INF/services/com.fasterxml.jackson.core.ObjectCodec'}

当添加它构建和部署的排除项时,但在堆栈转储下面的运行时失败(AFAIK 它找不到 SAX 提供程序,即使它被添加到我理解的类路径中)

edu.usf.cutr.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory 未找到在 edu.usf.cutr.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)在 edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:176)在 edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)在 edu.usf.cutr.javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136)在 com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:97)在 com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:85)在 com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:82)在 com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:46)

在#1 或#2 上前进的正确方法是什么?

解决方案

数字 2 是正确的方法(当您在官方 Java 包命名空间中包含类时,Android 不喜欢它 - 但话说回来,我写了原始答案所以我有偏见 ;) ).

我相信 FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found 错误是由于 Android 构建工具中的一个错误.在用于 Eclipse 和 Gradle 插件的早期 ADT 版本中0.7.0 /META-INF/* 文件在构建过程中从 JAR 中剥离.根据谷歌的说法,>= v0.7.0 似乎不应该有问题,但从其他人的报告来看,它听起来仍然可能有问题,并且可能会删除 META-INF/services/javax.xml.stream.XMLInputFactory 文件,该文件是平台注册 Aalto 所必需的.

尝试AOSP 问题 59658 评论 22:

<块引用>

  1. 右键单击/src/main(您有/java 和/res 文件夹),
  2. 选择新建 > 文件夹 > Java 资源文件夹,
  3. 单击完成(不要更改文件夹位置),
  4. 右键单击新的/resources 文件夹,
  5. 选择新建 > 目录
  6. 输入META-INF"(不带引号),
  7. 右键点击/resources/META-INF文件夹,
  8. 选择新建 > 目录
  9. 输入服务"(不带引号)
  10. 将您需要的任何文件复制到/resources/META-INF/services

对于您来说,在上面的第 10 步中,您需要复制 这个文件进入/resources/META-INF/services.如果将来文件链接被破坏,文件的名称是 javax.xml.stream.XMLInputFactory 并且它由一行组成:

com.fasterxml.aalto.stax.InputFactoryImpl

编辑

如果您收到错误:在打包 APK 过程中出现重复文件...存档中的路径:META-INF/services/javax.xml.stream.XMLInputFactory",您可以尝试告诉 Gradle 保留第一次出现:

android {包装选项{pickFirst 'META-INF/services/javax.xml.stream.XMLInputFactory'}}

编辑 2

此错误可能会影响pickFirst".请确保您运行的是最新版本的 Android Studio,并更新您的本地工具和 Android Gradle 插件以确保您运行的是最新版本的工具.这可能会在 Android Studio 1.3 RC1 中修复.

I'm strugling with using jackson-dataformat-xml on android

I have some very basic code that works fine on oracle jre

JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);

First I tried official documentation adapted for gradle (by me, not sure if done correctly):

compile 'com.fasterxml.jackson.core:jackson-core:2.5.4'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.4'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.5.4'

compile 'org.codehaus.woodstox:woodstox-core-asl:4.4.1'
compile 'javax.xml.stream:stax-api:1.0-2'

Result: gradle fails build time about bundling corelibraries into an application

...
:app:preDexDebug
trouble processing "javax/xml/stream/EventFilter.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
...

2nd attempt trying to follow Sean's answer (Basicly he repackages corelibs with prefix names and rebuilds jackson-dataformat-xml to use the prefixed names)

compile 'com.fasterxml.jackson.core:jackson-core:2.1.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.1.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.1.2'
// Repackaged XML-specific libraries
compile 'edu.usf.cutr.android.xml:jackson-dataformat-xml-android:2.1.2'
compile 'edu.usf.cutr.android.xml:stax2-api-android:3.1.1'
compile 'edu.usf.cutr.android.xml:stax-api-android:1.0-2'
compile 'edu.usf.cutr.android.xml:aalto-xml-android:0.9.8'

And build time failed on duplicates

Duplicate files copied in APK META-INF/services/com.fasterxml.jackson.core.ObjectCodec

so added:

packagingOptions {
    ...
    exclude 'META-INF/services/com.fasterxml.jackson.core.JsonFactory'
    exclude 'META-INF/services/com.fasterxml.jackson.core.ObjectCodec'
}

When adding the exclusions it builds and deploys, but fails runtime on below stackdump (AFAIK it cant find the SAX provider, even tho it is added to the classpath to my understanding)

edu.usf.cutr.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found
            at edu.usf.cutr.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)
            at edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:176)
            at edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)
            at edu.usf.cutr.javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136)
            at com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:97)
            at com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:85)
            at com.fasterxml.jackson.dataformat.xml.XmlFactory.<init>(XmlFactory.java:82)
            at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:46)

What is the proper way to move forward on either #1 or #2?

解决方案

Number 2 is the correct approach (Android doesn't like it when you include classes in the official Java package namespace - but then again, I wrote the original answer so I'm biased ;) ).

I believe the FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found error is due to a bug in the Android build tools. In previous versions of ADT for Eclipse and Gradle plugin < 0.7.0 the /META-INF/* files are stripped from the JARs during the build process. It seems like >= v0.7.0 shouldn't have the problem according to Google, but from others' reports it sounds like it still may be problematic, and could potentially remove the META-INF/services/javax.xml.stream.XMLInputFactory file, which is required for the platform to register Aalto.

Try the workaround mentioned in AOSP issue 59658 comment 22:

  1. right click on /src/main (where you have /java and /res folders),
  2. select New > Folder > Java Resources Folder,
  3. click Finish (do not change Folder Location),
  4. right click on new /resources folder,
  5. select New > Directory
  6. enter "META-INF" (without quotes),
  7. right click on /resources/META-INF folder,
  8. select New > Directory
  9. enter "services" (without quotes)
  10. copy any file you need into /resources/META-INF/services

For you, in step 10 above you'd need to copy this file into /resources/META-INF/services. In case the file link is broken in the future, the name of the file is javax.xml.stream.XMLInputFactory and it consists of a single line:

com.fasterxml.aalto.stax.InputFactoryImpl

EDIT

If you get a "Error:duplicate files during packaging of APK... Path in archive: META-INF/services/javax.xml.stream.XMLInputFactory", you can try telling Gradle to keep the first occurrence with:

android {
  packagingOptions {
    pickFirst 'META-INF/services/javax.xml.stream.XMLInputFactory'
  }
}

EDIT 2

This bug may be affecting "pickFirst". Please make sure you're running the latest version of Android Studio, and update your local tools and Android Gradle plugin to make sure you're running the most recent version of the tools. This may be fixed in Android Studio 1.3 RC1.

这篇关于在 android 上使用 jackson-dataformat-xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆