Android Studio Multiple dex files gradle error

前端 未结 4 1162
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 04:03

I get this error when I Run->app for an Android application in Android Studio

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex file         


        
4条回答
  •  我寻月下人不归
    2021-02-19 04:16

    The com.google.common.annotations package seems to be part of Guava. I see it in the dependencies twice in slightly different variations: once as part of Google API Client, once as your own dependency:

    +--- com.google.api-client:google-api-client:1.19.0
    |    \--- com.google.guava:guava-jdk5:13.0
    

    and

    +--- com.google.guava:guava:14.0.+ -> 14.0.1
    

    So the cause of this error is that you have the same classes defined in multiple dex files (in different variations of the Guava library) being included by your other dependencies. You'll need to find a way to exclude these duplicated dependencies, or possibly just ensure that you use the same version across all dependencies.

    One thing you could try is to exclude the guava module from one of the dependencies. So, where you have the API Client module defined, add an exclusion rule for the guava module:

    compile ('com.google.api-client:google-api-client:1.19.0') {
        exclude group: 'com.google.guava', module: 'guava-jdk5'
    }
    

    I can't guarantee this won't cause problems for the Google API Client library (since they are two different versions of Guava) but it's worth a try.

    EDIT: From your depdencies, try changing this:

    compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
        // Exclude artifacts that the Android SDK/Runtime provides.
        exclude(group: 'com.google.guava')     //-- !!! this does not seem to work !!!
    

    to:

    compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
        exclude(group: 'com.google.guava', module: 'guava-jdk5')
    

    The google-api-client-android library doesn't actually contain Guava -- I didn't realize you had two similarly named dependencies in there.

提交回复
热议问题