Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

apache poi - Using com.bea.xml.stream package on android

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 successfully converted all the jar's to android compatible ones including the Apache POI library but it still gives me this run time error:

06-22 01:06:52.461  14865-14865/com.quizwiz.sharmakritya.poi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.quizwiz.sharmakritya.poi, PID: 14865
    edu.usf.cutr.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory 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.XMLEventFactory.newInstance(XMLEventFactory.java:30)
            at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
            at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)
            at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)
            at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
            at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:87)
            at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:273)
            at org.apache.poi.xslf.usermodel.XMLSlideShow.empty(XMLSlideShow.java:103)
            at org.apache.poi.xslf.usermodel.XMLSlideShow.<init>(XMLSlideShow.java:75)
            at com.quizwiz.sharmakritya.poi.PPT.onCreate(PPT.java:16)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

The crux of the whole error is android is missing the package com.bea.xml.stream. How do I resolve this ?

I tried resolving this on my own by downloading an external jar from here. But that gave me strange errors:

06-22 01:10:42.906  20374-20374/com.quizwiz.sharmakritya.poi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.quizwiz.sharmakritya.poi, PID: 20374
    java.lang.RuntimeException: Unable to instantiate application android.support.multidex.MultiDexApplication: java.lang.RuntimeException: Multi dex installation failed (Field dexElementsSuppressedExceptions not found in class dalvik.system.PathClassLoader).
            at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4703)
            at android.app.ActivityThread.access$1600(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Multi dex installation failed (Field dexElementsSuppressedExceptions not found in class dalvik.system.PathClassLoader).
            at android.support.multidex.MultiDex.install(MultiDex.java:178)
            at android.support.multidex.MultiDexApplication.attachBaseContext(MultiDexApplication.java:39)
            at android.app.Application.attach(Application.java:201)
            at android.app.Instrumentation.newApplication(Instrumentation.java:997)
            at android.app.Instrumentation.newApplication(Instrumentation.java:981)
            at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
????????????at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4703)
????????????at android.app.ActivityThread.access$1600(ActivityThread.java:175)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:146)
????????????at android.app.ActivityThread.main(ActivityThread.java:5602)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:515)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
????????????at dalvik.system.NativeStart.main(Native Method)

Note: removing the com.bea.stax.impl_1.2.0.jar from Gradle Dependencies gets me back to the first error.

Please help me find a way to use the package com.bea.xml.stream on android. UPDATE

The dependencies before your answer in gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions{
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
    }

    defaultConfig {
        applicationId "com.quizwiz.sharmakritya.poi"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:multidex:1.0.0'
    compile files('libs/stax-api-android-1.0-2.jar')
    compile files('libs/stax2-api-android-3.1.1.jar')
    //compile files('libs/com.bea.stax.impl_1.2.0.jar')
    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'){
    exclude group: 'stax', module: 'stax-api'
    }
    compile files('libs/xmlbeans-2.6.0.jar')
    compile files('libs/stax-1.2.0_rc2-dev.jar')
}

UPDATE #2 The error while compilation I get:

    Error:duplicate files during packaging of APK C:UserssharmakrityaAndroidStudioProjectsPOIappuildoutputsapkapp-debug-unaligned.apk
    Path in archive: META-INF/services/javax.xml.stream.XMLInputFactory
    Origin 1: C:UserssharmakrityaAndroidStudioProjectsPOIappuildintermediatesjavaResourcesdebugMETA-INFservicesjavax.xml.stream.XMLInputFactory
    Origin 2: C:Userssharmakritya.gradlecachesmodules-2files-2.1edu.usf.cutr.android.xmlaalto-xml-android.9.8f4e7339b028f92638b36ab9ac3cc3314a860d1aalto-xml-android-0.9.8.jar
You can ignore those files in your build.gradle:
    android {
      packagingOptions {
        exclude 'META-INF/services/javax.xml.stream.XMLInputFactory'
      }
    }
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/services/javax.xml.stream.XMLInputFactory
    File 1: C:UserssharmakrityaAndroidStudioProjectsPOIappuildintermediatesjavaResourcesdebugMETA-INFservicesjavax.xml.stream.XMLInputFactory
    File 2: C:UserssharmakrityaAndroidStudioProjectsPOIappuildintermediatesjavaResourcesdebugMETA-INFservicesjavax.xml.stream.XMLInputFactory
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...