Duplicate files while including butterknife with gradle
Asked Answered
B

4

31

I have a basic Android app that I created with Android Studio, and I'm having problems adding butterknife to my build. I get this error:

Error:: duplicate files during packaging of APK C:\apps\orion\app\build\apk\app-debug-unaligned.apk
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK META-INF/services/javax.annotation.processing.Processor
    File 1: C:\Users\andres\.gradle\caches\modules-2\files-2.1\com.jakewharton\butterknife\4.0.1\f43b36925363701633d01adb8e54df7150397a78\butterknife-4.0.1.jar
    File 2: C:\Users\andres\.gradle\caches\modules-2\files-2.1\com.jakewharton\butterknife\4.0.1\f43b36925363701633d01adb8e54df7150397a78\butterknife-4.0.1.jar

My dependencies look like this:

dependencies {

    compile 'com.android.support:support-v4:+'
    compile 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.squareup.dagger:dagger:1.2.1'
    compile 'com.jakewharton:butterknife:4.0.1'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile 'com.android.support:appcompat-v7:+'
    compile project(':lib')
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
Burseraceous answered 16/3, 2014 at 15:58 Comment(4)
FYI dagger-compiler should use the 'provided' scope for the dependency declaration.Fuze
This is one of those SO questions that have the wrong accepted answer. Listen to THE Jake Wharton!Skunk
@lemuel what? the accepted answer is by Xavier Ducrohet, who is the head of Android Tooling at GooglePhare
@AdamBurley I know who Xavier is. But look up Jake Wharton and Dagger, in the context of 2014. Also, look at how old this is. Also, look at the comments under Xavier Ducrohet's answer. I encountered this issue 7 years ago, and Nima G's answer was what worked.Skunk
U
82

Later versions of the plugin will tell you how to fix this. I think we introduced the fix in 0.8 so you should probably upgrade. Then the fix is to put this in your build.gradle

android {
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

This will exclude this file from the packaging which is fine since it's not actually needed in the APK.

Unthankful answered 16/3, 2014 at 18:31 Comment(5)
Thanks, that worked. Could you tell me which plugin should I upgrade?Burseraceous
This doesn't seem to be fixed in gradle 0.8 unfortunately :-( This workaround is still required.Chitterlings
This is not a workaround. This is the normal solution. With 0.9.2 you can also decide to package the one of the file. See tools.android.com/tech-docs/new-build-systemUnthankful
I'm using 0.9 and still the same situation.Saprophyte
Version 1.2 reporting. Still no fix.Cymar
L
15

That's because you wrote compile for dagger-compiler, replace it with provided and the issue will be fixed.

compile 'com.squareup.dagger:dagger:1.2.1'
provided 'com.squareup.dagger:dagger-compiler:1.2.1'
Lanfri answered 24/3, 2015 at 19:43 Comment(4)
Instead of exclude and stuff this is the best answer !Compensatory
So apt needs to be replaced with provided?Redletter
@Redletter you can use apt as well, but only for the compilerLanfri
@Lanfri Incidentally, Jack compiler in Android N sdk doesn't support apt. See code.google.com/p/android/issues/detail?id=203850Redletter
K
7

The best option in version >= 0.9.1 of Gradle build tools is probably:

android {
    packagingOptions {
        pickFirst 'META-INF/services/javax.annotation.processing.Processor'
    }
}

For more, see the Android Tools Project page: New Build System.

Edit: One last note here if you start having problems with generated code, make sure to structure your dependencies properly. I ended up removing any exclusion of the Processor line and structuring my annotation processed dependencies like:

compile "org.parceler:parceler-api:0.2.15"
apt "org.parceler:parceler:0.2.15"

and

provided 'com.squareup.dagger:dagger-compiler:1.2.2'
apt 'com.squareup.dagger:dagger-compiler:1.2.2'
Kanal answered 18/2, 2015 at 1:7 Comment(0)
C
3

If after applying above given solutions you still face the same issue as I was, then if you are using glide library then change the version of the glide to it's max. eg.

implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
Carnage answered 9/10, 2019 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.