Android Gradle Duplicate files copied in APK META-INF/license.txt
Asked Answered
H

6

75

I'm going to add RESTful Web Service support with Spring to my Android application as described here https://spring.io/guides/gs/consuming-rest-android/.

This is top-level build.gradle config:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

This is my app/build.gradle config:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 8
        targetSdkVersion 17
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    dependencies {
        compile 'com.android.support:appcompat-v7:+'
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
    }

    packagingOptions {
        exclude 'META-INF/NOTICE' // will not include NOTICE file
        exclude 'META-INF/LICENSE' // will not include LICENSE file
    }

}

Right now it fails with a following error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/license.txt
    File1: user\.gradle\caches\modules-2\files-2.1\org.springframework.android\spring-android-rest-template\1.0.1.RELEASE\e132d929bd181941f79b0d63edafb8a86ae6fd33\spring-android-rest-template-1.0.1.RELEASE.jar
    File2: user\.gradle\caches\modules-2\files-2.1\org.springframework.android\spring-android-core\1.0.1.RELEASE\e68f0e8e4b636ee30c4de58953be38d9b72a5e3b\spring-android-core-1.0.1.RELEASE.jar

What am I doing wrong and how to fix it ?

Heckman answered 2/6, 2016 at 8:42 Comment(1)
Possible duplicate of How do I resolve "Duplicate files copied in APK META-INF/*"Margetmargette
E
252

Write below lines in your app level 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'
    }
}
Extrusive answered 2/6, 2016 at 8:45 Comment(10)
I have updated my question with these lines.. For a some reason it doesn't helpHeckman
Ok.. Good to know :)Extrusive
OMG how people leaved without Stackoverflow? It would take ages to find this solution. Thank you strangerThou
Will anyone explain what those lines do briefly pleaseLeibniz
Spent two hours trying to getting my tests working on my android project.Didache
It doesn't remove "duplicate" files. It excludes the listed files without any further checks. In most cases this "solution" is illegal.Carvelbuilt
God Sets Up Your Work... ;-)Clove
@TheincredibleJan do you have any legal suggestions? As I have been searching for hours but couldn't find any solutionThames
With this solution you violate the licence rules. Check my answer above.Planet
In my case, I added exclude 'META-INF/ASL2.0' also along with the above set. Then it worked.Guffaw
P
11

If the solution in the selected answer does not fix your problem, try adding

    exclude 'META-INF/ASL2.0'

as well. Or basically, identify the name of the duplicate file and exclude it. The addition above fixed the issue for me.

Psychophysiology answered 4/1, 2017 at 10:13 Comment(0)
P
5

Its helps me. Try this code:)

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
}
Proptosis answered 14/12, 2016 at 4:12 Comment(0)
G
0

adding these lines helped me.

shadowJar {
    // Filtering shadow jar contents by file pattern.
    exclude 'schemaorg_apache_xmlbeans/attribute/**'
    exclude 'schemaorg_apache_xmlbeans/attributegroup/**'
    exclude 'schemaorg_apache_xmlbeans/element/**'
    exclude 'schemaorg_apache_xmlbeans/identityconstraint/**'
    exclude 'schemaorg_apache_xmlbeans/javaname/**'
    exclude 'schemaorg_apache_xmlbeans/modelgroup/**'
    exclude 'schemaorg_apache_xmlbeans/namespace/**'
    exclude 'schemaorg_apache_xmlbeans/src/**'
    exclude 'schemaorg_apache_xmlbeans/system/sXML*/**'
    exclude 'schemaorg_apache_xmlbeans/system/s8C3F193EE11A2F798ACF65489B9E6078/**'
    exclude 'schemaorg_apache_xmlbeans/type/**'

    exclude 'repackage/**'

    exclude 'LICENSE.txt'
    exclude 'NOTICE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/maven/**'

    // these are only needed when handling Visio files, remove it if you would like to use Visio support
    dependencies {
        exclude(dependency('com.github.virtuald:curvesapi'))
        exclude(dependency('commons-codec:commons-codec'))
    }

    // Relocate javax dependencies so Android does not choke
    relocate 'javax.xml.namespace', 'org.apache.poi.javax.xml.namespace'
    relocate 'javax.xml.stream', 'org.apache.poi.javax.xml.stream'
    relocate 'javax.xml.XMLConstants', 'org.apache.poi.javax.xml.XMLConstants'
}
Gloriole answered 12/6, 2017 at 21:25 Comment(0)
N
0

This one worked for me:

    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'
    exclude 'META-INF/spring.schemas'
    exclude 'META-INF/spring.tooling'
    exclude 'META-INF/INDEX.LIST'
    exclude 'META-INF/spring.handlers'
}
configurations {
    all*.exclude module: 'classworlds'
    all*.exclude module: 'commons-logging'
    all*.exclude module: 'httpclient'
    all*.exclude module: 'maven-artifact'
    all*.exclude module: 'maven-artifact-manager'
    all*.exclude module: 'maven-error-diagnostics'
    all*.exclude module: 'maven-model'
    all*.exclude module: 'maven-project'
    all*.exclude module: 'maven-settings'
    all*.exclude module: 'plexus-container-default'
    all*.exclude module: 'plexus-interpolation'
    all*.exclude module: 'plexus-utils'
    all*.exclude module: 'wagon-file'
    all*.exclude module: 'wagon-http-lightweight'
    all*.exclude module: 'wagon-provider-api'

}
Ninon answered 3/10, 2017 at 19:43 Comment(0)
P
0

Use

packagingOptions {
   merge'META-INF/license.txt'
}

or if they are the same

packagingOptions {
   pickFirst  'META-INF/license.txt'
}
Planet answered 17/2, 2022 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.