创buildAndroid APK时出现DuplicateFileException错误

我在Windows 7上使用Android Studio 2.0。在构build我的Android项目时,出现以下错误:

:app:transformResourcesWithMergeJavaResForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK com/google/appengine/repackaged/org/apache/commons/codec/language/bm/sep_approx_spanish.txt File1: C:\Users\USER\.gradle\caches\modules-2\files-2.1\com.google.appengine\appengine-api-1.0-sdk\1.9.28\e92c18272b555027d9ec666e7a89162f10638314\appengine-api-1.0-sdk-1.9.28.jar File2: C:\Users\USER\.gradle\caches\modules-2\files-2.1\com.google.appengine\appengine-endpoints\1.9.28\bf2e8a74bd28e388b3487fc78a0c7adfa592fd5d\appengine-endpoints-1.9.28.jar` 

我一直在挖掘几个小时,并没有看到任何事情导致这一点。 我在我的主目录中删除了.gradle,但它只是重build它一切。 有谁知道是什么原因造成的?

全部gradle输出:

 Executing tasks: [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug] Configuration on demand is an incubating feature. WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for release as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages Incremental java compilation is an incubating feature. :app:clean :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :backend:appengineDownloadSdk :backend:compileJava :backend:processResources UP-TO-DATE :backend:classes :backend:appengineEndpointsGetClientLibs UP-TO-DATE :backend:appengineEndpointsExpandClientLibs UP-TO-DATE :backend:compileEndpointsSrcJava :backend:processEndpointsSrcResources UP-TO-DATE :backend:endpointsSrcClasses :backend:_appengineEndpointsAndroidArtifact UP-TO-DATE :backend:jar UP-TO-DATE :app:prepareComAndroidSupportAnimatedVectorDrawable2330Library :app:prepareComAndroidSupportAppcompatV72330Library :app:prepareComAndroidSupportSupportV42330Library :app:prepareComAndroidSupportSupportVectorDrawable2330Library :app:prepareComGoogleAndroidGmsPlayServicesAuth840Library :app:prepareComGoogleAndroidGmsPlayServicesBase840Library :app:prepareComGoogleAndroidGmsPlayServicesBasement840Library :app:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library :app:prepareDebugDependencies :app:compileDebugAidl :app:compileDebugRenderscript :app:generateDebugBuildConfig :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets :app:generateDebugResValues UP-TO-DATE :app:processDebugGoogleServices :app:generateDebugResources :app:mergeDebugResources :app:processDebugManifest :app:processDebugResources :app:generateDebugSources :app:mockableAndroidJar UP-TO-DATE :app:preDebugUnitTestBuild UP-TO-DATE :app:prepareDebugUnitTestDependencies :app:preDebugAndroidTestBuild UP-TO-DATE :app:prepareDebugAndroidTestDependencies :app:compileDebugAndroidTestAidl :app:processDebugAndroidTestManifest :app:compileDebugAndroidTestRenderscript :app:generateDebugAndroidTestBuildConfig :app:generateDebugAndroidTestAssets UP-TO-DATE :app:mergeDebugAndroidTestAssets :app:generateDebugAndroidTestResValues UP-TO-DATE :app:generateDebugAndroidTestResources :app:mergeDebugAndroidTestResources :app:processDebugAndroidTestResources :app:generateDebugAndroidTestSources :app:compileDebugJavaWithJavac Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. :app:compileDebugNdk UP-TO-DATE :app:compileDebugSources :app:buildInfoDebugLoader :app:transformClassesWithExtractJarsForDebug :app:transformClassesWithInstantRunVerifierForDebug :app:transformClassesWithJavaResourcesVerifierForDebug UP-TO-DATE :app:mergeDebugJniLibFolders :app:transformNative_libsWithMergeJniLibsForDebug :app:processDebugJavaRes UP-TO-DATE :app:transformResourcesWithMergeJavaResForDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK com/google/appengine/repackaged/org/apache/commons/codec/language/bm/sep_approx_spanish.txt File1: C:\Users\USER\.gradle\caches\modules-2\files-2.1\com.google.appengine\appengine-api-1.0-sdk\1.9.28\e92c18272b555027d9ec666e7a89162f10638314\appengine-api-1.0-sdk-1.9.28.jar File2: C:\Users\USER\.gradle\caches\modules-2\files-2.1\com.google.appengine\appengine-endpoints\1.9.28\bf2e8a74bd28e388b3487fc78a0c7adfa592fd5d\appengine-endpoints-1.9.28.jar * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 15.364 secs 

从我可以告诉的是,这是两个依赖之间的冲突:

 dependencies { appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.28' compile 'com.google.appengine:appengine-endpoints:1.9.28' compile 'com.google.appengine:appengine-endpoints-deps:1.9.28' compile 'javax.servlet:servlet-api:2.5' compile 'com.google.http-client:google-http-client-jackson2:1.20.0' compile 'com.google.http-client:google-http-client-gson:1.19.0' } 

我认为这是两个依赖:

 appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.28' compile 'com.google.appengine:appengine-endpoints:1.9.28' compile 'com.google.appengine:appengine-endpoints-deps:1.9.28' 

我无法删除任何这些,因为它会导致后端模块上的编译错误。 看起来这两个依赖关系几乎是相同的,但是。 他们是否都在那里? 任何build议表示赞赏!

Solutions Collecting From Web of "创buildAndroid APK时出现DuplicateFileException错误"

要完成@ dsh的回答:

在依赖关系树中有两个包含相同文件sep_approx_spanish.txt jar文件,这是不允许的。

为了解决你的问题,你必须追踪你的哪个gradle依赖包含两个名为appengine-api-1.0-sdk-1.9.28.jarappengine-endpoints-1.9.28.jar ; 要做到这一点,你可以打开android studio的terminal控制台,然后input:

 //if ou are on windows gradlew dependencies //if you are on unix based os ./gradlew dependencies 

你会看到gradle做了很多事情,但是在某个时候它会打印整个依赖关系树。 一旦你发现哪两个依赖导致了问题,在一个依赖声明的末尾添加这段代码

 { exclude "sep_approx_spanish.txt" } 

所以例如,如果这两个依赖是这些

 compile 'com.google.appengine:appengine-endpoints:1.9.28' compile 'com.google.appengine:appengine-endpoints-deps:1.9.28 

如果必须成为

 compile 'com.google.appengine:appengine-endpoints:1.9.28'{ exclude "sep_approx_spanish.txt" } compile 'com.google.appengine:appengine-endpoints-deps:1.9.28 

要么

 compile 'com.google.appengine:appengine-endpoints:1.9.28' compile 'com.google.appengine:appengine-endpoints-deps:1.9.28'{ exclude "sep_approx_spanish.txt" } 

通过这种方式你可以告诉gradle不考虑这两个库中的一个文件,避免apk内的文件重复。

该错误告诉你,构build你的APK会导致两个名为com / google / appengine / repackaged / org / apache / commons / codec / language / bm / sep_approx_spanish.txt的文件。 这是失败的,因为APK只能在任何给定的path中包含一个文件。 这可能是由你的类path中包含文件的两个不同的jar文件引起的。 在我的项目中,这只发生在运行时没有实际使用的文件,所以我完全将它们从APK中排除。

完成@Apperside的答案。

我通过将下面几行添加到我的应用程序模块的build.gradle文件中来解决了这个错误:

 android{ packagingOptions { exclude 'com/google/appengine/repackaged/org/apache/commons/codec/language/bm/*' exclude 'com/google/appengine/repackaged/org/codehaus/jackson/impl/VERSION.txt' exclude 'com/google/appengine/repackaged/org/apache/commons/codec/language/*' } (...) }