本文介绍了在Android中集成org.apache.poi和javax.xml.stream.*包(stax-api)-如何在Android Studio中设置--core-library参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio 1.5.1

I'm using Android studio 1.5.1

我想在我的android项目中包含org.apache.poi-ooxml库.要包含该库,我需要包含一些其他库依赖项,其中包括stax-api库.

I'd like to include the org.apache.poi-ooxml library in my android project. To include that library I needed to include some other library dependencies, among which the stax-api library.

stax api的问题在于它具有javax.*中的所有软件包,而后者是一个核心库". Java jdk包含所有这些库,因此,如果我要在Java SE中使用它,则不需要该stax-api库.另一方面,Android具有部分"的stax-api库.对于android,我只需要javax.xml.stream.*包.这意味着我需要解压缩stax-api,删除除javax.xml.stram包之外的所有内容,然后重新打包.

The problem with stax api is that it has all the packages in javax.* which is a "core library". Java jdk has all these libraries included, so if I were to use it in Java SE, I wouldn't need that stax-api library. Android, on the other hand, has a "partial" stax-api library. For android I only need the javax.xml.stream.* package. That means that I need to extract the stax-api, remove everything except the javax.xml.stram package, and repackage it again.

因此,我想在Android中使用此修改后的库是安全的.但是,它具有javax.*包,根据Android Studio来说,它是一个核心库,因此Android Studio(或Android Studio中的任何组件)会向我发出警告:

So I guess it is safe to use this modified library in Android. But, it has the javax.* package, which, according to Android studio is a core library, so Android Studio (or whatever component in Android Studio) gives me a warning:

在以下情况下对核心类(java.*或javax.*)的不良使用或错误使用 没有建立核心库.

Ill-advised or mistaken usage of a core class (java.* or javax.*) when not building a core library.

这通常是由于无意中将核心库文件包含在其中 使用IDE(例如Eclipse)时,您的应用程序的项目.如果 您确定您不是故意定义核心类,那么这 是最有可能发生的情况的解释.

This is often due to inadvertently including a core library file in your application's project, when using an IDE (such as Eclipse). If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.

但是,您实际上可能正在尝试在核心中定义一个类 命名空间,例如您可能从中获取的源 非Android虚拟机项目.这肯定不会 工作.至少,这会损害您的应用与 该平台的未来版本.这也经常令人怀疑 合法性.

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project. This will most assuredly not work. At a minimum, it jeopardizes the compatibility of your app with future versions of the platform. It is also often of questionable legality.

如果您真的打算构建一个核心库-仅 作为创建完整虚拟机发行版的一部分是适当的, 而不是编译应用程序-然后使用 "--core-library"选项可禁止显示此错误消息.

If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message.

如果您继续使用"--core-library",但实际上正在构建一个 应用程序,然后警告您的应用程序仍将失败 在某个时候进行构建或运行.请为生气的顾客做好准备 谁发现,例如,您的应用程序一旦停止运行 他们升级了操作系统.你应该为此负责 问题.

If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem.

如果您合法地使用了一些恰巧位于内核中的代码 包装,那么最简单的安全选择就是重新包装 该代码.也就是说,将相关的类移到您自己的包中 命名空间.这意味着他们将永远不会与核心冲突 系统类. JarJar是一种可以帮助您实现这一目标的工具. 如果发现无法执行此操作,则表明 你走的路最终会导致痛苦,痛苦,悲伤, 和感叹.

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. JarJar is a tool that may help you in this endeavor. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.

因此,我想使用此--core-library选项.但是在哪里设置呢?

So, I'd like to use this --core-library option. But where to set it?

我已经查看了 Android Studio忽略--core-library标志这并没有帮助我.我认为这些答案已经过时,这就是为什么我要问一个新问题.

I already looked at Android Studio ignore --core-library flag which didn't help me. I think those answers are outdated, and that's why I'm asking a new question.

我尝试过的操作:

  1. build.gradle:

  1. build.gradle:

dexOptions {
  coreLibrary true;
}

  • build.gradle:

  • build.gradle:

    dexOptions {
      preDexLibraries = false
    }
    
    project.tasks.withType(com.android.build.gradle.tasks.Dex) {
      additionalParameters=['--core-library']
    }
    

  • 文件->其他设置->默认设置->编译器-> Android编译器并检查添加--core-library标志"

  • File --> Other Settings --> Default Settings --> Compilers --> Android Compilersand check the 'Add --core-library flag'

    这些都不起作用.有什么方法可以设置该选项?

    None of these worked. Is there any way to set that option?

    为什么需要STAX:我正在使用工作簿,工作表,列,.xlsx文件的单元格做一些事情.当我仅包含poi-ooxml-3.14-beta1-20151223.jar时,在构建时间中出现错误,提示class file for org.apache.poi.ss.usermodel.Workbook not found.

    Why do I need STAX:I'm doing some stuff with Workbook, Sheet, Columns, Cells for .xlsx files.When I include only poi-ooxml-3.14-beta1-20151223.jar I get an error in build time saying class file for org.apache.poi.ss.usermodel.Workbook not found.

    在运行时包含poi-3.14-beta1-20151223.jar之后,我得到了Could not find method org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.isSetBookViews, referenced from method org.apache.poi.xssf.usermodel.XSSFWorkbook.

    Upon including poi-3.14-beta1-20151223.jar on runtime I get, among others, Could not find method org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.isSetBookViews, referenced from method org.apache.poi.xssf.usermodel.XSSFWorkbook.

    在运行时包括poi-ooxml-schemas-3.14-beta1-20151223.jar时得到Failed resolving Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorkbook; interface 59 'Lorg/apache/xmlbeans/XmlObject;java.lang.VerifyError: org/apache/poi/xssf/usermodel/XSSFWorkbook

    Upon including poi-ooxml-schemas-3.14-beta1-20151223.jar during runtime I get , among others, Failed resolving Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorkbook; interface 59 'Lorg/apache/xmlbeans/XmlObject; and java.lang.VerifyError: org/apache/poi/xssf/usermodel/XSSFWorkbook

    在运行时包括xmlbeans-2.6.0.jar时,我会得到Could not find method javax.xml.stream.events.Namespace.getPrefix, referenced from method org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.getQNamejava.lang.NoClassDefFoundError: javax.xml.stream.XMLEventFactory at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)

    Upon including xmlbeans-2.6.0.jar during runtime I get, among others, Could not find method javax.xml.stream.events.Namespace.getPrefix, referenced from method org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.getQName and java.lang.NoClassDefFoundError: javax.xml.stream.XMLEventFactory at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)

    更新因此,来自 http://poi.apache.org/faq.html#faq-N1017E

    此错误表明 XMLEventFactory类不提供以下功能 POI取决于.可能有许多不同的原因 这个:

    This error indicates that the class XMLEventFactory does not provide functionality which POI is depending upon. There can be a number of different reasons for this:

    过时的xml-apis.jar,stax-apis.jar或xercesImpl.jar:

    Outdated xml-apis.jar, stax-apis.jar or xercesImpl.jar:

    • 这些库在Java 5及更低版本中是必需的,但在规范兼容的Java 6实现中实际上并不是必需的,因此请尝试 从类路径中删除这些库.如果不可能的话 尝试升级到这些jar文件的较新版本.
    • 运行IBM Java 6(可能作为WebSphere Application Server的一部分):IBM Java 6不提供Java所需的所有接口 XML标准,只有IBM Java 7似乎提供了正确的方法 接口,因此请尝试升级JDK.
    • Sun/Oracle Java 6的补丁程序级别已过期:某些接口仅包含在Java 6的补丁程序级别中/已修复. 使用最新的可用补丁程序级别运行,甚至更好地使用Java 7/8,此功能在所有情况下均应可用.
    • These libraries were required with Java 5 and lower, but are not actually required with spec-compliant Java 6 implementations, so try removing those libraries from your classpath. If this is not possible, try upgrading to a newer version of those jar files.
    • Running IBM Java 6 (potentially as part of WebSphere Application Server): IBM Java 6 does not provide all the interfaces required by the XML standards, only IBM Java 7 seems to provide the correct interfaces, so try upgrading your JDK.
    • Sun/Oracle Java 6 with outdated patchlevel: Some of the interfaces were only included/fixed in some of the patchlevels for Java 6. Try running with the latest available patchlevel or even better use Java 7/8 where this functionality should be available in all cases.

    因此,如果我没看错的话,在Android中,我确实需要一个截断的" STAX API.

    So, if I read this correctly, in Android, I do need a "truncated" STAX api.

    推荐答案

    当您尝试使用Apache POI时会遇到很多问题,它取决于Android应用程序中的库.在xmlbeans.jar中有重复的类,其中"javax.*"包被Android编译器和其他一些禁止.

    There are a number of problems when you try to use Apache POI and it's depending libraries in an Android Application. Among others there are duplicate classes in xmlbeans.jar, the "javax.*" packages are prohibited by the Android compiler and a few others.

    当前有两个项目试图解决这些问题:

    There are currently two projects which try to fix those issues:

    • https://github.com/andruhon/android5xlsx
    • https://github.com/centic9/poi-on-android/ (which I maintain)

    android5xlsx提供了现成的jar文件以包含在您的应用程序中,但目前使用的POI有点过时. poi-on-android基于POI 3.15-beta1,可以很容易地为较新版本的POI重新编译.这两个项目都适用于Android 5+,并带有示例代码,应允许在Android上基本使用Apache POI.

    android5xlsx provides ready-made jar-files to include in your application, but currently uses a somewhat outdated version of POI. poi-on-android is based on POI 3.15-beta1 and can be recompiled for newer versions of POI fairly easily.Both projects are for Android 5+ and come with sample code and should allow basic usage of Apache POI on Android.

    这篇关于在Android中集成org.apache.poi和javax.xml.stream.*包(stax-api)-如何在Android Studio中设置--core-library参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-06 12:14