Error converting bytecode to dex: Multiple dex files, Android Studio 3.0

早过忘川 提交于 2020-01-06 06:55:53

问题


I recently upgraded to Android Studio 3.0, and while running the program I get the following error:

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/vuforia/TransformModel;
Error:com.android.dex.DexException: Multiple dex files define Lcom/vuforia/TransformModel;
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:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/vuforia/TransformModel;

Apart from my main app module, I have two other modules in my project. I already tried

  • clean and Make/rebuild,
  • Use the latest build tools and Java versions,
  • predex to false,
  • deleting .gradle folder, but none of them work.

Can anyone help me ?

app Module:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.example.shais.gpsdetector"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        resValue "string", "google_maps_key", (project.findProperty("GOOGLE_MAPS_API_KEY") ?: "")
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
        preDexLibraries = false
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.google.android.gms:play-services-location:10.2.4'
    compile 'com.google.android.gms:play-services-places:10.2.4'
    compile 'com.google.android.gms:play-services-maps:10.2.4'
    compile 'com.google.maps.android:android-maps-utils:0.5+'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    implementation 'com.cocoahero.android:geojson:1.0.1'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'com.android.support:design:25.3.1'
    implementation project(':vuforia')
    implementation project(':rajawali')
}

Vuforia Module:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        preDexLibraries = false
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile files('libs/Vuforia.jar')
}

rajawali Module:

apply plugin: 'com.android.library'
apply plugin: 'jacoco'
apply plugin: 'signing'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            testCoverageEnabled true
            minifyEnabled false
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }   
    lintOptions {
        abortOnError false
    }
}

dependencies {   
    androidTestCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'org.jetbrains:annotations-java5:15.0'
}

回答1:


Finally, I was able to solve the problem. The error arose because of the two paths of Vuforia jar in Vuforia gradle. The following two lines in Vuforia Gradle are actually pointing to the same library jar.

compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/Vuforia.jar')

I simply removed the second compilation of jar and it removed my error.




回答2:


First, try to remove the .gradle folder in your project. Then you need to check for the dependencies from the dependency tree with the following command in Linux:

./gradlew app:dependencies

or if you're using Windows try this:

gradlew.bat app:dependencies

If you found duplicated dependencies in the list, you can exclude it with something like this:

compile('com.library.name:version') {
  exclude group: 'com.example', module: 'library'
}


来源:https://stackoverflow.com/questions/46986592/error-converting-bytecode-to-dex-multiple-dex-files-android-studio-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!