com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.xml

I am creating one app that uses a RestAPI to fetch data, and for that operation I am using retrofit 2, okhttp3 and jackson for parsing json to object, my app also use Firebase Cloud Messaging

when I compile my code it gives me following error & I can't able to run it

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/maven/com.fasterxml.jackson.core/jackson-databind/pom.xml File1: /Users/silent/work/silentinfotech/DoorEye/app/libs/jackson-databind-2.7.2.jar File2: /Users/silent/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.2.2/3c8f6018eaa72d43b261181e801e6f8676c16ef6/jackson-databind-2.2.2.jar

I am using Android Studio 2.1.1 and OS X El Capitan 10.11.2

some Library added in projects libs folder

converter-jackson-2.0.2.jar

jackson-annotations-2.7.0.jar

jackson-core-2.7.2.jar

jackson-databind-2.7.2.jar

My build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.silentinfotech.dooreye"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {

        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'

    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
   // compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.firebase:firebase-messaging:9.0.0'
    compile 'com.firebase:firebase-client-android:2.5.1+'
   // compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

}
apply plugin: 'com.google.gms.google-services'

I also tried adding following in my build.gradle file but it don't work for me

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

also tried invalidate caches & restart, and also rebuild, clean, even I tried with manually deleting caches but still its give me error

I am using firebase Cloud messaging in my project when I remove all the dependency of Firebase Cloud Messaging then project successfully run but when I add FCM dependency it always give error.


Solution 1:

Instead of this

packagingOptions {

        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'

    }

try this

 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'

   }

and more thing

Remove this line

apply plugin: 'com.google.gms.google-services'

from Bottom and add to Top after this apply plugin: 'com.android.application'.

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

Update :

just remove

compile fileTree(dir: 'libs', include: '*.jar')

and apply dependencies.

Solution 2:

Make changes in the gredle, You have to exclude to maven too.

packagingOptions {

    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/maven

}

and if you are using google-play-service you can exclude the annotation like

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile ('com.google.android.gms:play-services:8.1.0'){
    exclude group: 'com.google.guava'
}
}

Or you can try this also

configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}