问题描述
我使用的是 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:
处理javax/xml/stream/EventFilter.class"的问题:
在以下情况下不明智或错误地使用核心类(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.
但是,您实际上可能正在尝试在核心中定义一个类命名空间,例如,您可能从非安卓虚拟机项目.这绝对不会工作.至少,它会危及您的应用程序与平台的未来版本.也常常受到质疑合法性.
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.
我所做的尝试:
build.gradle:
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 后,我得到,其中,找不到方法 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.isSetBookViews,从方法 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 resolve Lorg/openxmlformats/schemas/spreadsheetml/x2006/main/CT工作簿;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
后,我得到,其中包括 找不到方法 javax.xml.stream.events.Namespace.getPrefix,从方法 org 引用.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.getQName
和 java.lang.NoClassDefFoundError: javax.xml.stream.XMLEventFactory at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.(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
18.为什么会出现 java.lang.NoClassDefFoundError: javax/xml/stream/XMLEventFactory.newFactory()
这个错误表明类 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 没有提供XML 标准,似乎只有 IBM Java 7 提供了正确的接口,因此请尝试升级您的 JDK.
- 具有过时补丁级别的 Sun/Oracle Java 6:某些接口仅包含/修复在 Java 6 的某些补丁级别中.尝试使用最新的可用补丁级别运行,甚至更好地使用 Java7/8 应该在所有情况下都可以使用此功能.
所以,如果我没看错的话,在 Android 中,我确实需要一个截断的"STAX api.
So, if I read this correctly, in Android, I do need a "truncated" STAX api.
推荐答案
当您尝试使用 Apache POI 时会遇到许多问题,并且它依赖于 Android 应用程序中的库.其中 xmlbeans.jar 中有重复的类,Android 编译器和其他一些程序禁止使用javax.*"包.
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:
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 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!