1

No precise moment when this kind of dex problem started to occure. Clearing project, invalidating cache does not help. It seems like I have problem with intellij annotations, but including or excluding it did not have any effect on it.

When I added solution from this post, the problem appeared to be solved, but after switching flavor, or branch dex problem appears again randomly. Is there any step by step solution, how to get rid of dex problem in general ?

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$CursorType;
Error:com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$CursorType;
Error:  at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
Error:  at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
Error:  at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
Error:  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
Error:  at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
Error:  at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
Error:  at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
Error:  at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
Error:  at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
Error:  at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Error:Execution failed for task ':app:transformDexArchiveWithDexMergerForAlfaDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$CursorType;

Here is my gradle app file, I am using.

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.25.0'
        classpath "io.realm:realm-gradle-plugin:1.0.0"
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
}

def versionPropsCode = 0
def versionPropsName = "1.0.0"
def versionPropsFile = file('version.properties')
def applicationName = "my.app.name"
def applicationIdName = "${applicationGroup}.${applicationName}"
def devApplicationIdName = "${applicationIdName}.dev"
def alfaApplicationIdName = "${applicationIdName}.alfa"
def betaApplicationIdName = "${applicationIdName}.beta"
def rcApplicationIdName = "${applicationIdName}.rc"

android {

    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId applicationIdName
        minSdkVersion 15
        targetSdkVersion 26
        versionCode versionPropsCode
        versionName versionPropsName
        renderscriptTargetApi 15
        renderscriptSupportModeEnabled true
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        setProperty("archivesBaseName", "$applicationId-$versionName-$versionCode")
    }

    dexOptions {
        javaMaxHeapSize "4g"
        preDexLibraries = false
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/services/javax.annotation.processing.Processor'

        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/INDEX.LIST'
    }

    buildTypes {
        debug {}
        release {
        }
    }

    flavorDimensions "api"

    productFlavors {
        dev {
            dimension "api"
            applicationId devApplicationIdName
        }
        alfa {
            dimension "api"
            applicationId alfaApplicationIdName
        }
        beta {
            dimension "api"
        }
        production {
            dimension "api"
            applicationId applicationIdName
        }
    }
    testOptions {
        animationsDisabled = true
    }

}
ext {
    supportLibVersion = '26.1.0'
    firebaseLibVersion = '11.6.2'
    gmsLibVersion = '11.0.2'
    AAVersion = "4.3.1"
    archVersion = "1.0.0"
}

configurations {
    cleanedAnnotations

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

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

    implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true
        exclude group: 'com.crashlytics.sdk.android:crashlytics', module: 'app'
    }

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    implementation "com.android.support:support-annotations:$supportLibVersion"

    implementation 'com.android.support:multidex:1.0.2'
    implementation "com.android.support:appcompat-v7:$supportLibVersion"
    implementation "com.android.support:cardview-v7:$supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$supportLibVersion"
    implementation "com.android.support:support-v4:$supportLibVersion"
    implementation "com.android.support:design:$supportLibVersion"
    implementation "com.android.support:palette-v7:$supportLibVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    implementation "com.google.android.gms:play-services-location:$firebaseLibVersion"
    implementation "com.google.android.gms:play-services-analytics:$firebaseLibVersion"
    implementation "com.google.android.gms:play-services-auth:$firebaseLibVersion"

    implementation "com.google.firebase:firebase-appindexing:$firebaseLibVersion"
    implementation "com.google.firebase:firebase-messaging:$firebaseLibVersion"
    implementation "com.google.firebase:firebase-core:$firebaseLibVersion"
    implementation "com.google.firebase:firebase-database:$firebaseLibVersion"
    implementation "com.google.firebase:firebase-config:$firebaseLibVersion"
    implementation 'com.firebase:firebase-client-android:2.3.1'

    implementation 'com.facebook.android:facebook-android-sdk:4.27.0'
    implementation 'com.facebook.network.connectionclass:connectionclass:1.0.1'

    implementation 'com.daimajia.androidanimations:library:2.3@aar'
    implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
    implementation 'com.daimajia.easing:library:2.1@aar'

    implementation 'com.github.ksoichiro:android-observablescrollview:1.6.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation('com.github.rey5137:material:1.2.4') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
    }

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion") {
        exclude group: 'org.jetbrains', module: 'annotations'
    }

    implementation 'com.squareup.okio:okio:1.13.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

    implementation 'com.jakewharton:butterknife:8.8.1'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    implementation 'eu.inloop:androidviewmodel:1.3.4'
    implementation 'com.wefika:flowlayout:0.4.1'
    implementation 'com.makeramen:roundedimageview:2.2.1'
    implementation 'com.github.chrisbanes:PhotoView:2.1.3'
    implementation 'jp.wasabeef:recyclerview-animators:2.2.7'
    implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
    implementation 'it.sephiroth.android.library.easing:android-easing:1.0.3'
    implementation 'org.apache.commons:commons-lang3:3.5'

    implementation "android.arch.lifecycle:runtime:$archVersion"
    implementation "android.arch.lifecycle:extensions:$archVersion"

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

    cleanedAnnotations 'org.jetbrains:annotations:13.0'

    implementation project(':paper-onboarding-release')

    // Instrumentation test dependencies
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.0') {
        exclude module: 'support-annotations'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-intents:3.0.0') {
        exclude module: 'support-annotations'
    }
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.0'
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestImplementation ('com.github.andrzejchm.RESTMock:android:0.2.2')

    // Unit tests dependencies
    testImplementation 'junit:junit:4.12'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

    androidTestImplementation "com.android.support:support-annotations:$supportLibVersion"

    implementation('com.android.support.test.espresso:espresso-idling-resource:3.0.0')

    implementation 'com.facebook.stetho:stetho:1.5.0'
    implementation('com.facebook.stetho:stetho-okhttp:1.5.0') {
        exclude group: 'com.squareup.okhttp'
    }

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'

    cleanedAnnotations 'org.jetbrains:annotations:13.0'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

project.afterEvaluate {
    project.android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            project.publishing.publications.create(variant.name, MavenPublication) {
                groupId applicationIdName
                version variant.versionName
                artifactId variant.name
                if (output.outputFile.exists()) {
                    artifact(output.outputFile)
                }
            }
        }
    }
}

List<String> artifactoryPublications = new ArrayList<String>()
project.android.applicationVariants.all { artifactoryPublications.add(it.name) }

artifactory {
    contextUrl = "$System.env.ARTIFACTORY_PUBLISH_CONTEXT_URL"
    publish {
        repository {
            repoKey = 'my-repo-key'
            username = "$user"
            password = "$pass"
        }
        defaults {
            publishArtifacts = true
            artifactoryPublications.each { publications(it) }
            properties = ['qa.level': 'basic', 'dev.team': 'core']
            publishIvy = false
            publishPom = false
        }
    }
}

task grantAlfaAnimationPermissions(type: Exec, dependsOn: "installAlfaDebug") {
    group = 'permission'
    description = 'Grant permissions for testing.'

    def absolutePath = file('..') // Get project absolute path
    commandLine "$absolutePath/set_animation_permissions.sh $alfaApplicationIdName".split(" ")
}

task grantProductionAnimationPermissions(type: Exec, dependsOn: "installProductionDebug") {
    group = 'permission'
    description = 'Grant permissions for testing.'

    def absolutePath = file('..') // Get project absolute path
    commandLine "$absolutePath/set_animation_permissions.sh $applicationIdName".split(" ")
}

//https://discuss.kotlinlang.org/t/org-jetbrains-annotations-notnull-problem-with-android-build/629/5
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)
        }
    }
}

// Source: http://stackoverflow.com/q/29908110/112705
afterEvaluate {
    // When launching individual tests from Android Studio, it seems that only the assemble tasks
    // get called directly, not the install* versions
    tasks.each { task ->
        if (task.name.startsWith("assembleAlfaDebugAndroidTest")) {
            task.dependsOn grantAlfaAnimationPermissions
        }
        if (task.name.startsWith("assembleProductionDebugAndroidTest")) {
            task.dependsOn grantProductionAnimationPermissions
        }
    }
}
Malbac
  • 197
  • 1
  • 1
  • 12

3 Answers3

2

Excluding the annotations module within the loaded Kotlin package did the trick for me. However, you're using multiple Kotlin versions here:

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion") {
    exclude group: 'org.jetbrains', module: 'annotations'
}

I would recommend to exclude the annotations within the the non jre7version as well:

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"{
        exclude group: 'org.jetbrains', module: 'annotations'
    }
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion") {
        exclude group: 'org.jetbrains', module: 'annotations'
    }

Also a clean build can resolve various caching errors. If this doesn't work I would recommend to check out a previous version of your project where the gradle build process didn't fail and include the new dependencies step by step. With this approach you can easily find the conflicting packages.

schlenger
  • 1,447
  • 1
  • 19
  • 40
0

I had an sdk that used

com.google.code.findbugs:jsr305:3.0.2

and an app that used:

org.jetbrains:annotations-java5:15.0

Deleting the app dependency resolved the issue.

tonisives
  • 1,962
  • 1
  • 18
  • 17
0

In my case I had this issue " Type org.jetbrains.annotations.Nullable is defined multiple times ". I solved it deleting package " org.jetbrains.annotations" by deleting "org" folder inside res folder of the application.

Vikash Sharma
  • 539
  • 8
  • 13