错误:任务':app:packageAllDebugClassesForMultiDex'执行失败。 Apache POI Android

时间:2016-04-07 19:27:43

标签: java android

我想在我的Android项目中包含Apache POI,但是我收到以下错误:

Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class

我的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        applicationId "com.example.alexandra.kavb"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

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

    dexOptions {
        javaMaxHeapSize "2g"
    }
}

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.google.android.gms:play-services-appindexing:8.1.0'
    compile files('libs/commons-codec-1.10.jar')
    compile files('libs/commons-logging-1.2.jar')
    compile files('libs/log4j-1.2.17.jar')
    compile files('libs/poi-3.14-20160307.jar')
    compile files('libs/poi-ooxml-3.14-20160307.jar')
    compile files('libs/poi-ooxml-schemas-3.14-20160307.jar')
    compile files('libs/xmlbeans-2.6.0.jar')
    compile 'com.android.support:multidex:1.0.1'
}

我的失败日志:

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:collectDebugMultiDexComponents
:app:packageAllDebugClassesForMultiDex FAILED
Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class
Information:BUILD FAILED
Information:Total time: 2.68 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

我已经设法将错误追踪到xmlbeans.jar。但是删除它会导致文档创建无效。

2 个答案:

答案 0 :(得分:1)

你能做的是:

  1. 解压缩jar文件。 (只需将.jar扩展名更改为.zip
  2. 即可
  3. 这将删除重复的文件。
  4. 使用jar重新创建jar cf xmlbeans.jar -C (unzipped folder path) .(请注意,命令末尾有一个点)
  5. 使用此重新生成的jar

答案 1 :(得分:1)

这是依赖树。您可以看到xmlbeanspoi:poi-ooxml-schemas的依赖关系,commons-codecpoi的依赖关系,因此您重复了这些错误。

+--- commons-codec:commons-codec:1.10
+--- commons-logging:commons-logging:1.2
+--- log4j:log4j:1.2.17
+--- org.apache.poi:poi:3.14
|    \--- commons-codec:commons-codec:1.10
+--- org.apache.poi:poi-ooxml:3.14
|    +--- org.apache.poi:poi:3.14 (*)
|    +--- org.apache.poi:poi-ooxml-schemas:3.14
|    |    \--- org.apache.xmlbeans:xmlbeans:2.6.0
|    |         \--- stax:stax-api:1.0.1
|    \--- com.github.virtuald:curvesapi:1.03
+--- org.apache.poi:poi-ooxml-schemas:3.14 (*)
\--- org.apache.xmlbeans:xmlbeans:2.6.0 (*)

如果您刚刚使用Gradle编译依赖项,那么您就不会遇到这个问题。您可以在Maven Repository中搜索作为JAR文件下载的所有依赖项。

ext {
    // Variables to keep libraries consistent
    supportLibVersion = '23.2.1'
    apachePOIVersion = '3.14'
    googlePlayServicesVersion = '8.4.0'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile "com.android.support:design:${supportLibVersion}"
    compile "com.google.android.gms:play-services-appindexing:${googlePlayServicesVersion}"
    compile 'commons-logging:commons-logging:1.2'
    compile 'log4j:log4j:1.2.17'
    compile "org.apache.poi:poi:${apachePOIVersion}"
    compile "org.apache.poi:poi-ooxml:${apachePOIVersion}"
    compile "org.apache.poi:poi-ooxml-schemas:${apachePOIVersion}"
}

注意:我实际上并没有构建具有这些依赖关系的应用程序,因此可能仍然存在(不同的)错误。