Kotlin gradle.build ZipException because of duplicates

I add Kotlin to the gradle.build with dependency (Kotlin version 1.0.4):

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

As I found out it has a conflict with my other dependency:

compile 'org.jetbrains:annotations-java5:15.0'

After I run the build it crashes with the ZipException:

com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/jetbrains/annotations/Nullable.class

What I already tried:

I tried this solution:

But it ended up, that not only Nullable.class and NotNull.class were duplicates, but also classes from org/intellij/lang/annotations/.

After I added five classes it still crashed. Is there a better way to get rid of this problem, by not adding every class manually that causes this exception?

UPDATE (02.02.16)

As I worked a bit on the problem, I found out that for my project the relevant annotation classes are NonNls.class and Contract.class that are missing, if I remove org.jetbrains:annotations-java5:15.0. They are not in kotlin-runtime, but in the Jetbrains annotation dependency. How can I add it without adding the Jetbrains annotation dependency again? Or what about adding the whole library to Kotlin?

Take a look at my reply half a year ago. That is in an android context, but there is nothing android-specific to the solution:

I see, that is where the stackoverflow answer comes from as well. The amount of classes should be fairly limited so I would stick with manually enumerating them. Of course you could configure gradle with a filter (instead of manual exclusions) and in that filter check for containment of the name in kotlin-stdlib → better yet, cache this somehow as it’s not really fast.

Could you be more explicit, what other classes in particular are duplicates (except Nullable, NotNull, Mutable and ReadOnly)?

Other classes are for example Flow.class or Identifier.class.

You should examine from which dependencies these two annotation classes are coming from, because kotlin-runtime bundles only those 4 annotations, and neither Flow nor Identifier are in that list.

Most likely the conflict is between 'org.jetbrains:annotations-java5:15.0' and 'org.jetbrains:annotations:15.0'.

You can use dependenies gradle task to inspect the dependency graph of your project.

Thanks for your answer. I only have 'org.jetbrains:annotations-java5:15.0' as a dependency and not the other one. I updated my question, because I’m at the point to drop the whole dependency.

Hey @ilya.gorbunov I’m having the build error

Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> java.io.IOException: Can't write [/home/imedrix-server/StudioProjects/kardioscreen-operatorapp/app/build/intermediates/multi-dex/debug/componentClasses.jar] (Can't read [/home/imedrix-server/StudioProjects/kardioscreen-operatorapp/app/build/intermediates/transforms/desugar/debug/76.jar(;;;;;;**.class)] (Duplicate zip entry [76.jar:org/intellij/lang/annotations/Flow.class])) 

I added kotlin library
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

to the existing Android Java project then only I’m getting the error. If I remove there is no error.

Please help me to solve the issue.

@praveenpro It looks like you have class org.intellij.lang.annotations.Flow coming from several jars. One of them is coming from org.jetbrains:annotations maven artifact which is a transitive dependency of org.jetbrains.kotlin:kotlin-stdlib-jre7 and to find the other, you can run gradlew dependency to show the dependency graph of your project.
Then you may want to exclude one of the conflicting artifacts: java - How do I exclude all instances of a transitive dependency when using Gradle? - Stack Overflow

1 Like

Thanks a lot @ilya.gorbunov The issue is solved by adding the line

configurations {
        compile.exclude group : 'org.jetbrains' , module : 'annotations'
    }