使用在Android杰克逊DATAFORMAT-XML杰克逊、Android、XML、DATAFORMAT

2023-09-07 17:22:58 作者:淺殤

我strugling使用杰克逊DATAFORMAT-XML在Android

我有一些非常基本的code,它在Oracle JRE正常工作

  JacksonXmlModule模块=新JacksonXmlModule();
module.setDefaultUseWrapper(假);
XmlMapper xmlMapper =新XmlMapper(模块);
 

首先我想官方文档适用于摇篮(由我,不知道,如果做得正确):

 编译com.fasterxml.jackson.core:杰克逊核心:2.5.4
编译com.fasterxml.jackson.core:杰克逊 - 注释:2.5.4
编译com.fasterxml.jackson.core:杰克逊 - 数据绑定:2.5.4
编译com.fasterxml.jackson.dataformat:杰克逊DATAFORMAT-XML:2.5.4

编译组织codehaus.woodstox:woodstox核心 - 翔升:4.4.1
编译javax.xml.stream中:STAX-API:1.0-2
 

结果:摇篮失败的建造时间大约捆绑corelibraries到应用程序

  ...
:应用程序:preDexDebug
故障处理的javax / XML /流/ EventFilter.class:
不明智或核心类错误的使用(Java。*或使用javax。*)
如果没有建立一个核心库。
...
 
47001 data format error hint 解析 JSON XML 内容错误

第2次尝试试图按照肖恩的回答 (Basicly他重新包装corelibs用preFIX名和重建杰克逊DATAFORMAT-XML使用prefixed名)

 编译com.fasterxml.jackson.core:杰克逊核心:2.1.2
编译com.fasterxml.jackson.core:杰克逊 - 注释:2.1.2
编译com.fasterxml.jackson.core:杰克逊 - 数据绑定:2.1.2
//重新包装XML专用库
编译edu.usf.cutr.android.xml:杰克逊DATAFORMAT-XML-安卓2.1.2
编译edu.usf.cutr.android.xml:stax2-API的Andr​​oid版本:3.1.1
编译edu.usf.cutr.android.xml:STAX-API的机器人:1.0-2
编译edu.usf.cutr.android.xml:阿尔托-XML-安卓0.9.8
 

和建造时间未能在重复

 复制的APK META-INF /服务/ com.fasterxml.jackson.core.Object codeC复制的文件
 

这么说:

  packagingOptions {
    ...
    不包括META-INF /服务/ com.fasterxml.jackson.core.JsonFactory
    不包括META-INF /服务/ com.fasterxml.jackson.core.Object codeC'
}
 

在将其添加构建并部署,但低于STACKDUMP无法运行时除外(AFAIK它不能找到SAX供应商,即使寿它被添加到类路径,以我的理解)

  edu.usf.cutr.javax.xml.stream.FactoryConfigurationError:提供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号是正确的做法(Android不喜欢它,当你包含类的官方Java包命名空间 - 不过话又说回来了,我写了原来的答复所以我有偏见;))

我相信的FactoryConfigurationError:提供com.bea.xml.stream.MXParserFactory找不到错误是由于的在Android的错误构建工具的。在ADT的Eclipse和摇篮插件&LT previous版本; 0.7.0在 / META-INF / * 文件从JAR文件在生成过程中剥离。这似乎是> = v0.7.0根据谷歌应该不会有问题,但是从别人的报告,这听起来像它仍然可能会有问题,而且可能会删除 META-INF /服务/使用javax .xml.stream.XMLInputFactory 文件,这是需要在平台注册阿尔托。

尝试在 AOSP问题59658意见22 :

     右键点击/ src目录/主(你有/ java和/ RES文件夹),   选择新建>文件夹> Java Resources文件夹,   单击Finish(不改变文件夹位置),   右键点击新建/资源文件夹,   选择新建>目录   输入META-INF(不带引号),   右键点击 /资源/ META-INF 文件夹,   选择新建>目录   输入服务(不带引号)   您需要的任何文件复制到 /资源/ META-INF /服务   

对于您来说,在第10步以上你需要复制的这个文件(javax.xml.stream.XMLInputFactory)到 /资源/ META-INF /服务

修改

如果你得到一个错误:期间APK ...路径在归档包装重复的文件:META-INF /服务/ javax.xml.stream.XMLInputFactory,你可以试着告诉摇篮保持第一次出现有:

 安卓{
  packagingOptions {
    PICKFIRSTMETA-INF /服务/ javax.xml.stream.XMLInputFactory
  }
}
 

编辑2

此错误可能会影响PICKFIRST 。请确保您运行的是Android Studio的最新版本,并更新你的本地工具和Android摇篮插件,以确保您正在运行的是最新版本的工具。这可能是固定在机器人工作室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:

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

For you, in step 10 above you'd need to copy this file (javax.xml.stream.XMLInputFactory) into /resources/META-INF/services.

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.