解决新版AndroidStudio 导入HttpClient冲突

HttpClient虽然已经被Google抛弃,但是有限项目依然依赖这些库,还得用。我把Httpclient的所有库导入项目后,会产生如下错误。

Error:duplicate files during packaging of APK F:\Downloads\MyApplication7\app\build\outputs\apk\app-debug-unaligned.apk

Path in archive: META-INF/DEPENDENCIES

Origin 1: F:\Downloads\MyApplication7\app\libs\httpclient-4.5.2.jar

Origin 2: F:\Downloads\MyApplication7\app\libs\httpcore-4.4.4.jar


经过查找可以在文件中(app\build.gradle)加入以下代码消除这个问题。

  1. packagingOptions {  
  2.     exclude 'META-INF/DEPENDENCIES'  
  3.     exclude 'META-INF/NOTICE'  
  4.     exclude 'META-INF/LICENSE'  
  5.     exclude 'META-INF/LICENSE.txt'  
  6.     exclude 'META-INF/NOTICE.txt'  
  7.     exclude 'META-INF/ASL2.0'  
  8.     exclude 'META-INF/notice.txt'  
  9. }  

整个文件如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.xba.imagetest"
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/notice.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile files('libs/httpclient-4.5.2.jar')
    compile files('libs/httpcore-4.4.4.jar')
}

相关推荐