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 。




如果我就是要在共享数据场景下使用,异步任务需修改主线程的全局变量。但是我不能在创建异步线程的函数中直接通过get获取,而是需要在run函数中创建异步任务,然后将Job &job添加进队列m_jobList.push_back(job),再通过check函数检查是否任务完成,如果完成则获取job的全局变量并往web端上传,否则继续循环检查check函数,那么如何设计代码可以安全的同步全局变量?我的代码如下,请尝试优化: int Job::executePQSQL() { try { LogPrint(LOG_LEVEL_INFO, "async of pqsql job"); // std::lock_guard<std::mutex> lock(mtx); m_mtxlock.lock(); LogPrint(LOG_LEVEL_INFO, "excute PQSQL"); timeBegin = apr_time_now(); string encoding = "'--client_encoding=?'"; encoding.replace(encoding.find('?'), 1, characterEncoding); if (!argsVec.empty()) { for (int i = 0; i < argsVec.size(); ++i) { int pos = scriptContent.find("?"); string res = "'" + argsVec[i] + "'"; scriptContent = scriptContent.replace(pos, 1, res); } } LogPrint(LOG_LEVEL_INFO, "PQSQL test1"); argsVec.clear(); LogPrint(LOG_LEVEL_ERROR, "cmdUser=%s, dbName=%s, password=%s, ip=%s, currentSchema=%s, scriptContent=%s", cmdUser.c_str(), dbName.c_str(), password.c_str(), ip.c_str(), currentSchema.c_str(), scriptContent.c_str()); LogPrint(LOG_LEVEL_ERROR, "scriptType=%s", scriptType.c_str()); PostSql pq(cmdUser.c_str(), dbName.c_str(), password.c_str(), ip.c_str(), stoi(port), currentSchema.c_str(), type.c_str(), scriptContent.c_str()); std::string m; if (!pq.connect(m)) { addResult(m); return 0; } LogPrint(LOG_LEVEL_INFO, "PQSQL test2"); m = pq.excuteSql(scriptContent.c_str()); LogPrint(LOG_LEVEL_INFO, "PQSQL test3"); LogPrint(LOG_LEVEL_INFO, "Get m: %s", m.c_str()); addResult(m); errorMsg.clear(); LogPrint(LOG_LEVEL_INFO, "add pqsql erromsg:%s", errorMsg.c_str()); timeEnd = apr_time_now(); m_mtxlock.unlock(); return 0; } catch (const std::bad_alloc& e) { LogPrint(LOG_LEVEL_ERROR, "内存分配失败: %s", e.what()); errorMsg = e.what(); return -1; } catch (const std::exception& e) { LogPrint(LOG_LEVEL_ERROR, "PQSQL执行错误: %s", e.what()); errorMsg = e.what(); return -1; } } int Job::run() { if (scriptType == "PQSQL") { auto jobPtr = std::make_shared<Job>(*this); future = std::async(std::launch::async, &Job::executePQSQL, jobPtr); return 0; } return 0; } int Job::check() { if (future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { try { LogPrint(LOG_LEVEL_INFO, "is Finished"); JobResult jobResult_1 = future.get(); result = jobResult_1.result; resultLen = jobResult_1.resultLen; timeBegin = jobResult_1.timeBegin; timeEnd = jobResult_1.timeEnd; LogPrint(LOG_LEVEL_ERROR, "Get result: %s", result.c_str()); } catch (const std::exception& e) { LogPrint(LOG_LEVEL_ERROR, "Async task execution failed: %s", e.what()); } } else { LogPrint(LOG_LEVEL_INFO, "not Finished"); } return 0; }
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值