Error:Execution failed for task ':asynctask:packageDebug'

本文介绍了解决Android项目中因httpclient-cache和httpcore库导致的Duplicate files错误的方法。通过在build.gradle文件中添加排除特定文件的配置,可以有效避免打包APK时出现的重复文件问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出错情形:

在项目里面导入apache官方下载的jar包:

httpclient-4.5.1.jar

httpcore-4.4.3.jar

httpclient-cache-4.5.1.jar

我的build.gradle文件:

<pre name="code" class="plain">apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.lake.asynctask"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/httpclient-cache-4.5.1.jar')
}



在代码通过编译运行正确的情况下,报出如下错误:

Information:Gradle tasks [:asynctask:assembleDebug]
:asynctask:preBuild UP-TO-DATE
:asynctask:preDebugBuild UP-TO-DATE
:asynctask:checkDebugManifest
:asynctask:preReleaseBuild UP-TO-DATE
:asynctask:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:asynctask:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:asynctask:prepareDebugDependencies
:asynctask:compileDebugAidl UP-TO-DATE
:asynctask:compileDebugRenderscript UP-TO-DATE
:asynctask:generateDebugBuildConfig UP-TO-DATE
:asynctask:generateDebugAssets UP-TO-DATE
:asynctask:mergeDebugAssets UP-TO-DATE
:asynctask:generateDebugResValues UP-TO-DATE
:asynctask:generateDebugResources UP-TO-DATE
:asynctask:mergeDebugResources UP-TO-DATE
:asynctask:processDebugManifest UP-TO-DATE
:asynctask:processDebugResources UP-TO-DATE
:asynctask:generateDebugSources UP-TO-DATE
:asynctask:processDebugJavaRes UP-TO-DATE
:asynctask:compileDebugJavaWithJavac UP-TO-DATE
:asynctask:compileDebugNdk UP-TO-DATE
:asynctask:compileDebugSources UP-TO-DATE
:asynctask:preDexDebug UP-TO-DATE
:asynctask:dexDebug UP-TO-DATE
:asynctask:validateDebugSigning
:asynctask:packageDebug
Error:duplicate files during packaging of APK /home/lake/android_workspace/Thread_Handler_AsyncTask/asynctask/build/outputs/apk/asynctask-debug-unaligned.apk
Path in archive: META-INF/DEPENDENCIES
Origin 1: /home/lake/android_workspace/Thread_Handler_AsyncTask/asynctask/libs/httpclient-cache-4.5.1.jar
Origin 2: /home/lake/android_workspace/Thread_Handler_AsyncTask/asynctask/libs/httpcore-4.4.3.jar
You can ignore those files in your build.gradle:
android {
 packagingOptions {
   exclude 'META-INF/DEPENDENCIES'
 }
}
Error:Execution failed for task ':asynctask:packageDebug'.
> Duplicate files copied in APK META-INF/DEPENDENCIES
  File 1: /home/lake/android_workspace/Thread_Handler_AsyncTask/asynctask/libs/httpclient-cache-4.5.1.jar
  File 2: /home/lake/android_workspace/Thread_Handler_AsyncTask/asynctask/libs/httpcore-4.4.3.jar

Information:BUILD FAILED
Information:Total time: 2.135 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console


解决方法:

在build.gradle中添加内容如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.lake.asynctask"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/httpclient-cache-4.5.1.jar')
}

这个不仅可以解决"Duplicate files" 错误,还可以忽略 apache's libs 和dependency error 。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值