For getting Apache POI to work on Android, I need to get Stack to work on Android. Following this question:Using JAXB with Google Android and @Sean Barbeau\'s answer. I succ
for me adding
implementation 'com.android.support:multidex:1.0.0',
implementation 'com.fasterxml:aalto-xml:1.0.0'
and
multiDexEnabled true
in defaultConfig
solve the problem
Add the following to your gradle file:
implementation 'com.fasterxml:aalto-xml:1.0.0'
And relax...
Try using the pre-packaged JARs from the CUTR Maven repo for stax and Aalto:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
// CUTR SNAPSHOTs
url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/snapshots"
}
maven {
// CUTR Releases
url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/releases"
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
packagingOptions{
// exclude 'META-INF/DEPENDENCIES' <--- and try commenting this out, I don't believe it should be needed
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
pickFirst 'META-INF/services/javax.xml.stream.XMLInputFactory'
}
defaultConfig {
applicationId "com.quizwiz.sharmakritya.poi"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/poi-3.12-20150511.jar')
compile files('libs/poi-ooxml-3.12-20150511.jar')
compile files('libs/poi-ooxml-schemas-3.12-20150511.jar'){
compile files('libs/xmlbeans-2.6.0.jar')
// JSR173 APIs
compile 'edu.usf.cutr.android.xml:stax2-api-android:3.1.1'
compile 'edu.usf.cutr.android.xml:stax-api-android:1.0-2'
// JSR173 provider implementation - Aalto
compile 'edu.usf.cutr.android.xml:aalto-xml-android:0.9.8'
}
I believe the main issue here is that Apache POI depends on JSR173 Streaming API for XML, which depends on the platform (in our case Android) being able to provide an implementation and underlying XML parsing provider. Android doesn't support JSR173, so it can't find a provider - com.bea.xml.stream.EventFactory
is the default provider, and it can't even find that one (see JAXB marshalling exception javax.xml.stream.FactoryConfigurationError running with Java 5 for more details on this topic).
Aalto is an open-source JSR173 provider, so you should be able to use this in place of the BEA implementation (com.bea.xml.stream.EventFactory
). The Aalto archive (JAR file) includes the necessary registration (under META-INF/services/javax.xml.stream.XMLInputFactory
file, that points to the Aalto class com.fasterxml.aalto.stax.InputFactoryImpl
) that should allow a Java platform to recognize it as a JSR173 implementation. As a result, you should no longer get the edu.usf.cutr.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found
error, because Android should recognize that Aalto is an available provider, and use Aalto instead.
Note - apparently there are bugs with previous versions of ADT for Eclipse and Gradle plugin < 0.7.0 that strips the /META-INF/*
files 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.
If the above doesn't work, 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
.
If the above doesn't work, I also noticed that you have a exclude 'META-INF/DEPENDENCIES'
in your packagingOptions - I'd remove this, as I don't believe you should need that line, and it may be causing problems with registering Aalto as well.
You could also check out some other solutions/work that people have done trying to get Apache POI working on Android:
Check FAQ #18:
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:
Outdated xml-apis.jar, stax-apis.jar or xercesImpl.jar:
- 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.
...
You probably need to exclude the stax:stax-api
dependency through the build system that you use. I just solved a similar problem (same stack trace but from Groovy) by applying this method.
UPDATE: It is best if you use the dependencies from a Maven repository, it will largely simplify your dependencies section:
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:multidex:1.0.0'
compile ('org.apache.poi:poi-ooxml:3.12') {
exclude group: 'stax', module: 'stax-api'
}
}
You can now also remove the extra libraries that you have under the libs
directory.
The Apache POI FAQ answer indicates that stax may not be needed at all for some Java versions, especially above 6.
After upgrading POI from 3.9 to 3.15 we were getting a:
javax.xml.stream.FactoryConfigurationError: Provider for class javax.xml.stream.XMLEventFactory cannot be created
Besides POI, our build included:
compile ('com.msopentech.odatajclient:odatajclient-engine:0.9.0')
it turned out that the odatajclient jar included a java.xml.stream.XMLEventFactory
file in its META-INF\services
, the contain of that file was com.fasterxml.aalto.stax.EventFactoryImpl
but the aalto included in odatajclient was old and com.fasterxml.aalto.stax.EventFactoryImpl
was finally inheriting from com.msopentech.javax.xml.stream.XMLEventFactory
instead of java.xml.stream.XMLEventFactory
class.
At run time POI was using the java.xml.stream.XMLEventFactory
, the class loader returned com.fasterxml.aalto.stax.EventFactoryImpl
and the cast to java.xml.stream.XMLEventFactory
failed.
The solution was excluding the module that included the old aalto:
compile ('com.msopentech.odatajclient:odatajclient-engine:0.9.0')
{
exclude group: 'com.msopentech.odatajclient', module: 'odatajclient-engine-xml'
}