在android上使用com.bea.xml.stream包

kri*_*tya 4 android apache-poi android-gradle-plugin

为了让Apache POI在Android上运行,我需要让Stack在Android上运行.关注这个问题:将JAXB与Google Android和@Sean Barbeau的答案结合使用.我成功地将所有jar转换为Android兼容的,包括Apache POI库,但它仍然给我这个运行时错误:

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)
Run Code Online (Sandbox Code Playgroud)

整个错误的关键是android缺少包com.bea.xml.stream.我该如何解决这个问题?

我试着通过从这里下载外部jar来自行解决这个问题.但这给了我奇怪的错误:

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)
Run Code Online (Sandbox Code Playgroud)

注意:从Gradle Dependencies中删除com.bea.stax.impl_1.2.0.jar会让我回到第一个错误.

请帮我找一个在android上使用com.bea.xml.stream包的方法. UPDATE

在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')
}
Run Code Online (Sandbox Code Playgroud)

UPDATE#2 编译时出错:

    Error:duplicate files during packaging of APK C:\Users\sharmakritya\AndroidStudioProjects\POI\app\build\outputs\apk\app-debug-unaligned.apk
    Path in archive: META-INF/services/javax.xml.stream.XMLInputFactory
    Origin 1: C:\Users\sharmakritya\AndroidStudioProjects\POI\app\build\intermediates\javaResources\debug\META-INF\services\javax.xml.stream.XMLInputFactory
    Origin 2: C:\Users\sharmakritya\.gradle\caches\modules-2\files-2.1\edu.usf.cutr.android.xml\aalto-xml-android\0.9.8\bf4e7339b028f92638b36ab9ac3cc3314a860d1\aalto-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:\Users\sharmakritya\AndroidStudioProjects\POI\app\build\intermediates\javaResources\debug\META-INF\services\javax.xml.stream.XMLInputFactory
    File 2: C:\Users\sharmakritya\AndroidStudioProjects\POI\app\build\intermediates\javaResources\debug\META-INF\services\javax.xml.stream.XMLInputFactory
Run Code Online (Sandbox Code Playgroud)

nek*_*jsi 5

查看FAQ#18:

此错误表示类XMLEventFactory未提供POI所依赖的功能.这可能有很多不同的原因:

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

  • Java 5及更低版本需要这些库,但实际上并不需要符合规范的Java 6实现,因此请尝试从类路径中删除这些库.如果无法做到这一点,请尝试升级到这些jar文件的较新版本.

...

您可能需要stax:stax-api通过您使用的构建系统排除依赖项.我通过应用此方法解决了类似的问题(相同的堆栈跟踪,但来自Groovy).

更新: 最好是使用Maven存储库中的依赖项,它将在很大程度上简化您的依赖项部分:

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

您现在还可以删除libs目录下的额外库.

Apache POI FAQ答案表明某些Java版本可能根本不需要stax,尤其是6以上版本.