Android Studio Multiple dex files gradle error

前端 未结 4 1161
佛祖请我去吃肉
佛祖请我去吃肉 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:10

    Here is the correct solution solution, worked for me. Excluding the guava-jdk5 dependency module from each individual import is counterproductive (for me it didn't work because I had some internal dependencies coming from my backend on which my app depends, and this exposes the real issue with this approach).

    The following solved the problem and is the recommended approach:

    configurations {
        all*.exclude group: 'com.google.guava', module: 'guava-jdk5'
    }
    

    Source: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 04:23

    It seems you can't use both guava and guava-jdk5 in the same project. Guava-jdk5 is still being maintained, so consider changing your project's guava reference to guava-jdk5:

    compile 'com.google.guava:guava-jdk5:17.0'
    
    0 讨论(0)
  • 2021-02-19 04:36

    For those who are using Google Cloud Endpoints in Android app:

    compile(project(path: ':backend', configuration: 'android-endpoints')) {
        exclude(module: 'guava-jdk5')
    }
    

    Where backend is name of your module with AppEngine app.

    In every other case just look for guava-jdk5 transitive dependency and exclude it.

    0 讨论(0)
提交回复
热议问题