出错情形:
在项目里面导入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 。