3

Am getting an unexpected error in Android studio while running the app,following is the error message which am getting

Error:duplicate files during packaging of APK /home/jithu/libs/android/android/aa/app/build/outputs/apk/app-debug-unaligned.apk Path in archive: META-INF/DEPENDENCIES Origin 1: /home/jithu/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpmime/4.3.6/cf8bacbf0d476c7f2221f861269365b66447f7ec/httpmime-4.3.6.jar Origin 2: /home/jithu/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.4.1/f5aa318bda4c6c8d688c9d00b90681dcd82ce636/httpcore-4.4.1.jar

Am pasting my build.gradle file also

 apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.15'
    compile 'com.google.code.gson:gson:2.2.4'
    compile "org.apache.httpcomponents:httpcore:4.4.1"
    compile "org.apache.httpcomponents:httpmime:4.3.6"


}
android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    // ...
}
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32
  • 1
    remove `compile "org.apache.httpcomponents:httpmime:4.3.6"` and try.. – M D May 21 '15 at 05:01
  • i did it,but that time its allows me to compile but giving following error java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType 05-20 18:44:58.485: E/AndroidRuntime(395): at org.apache.http.entity.mime.content.FileBody.(FileBody.java:89) – Jithu P.S May 21 '15 at 05:04
  • Probably duplicate : http://stackoverflow.com/questions/20673888/duplicate-files-copied-android-studio-0-4-0 – Haresh Chhelana May 21 '15 at 05:05
  • @HareshChhelana you can see i already added that exclude statements.still its giving me the error,and am using the latest version of studio. – Jithu P.S May 21 '15 at 05:09
  • @MD if i remove the httpmime we will get following class miss errors import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; these are missing – Jithu P.S May 21 '15 at 05:11
  • @JithuP.S But i guess `httpcore` contains `httpmime` – M D May 21 '15 at 05:12
  • @MD i think no,that why its giving me class miss error import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; – Jithu P.S May 21 '15 at 05:13
  • @JithuP.S Try [this answer](http://stackoverflow.com/questions/29741189/image-uploading-using-multipart-entity-error/29741403#29741403). It has worked for me. – Exception May 21 '15 at 05:17
  • @Exception i tried your answer also but same issue – Jithu P.S May 21 '15 at 05:29
  • @JithuP.S You got NoclassDefFoundError or duplicate file error? – Exception May 21 '15 at 05:33
  • @JithuP.S For what purpose are you using these apache libraries? – Exception May 21 '15 at 05:37
  • @Exception if i remove the httpmime then only getting the NoclassDefFoundError ,if am using the both libs am getting duplicate error,am using this libs for making header for volley request – Jithu P.S May 21 '15 at 05:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78375/discussion-between-jithu-p-s-and-exception). – Jithu P.S May 21 '15 at 05:56

1 Answers1

5

Well you are missing the flow of build gradle. As you are trying to use 2 android {..} snippet it is not detecting the 2nd one. As a result your exclusion of duplicate meta files are not working.

Possible solution:

Just change the order of your build gradle like below:

    apply plugin: 'com.android.library'    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
        }
    }
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.15'
    compile 'com.google.code.gson:gson:2.2.4'
    compile "org.apache.httpcomponents:httpcore:4.4.1"
    compile "org.apache.httpcomponents:httpmime:4.3.6"


}

If it still didn't work then check out the following:

Is it your library build gradle? I excluded my duplicate meta files from my main projects build gradle. So make sure to exclude the meta files from build gradle of app module rather than library module.

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
  • Did you moved the 'packagingOptions{..}' section in your projects build gradle from your library build gradle. Did you noticed, there are multiple build gradle files in your project. – Mohammad Arman May 21 '15 at 06:18
  • Now its ok,i changed the exclude from library to app – Jithu P.S May 21 '15 at 07:05
  • but i have a question,by excluding this files is it make any issues on run-time or it will affect any functionality? – Jithu P.S May 21 '15 at 07:06
  • 1
    Nope! Meta-files doesn't affect any programmatic functions of application. Meta files basically contains Textual information like legal-notice, Licences etc of open sources libraries. Excluding it will not affect any thing. In the next Comment I am also going to share why we have exclude it. – Mohammad Arman May 21 '15 at 07:13
  • what about exclude 'META-INF/DEPENDENCIES' ? – Jithu P.S May 21 '15 at 07:14
  • 1
    When we use multiple 3rd party open source libraries, sometimes 2 or more projects has same named text files (Example: License.txt or Notice.txt or dependencies.txt). That causes the conflict during build time. In that moment our mighty android studio suggest us to exclude those conflicting meta files. – Mohammad Arman May 21 '15 at 07:16
  • for me it worked with license.txt and notice.txt in small caps, packagingOptions { exclude 'META-INF/license.txt' exclude 'META-INF/notice.txt' } – Bharat Dodeja Dec 10 '15 at 07:38
  • @BharatDodeja Yes, it depends on the conflicting file name. Whatever file create conflict in the META-INF directory is needed to be excluded in the packagingOptions – Mohammad Arman Dec 10 '15 at 08:21