Org.jetbrains.annotations.NotNull problem with android build

Hi. I created a java library project and it uses org.jetbrains.annotations.NotNull annotation. (I added annotations.jar from intellij installation.) And I created a kotlin android project uses that java library. When I build kotlin project , dexing fails with "Multiple dex files define Lorg/jetbrains/annotations/Mutable" exception. I found that kotlin-runtime.jar contains its own  org.jetbrains.annotations.NotNull class.

How can I fix this problem?

1 Like

I think you don't need to bundle annotations.jar with your library and/or project. This will fix the problem

This isn’t for libraries bundling annotations.jar, it’s for pure-Java libraries which declare a dependency on the org.jetbrains:annotations:15.0 artifact. Why doesn’t Kotlin instead depend on this artifact so that normal build system dependency resolution can take place?

Kotlin is a library, after all, and so your own advice should apply:

I think you don’t need to bundle annotations.jar with your library and/or project. This will fix the problem

If Kotlin’s runtime embedded Guava you wouldn’t go around saying that libraries shouldn’t be depending on Guava because Kotlin will provide it. You might think that’s a ridiculous argument, and it is, but from the build system’s perspective it doesn’t matter what the library you’re erroneously duplicating on the classpath is, the error is still there.

5 Likes

As @JakeWharton said, I do not think this is related to bundle annotation.jar. I have a group of normal android dev dependencies, but if I add the kotlin-stdlib as a dependency, dexing fails with a “multiple dex files define” message.

I have the same problem, but have fixed it in gradle. The following code is the relevant subpart of my build.gradle (Original build.gradle)

apply plugin: 'com.android.application'

configurations {
    cleanedAnnotations

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

dependencies {

    compile files("${buildDir}/libs/annotations-cleaned.jar") { builtBy 'cleanAnnotationsJar' }

    cleanedAnnotations 'org.jetbrains:annotations:13.0'
}


task cleanAnnotationsJar(type:Jar, dependsOn:configurations.cleanedAnnotations) {
    archiveName = "annotations-cleaned.jar"
    exclude 'org/jetbrains/annotations/NotNull.class'
    exclude 'org/jetbrains/annotations/Nullable.class'

    doFirst {
        configurations.cleanedAnnotations.each { f ->
            project.logger.debug("Found ${f} in cleanedAnnotations")
            from zipTree(f)
        }
    }
}

Basically this creates (on demand) a version of the annotations jar that does not include NotNull and Nullable and effectively replaces the regular transitive dependency on the annotations module by this edited jar.

We’re going to depend on org.jetbrains:annotations:13.0 artifact instead of bundling these annotations.

2 Likes

Great, that should save a lot of headaches for lots of people.