146

Android Studio. I'm getting this kind of error during application run.

Error:Execution failed for task ':app:packageDebug'. Duplicate files copied in APK META-INF/notice.txt

build.gradle

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ASL2.0'
    }

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile 'com.j256.ormlite:ormlite-android:4.48'
    compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
    compile 'com.octo.android.robospice:robospice:1.4.11'
    compile 'com.octo.android.robospice:robospice-spring-android:1.4.11'
}

How can I fix this error?

EDITED

These exclude options solved my problem:

packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Robertas Setkus
  • 3,075
  • 6
  • 31
  • 54
  • possible duplicate of [Duplicate files copied (Android Studio 0.4.0)](http://stackoverflow.com/questions/20673888/duplicate-files-copied-android-studio-0-4-0) – Scott Barta Mar 17 '14 at 22:59
  • 3
    This is not duplicate! Other questioners fixed their error by adding exlude option. But as you all can see I added these options and my problem didn't go away. – Robertas Setkus Mar 18 '14 at 10:22
  • http://tools.android.com/tech-docs/new-build-system – binary Jan 27 '15 at 00:22
  • is there any idea about this issues http://stackoverflow.com/questions/30364950/errorduplicate-files-during-packaging-of-apk-android-studio-error/30365509#30365509 – Jithu P.S May 21 '15 at 06:02
  • That solved mine as welll. thanks – Reshma Aug 24 '15 at 08:14

8 Answers8

56

I think the string comparison is case sensitive. try with exclude 'META-INF/notice.txt'

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • My answer was about trying with `notice.txt` instead of `NOTICE.txt`? Though the error looks actually different from the one where the exclude happens. Do you have a `META-INF/notice.txt` file in your `src/main/resources/` ? – Xavier Ducrohet Mar 18 '14 at 15:48
  • Sorry Xavier. I didn't understood your answer then. Now I can see what did you mean. I tried to change to `exclude 'META-INF/notice.txt'` but then console prompted that `Duplicate files copied in APK META-INF/NOTICE.txt`. Strange, isn't it? There is no `resources` directory under `C:\PathToAndroidStudioProject\src\main`. – Robertas Setkus Mar 18 '14 at 22:04
  • 1
    If you put both `exclude 'META-INF/notice.txt'` and `exclude 'META-INF/NOTICE.TXT'`, does it work? – Xavier Ducrohet Mar 18 '14 at 22:31
  • 1
    in build.gradle file in 'android' part, try to add: `packagingOptions { exclude 'META-INF/ASL2.0' exclude 'META-INF/LICENSE' }` – Krystian Jul 25 '14 at 09:35
  • one more to exclude : exclude 'META-INF/NOTICE' – Krystian Jul 25 '14 at 10:04
  • is there any idea about this issues http://stackoverflow.com/questions/30364950/errorduplicate-files-during-packaging-of-apk-android-studio-error/30365509#30365509 i tried all of your suggestions but still have problem – Jithu P.S May 21 '15 at 06:02
22

I think you need to include only these options in build.gradle:

packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }
Gent
  • 6,215
  • 1
  • 37
  • 40
10

Short Answer:

See the detailed gradle output using gradle assemble and note the files that are duplicate and exclude them using:

android {
  packagingOptions {
    exclude 'META-INF/notice.txt'
  }
}

Long Answer:

Run the assemble gradle task from command line for detailed output:

./gradlew assemble || gradle assemble

This automatically shows what to exclude:

studioWorkspace/CCDroid git:(master) ✗ ± ./gradlew assembleDebug
:app:preBuild
:app:compileDebugNdk UP-TO-DATE
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preDexDebug UP-TO-DATE
:app:dexDebug UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
Error: duplicate files during packaging of APK /Users/shubham/code/studioProjects/CCDroid/app/build/outputs/apk/app-debug-unaligned.apk
    Path in archive: LICENSE
    Origin 1: /Users/shubham/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.14.8/8ac073941721e0b521ec8e8bad088b1e7b8cd332/lombok-1.14.8.jar
    Origin 2: /Users/shubham/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-all/1.8.4/5c97d8b6e715ed941aeb93d6fc401ab3eb18a566/mockito-all-1.8.4.jar
You can ignore those files in your build.gradle:
    android {
      packagingOptions {
        exclude 'LICENSE'
      }
    }
:app:packageDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK LICENSE
    File 1: /Users/shubham/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.14.8/8ac073941721e0b521ec8e8bad088b1e7b8cd332/lombok-1.14.8.jar
    File 2: /Users/shubham/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.14.8/8ac073941721e0b521ec8e8bad088b1e7b8cd332/lombok-1.14.8.jar


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 11.863 secs

See this part in output:

android {
  packagingOptions {
    exclude 'LICENSE'
  }
}

It even shows the list of dependencies which originated duplicate files (LICENSE). See the lines with Origin # in the output.

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
  • is there any idea about this issues http://stackoverflow.com/questions/30364950/errorduplicate-files-during-packaging-of-apk-android-studio-error/30365509#30365509 – Jithu P.S May 21 '15 at 06:01
  • 1
    I almost ignored your answer, even though it was the correct one for me. I would suggest placing the answer at the top of the post : android { packagingOptions { exclude 'LICENSE' } } followed by the explanation and the long gradle output (which is mostly not relevant to the answer. This part is You can ignore those files in your build.gradle: android { packagingOptions { exclude 'LICENSE' } } – HRVHackers Jul 17 '15 at 01:57
  • Done. Thanks a lot @gorbysbm. – Shubham Chaudhary Jul 17 '15 at 05:06
6

Add this in your build.gradle file ...

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

like this...

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "your package name"
    minSdkVersion 14
    targetSdkVersion 25
    versionCode 30
    versionName "3.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/NOTICE' // will not include NOTICE file
    exclude 'META-INF/LICENSE' // will not include LICENSE file
    exclude 'META-INF/DEPENDENCIES' // will not include LICENSE file
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
vrbsm
  • 1,188
  • 15
  • 22
3

It's more than one error, you are right.

Under apply plugin: 'android-library'

add this :: android { packagingOptions { exclude 'META-INF/ASL2.0' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' } }

1st error is by file duplicates, 2nd is by LICENSE and NOTICE files. It will work after

EDIT:: See my post here about identifying the problems:: Android Gradle plugin 0.7.0: "duplicate files during packaging of APK"

Community
  • 1
  • 1
Pian0_M4n
  • 2,505
  • 31
  • 35
  • is there any idea about this issues http://stackoverflow.com/questions/30364950/errorduplicate-files-during-packaging-of-apk-android-studio-error/30365509#30365509 – Jithu P.S May 21 '15 at 06:01
1

this works for me

android {
...

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
    }
}
Thiago
  • 12,778
  • 14
  • 93
  • 110
1

Interestingly, when I deleted this line from gradle, it worked:

compile 'org.apache.commons:commons-lang3:3.3.1'
Lazy
  • 1,807
  • 4
  • 29
  • 49
0

I had to play around a bit to find the right location for the packagingOptions. Solving the duplicate file problem for a conflict between jackson-core:2.2.2 and jackson-databind:2.2.2 Also the DSL seems to have changed recently So in the recent Android Studio 1.4.1 with buildTools 23 you have to put android.packaging options on the same level as compileOptions AND NOT inside any model { android { braces !

model { 
   android {
        compileSdkVersion = 21
        buildToolsVersion = "23.0.1"

        defaultConfig.with {
              applicationId = "com.android.sensorgraph"
              minSdkVersion.apiLevel = 21
              targetSdkVersion.apiLevel = 22
        }
   }

   android.packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
   }
   compileOptions.with {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
   }
} 
Karthikeyan
  • 196
  • 2
  • 10
U.V.
  • 572
  • 4
  • 11