安卓Gradle构建错误

6

我将在build.gradle文件中添加依赖项。

我的代码是:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.1.11'
compile 'org.apache.httpcomponents:httpmime:4.4.1'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.4.1'

我遇到了一个错误:

Warning:Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for debug as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages

Warning:Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for debug as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages

Warning:Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for release as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages

Warning:Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for release as it may be conflicting with the internal version provided by Android.
     In case of problem, please repackage it with jarjar to change the class packages

如何解决这个问题。 请帮助我?
5个回答

9
您可以尝试在 build.gradle(Module: app)中添加以下内容。这解决了我的问题:
packagingOptions{
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
}

我的最终构建 gradle 如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "info.androidhive.camerafileupload"
        minSdkVersion 19
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions{
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('libs/httpclient-4.3.6.jar')
    compile files('libs/httpcore-4.3.3.jar')
    compile files('libs/httpmime-4.3.6.jar')
}

注意:在我的情况下,我已经将这3个jar包粘贴到了libs文件夹中。您可以下载并执行此操作。


好的回答!谢谢。 - Tino
但是,如果我们在应用程序gargle中从package_name:version编译/下载文件,而不是从libs文件夹中获取呢? - VVB

4

3

如果你的compileSdkVersion是19(在我的情况下),我通过使用这个方法解决了问题。

compile ('org.apache.httpcomponents:httpmime:4.3'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'

如果您的compileSdkVersion是23,则使用以下内容:

android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    }
}

2

不必为每个库添加exclude,您可以为所有库添加以下配置:

configurations {
    all*.exclude group: 'org.apache.httpcomponents',module: 'httpclient'
}

1

如果什么都不起作用,尝试这个。肯定会有效。

替换这个

 dependencies {
......
.......
    compile 'org.apache.httpcomponents:httpclient:4.4.1'
}

使用以下代码:
dependencies {
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接