Adding httpcore and httpmime libraries to Android project gives an error in running the app

孤人 提交于 2019-12-13 01:38:55

问题


In my Android app I'm using httpcore and httpmime libraries. My build.gradle files dependancy section contains below part,

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

and when I'm running the app gives an error. the log is as shown below,

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

how can i fix this


回答1:


add this in your build.gradle file

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'
    }
}



回答2:


Better to use this one, it will include only one license file, some licenses (most of open source) do not allow to exclude license from final code:

    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'
}

It is not the best option since if you will have different license files with the same name you will loose one, but much better than exclude everything.



来源:https://stackoverflow.com/questions/36978528/adding-httpcore-and-httpmime-libraries-to-android-project-gives-an-error-in-runn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!