添加的HttpCore和httpmime库Android项目在运行的应用程序提供了一个错误(Addi

2019-09-28 15:10发布

在我的Android应用我使用的HttpCore和httpmime库。 我的build.gradle文件扶养部分包含以下部分,

compile 'org.apache.httpcomponents:httpcore:4.4.4'
compile 'org.apache.httpcomponents:httpmime:4.5.2'

当我运行的应用程序提供了一个错误。 日志是如以下所示,

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/NOTICE
        File1: /Users/marpak/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpmime/4.5.2/22b4c53dd9b6761024258de8f9240c3dce6ea368/httpmime-4.5.2.jar
        File2: /Users/marpak/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.4.4/b31526a230871fbe285fbcbe2813f9c0839ae9b0/httpcore-4.4.4.jar

我怎样才能解决这个问题

Answer 1:

在您的build.gradle文件中添加这

android {
    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'
    exclude 'META-INF/LGPL2.1'
    }
}


Answer 2:

最好使用这一个,这将只包括一个许可文件,某些许可证(大部分开源)不允许从最终代码排除证:

    packagingOptions {
    pickFirst 'META-INF/DEPENDENCIES.txt'
    pickFirst 'META-INF/LICENSE.txt'
    pickFirst 'META-INF/NOTICE.txt'
    pickFirst 'META-INF/NOTICE'
    pickFirst 'META-INF/LICENSE'
    pickFirst 'META-INF/DEPENDENCIES'
    pickFirst 'META-INF/notice.txt'
    pickFirst 'META-INF/license.txt'
    pickFirst 'META-INF/dependencies.txt'
    pickFirst 'META-INF/LGPL2.1'
}

这是不是最好的选择,因为如果你有相同名称不同的许可文件,你将失去一个,但远好于排除一切。



文章来源: Adding httpcore and httpmime libraries to Android project gives an error in running the app