多个 dex 文件定义 Lorg/intellij/lang/annotations/JdkConstants$CursorType

Multiple dex files define Lorg/intellij/lang/annotations/JdkConstants$CursorType

此类 dex 问题开始出现的具体时间不详。 清除项目,使缓存无效无济于事。 似乎我对 intellij 注释有问题,但包括或排除它对它没有任何影响。

当我从这个post添加解决方案时,问题似乎已经解决,但是在切换风味后,或者分支dex问题再次随机出现。是否有任何分步解决方案,一般如何摆脱 dex 问题?

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;

这是我正在使用的 gradle 应用程序文件。

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: 
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
        }
    }
}

在加载的 Kotlin 包中排除 annotations 模块对我有用。但是,您在这里使用了多个 Kotlin 版本:

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

我建议也排除非 jre7 版本中的注释:

    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'
    }

另外,干净的构建可以解决各种缓存错误。如果这不起作用,我会建议检查 gradle 构建过程没有失败的项目的先前版本,并逐步包含新的依赖项。使用这种方法,您可以轻松找到冲突的包。

我有一个使用

的 sdk

com.google.code.findbugs:jsr305:3.0.2

和一个应用使用了:

org.jetbrains:注释-java5:15.0

删除应用依赖项解决了问题。

在我的例子中,我遇到了这个问题“Type org.jetbrains.annotations.Nullable is defined multiple times”。 我通过删除应用程序的 res 文件夹中的“org”文件夹解决了删除包“org.jetbrains.annotations”的问题。