【问题标题】:Duplicate Files Copied in APK - Android在 APK 中复制的重复文件 - Android
【发布时间】:2017-06-14 09:40:26
【问题描述】:

我想用

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

我在 build.gradle (App) 的 android {} 标签中添加了useLibrary 'org.apache.http.legacy' 并在我的依赖项中添加了compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'。 我的应用(build.gradle)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.abc.ggg.app1"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.microsoft.cognitive:speakerrecognition:1.1.0'
    testCompile 'junit:junit:4.12'
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

}

我的项目(build.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我一直在寻找答案,但没有解决我的问题。我收到一个错误--

Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.3 is ignored for debug as it may be conflicting with the internal version provided by Android.

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
    File1: C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpmime\4.3\5b0002c5fb66867ca919be0fbd86de1cfaf76da7\httpmime-4.3.jar
    File2: C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpclient-android\4.3.5.1\eecbb0b998e77629862a13d957d552b3be58fc4e\httpclient-android-4.3.5.1.jar

请帮忙! :(

【问题讨论】:

  • 你只需要遗留库。删除compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'这一行。 httpcomponents 添加了 2 次,即为什么您会收到 DuplicateFileException。

标签: java android apache gradle apk


【解决方案1】:

您可以在build.gradle 部分添加以下内容

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
}

使用

compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'

然后清理-重建-运行

【讨论】:

  • 我添加了packagingOptions { exclude 'META-INF/ASL2.0' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/MANIFEST.MF' },它成功了。但我想了解为什么。知道的可以告诉我吗?
  • @UrviG 致电compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
  • 通过添加第一条评论中的 sn-p,它现在可以正常运行了。
  • @UrviG 克服这个问题??