【问题标题】:Multiple dex files define Lcom/google/firebase/FirebaseException多个 dex 文件定义 Lcom/google/firebase/FirebaseException
【发布时间】:2016-11-06 13:36:15
【问题描述】:

我遇到了与 Firebase 集成有关的问题。首先,我在根级build.gradle文件中添加了规则:

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

以及模块 Gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 2
        versionName "0.9"
    }
    buildTypes {
       ///
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-crash:9.0.2'
}

apply plugin: 'com.google.gms.google-services'

在构建项目期间,我收到错误:

错误:将字节码转换为dex时出错:原因: com.android.dex.DexException: 多个 dex 文件定义 Lcom/google/firebase/FirebaseException;

错误原因很清楚,但我没有编译任何库两次。我应该手动从构建过程中排除FirebaseException 类吗?如果是这样,怎么做?也许这是 Firebase 依赖项中的错误?

谢谢。

【问题讨论】:

    标签: android gradle firebase android-gradle-plugin build.gradle


    【解决方案1】:

    react-native-google-signin 模块有这个问题。由于如何修改build.gradle 的说明通常不是最新的、不完整的或只是在多个不相关的项目中定义,因此该项目仅在从react-native-google-signin 示例项目复制设置后编译。事实证明,语句的顺序和exclude group 命令一样重要。最终结果看起来像这样(app/build.gradle):

    dependencies {
        ...
        compile 'com.google.android.gms:play-services-auth:9.2.1'
        compile(project(":react-native-google-signin")) {
            exclude group: "com.google.android.gms"
        }   
    }
    
    task copyDownloadableDepsToLibs(type: Copy) {
       from configurations.compile
       into 'libs'
    }
    
    apply plugin: 'com.google.gms.google-services'
    

    顶部的build.gradle 像往常一样包含一个额外的gms 类路径:

    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.2.2'
            classpath 'com.google.gms:google-services:3.0.0'
        }
    }
    

    在这些更改构建完成后,没有任何Multiple dex 错误。

    【讨论】:

    【解决方案2】:

    FireBase 是一个庞大的库,因此您需要在应用程序中启用 multidex 支持。

    dependencies {
        compile ('com.google.firebase:firebase-core:9.0.2') {
            exclude module: 'play-services-base'
            exclude module: 'support-v4'
            exclude module: 'support-annotations'
        }
        compile 'com.android.support:multidex:1.0.1'
    }
    
    defaultConfig {
            // Enabling multidex support.
            multiDexEnabled true
    }
    

    【讨论】:

    • 谢谢,这对我有用!!!!我在使用 RNFirebase 的 Firestore 时遇到了这个问题
    【解决方案3】:

    您似乎已联系到methods count limit。尝试删除firebase 依赖项并检查您的应用程序的方法计数(例如,使用this gradle plugin(如果您不删除这些依赖项,您将根本无法构建您的项目,因此,使用这些方法计数插件)。

    Firebase is a HUGE library - 17k+ 方法。这取决于很多东西。您可以做的一件事是通过单击“methodscount.com”上的此按钮来检查依赖项列表:

    如果您的项目中已经有其中一些,您可以尝试排除它们:

    compile ('com.google.firebase:firebase-core:9.0.2') {
        exclude module: 'play-services-base'
        exclude module: 'support-v4'
        exclude module: 'support-annotations'
    }
    

    如果这没有帮助,那么您可能想为您的项目 configure multidex

    【讨论】:

      【解决方案4】:

      我正在使用 react-native-mapsreact-native-google-signin。

      而且,我得到多个 dex 文件定义 Lcom/google/firebase/FirebaseException

      请说出我的解决方案。

      打开 build.gradle (react-native-maps)

      dependencies {
           provided "com.facebook.react:react-native:+"
           compile "com.google.android.gms:play-services-base:10.2.4"
           compile "com.google.android.gms:play-services-maps:10.2.4"
      }
      

      版本是10.2.4

      继续打开 build.gradle (react-native-google-signin)

      dependencies {
          compile fileTree(include: ['*.jar'], dir: 'libs')
          compile "com.android.support:appcompat-v7:23.0.1"
          compile 'com.google.android.gms:play-services-auth:9.2.1' <- change here
          compile "com.facebook.react:react-native:+"
      }
      

      它使用版本 9.2.1,这就是原因。

      将其更改为 10.2.4 版本将是

      compile 'com.google.android.gms:play-services-auth:10.2.4'
      

      接下来,打开 build.gradle(app) 并添加一个新的

      compile 'com.google.android.gms:play-services-auth:10.2.4'
      

      现在你有了。

      compile 'com.google.android.gms:play-services-auth:10.2.4'
      compile(project(":react-native-google-signin")){
          exclude group: "com.google.android.gms" 
      }
      

      运行命令cd android &amp; gradlew clean &amp; cd .. util 没有错误然后运行react-native run-android。 希望能帮上忙。

      【讨论】:

        【解决方案5】:

        万一它对任何人有帮助,我遇到了类似的问题,这是由谷歌服务的 Gradle 插件引起的,它引入了与 Firebase 冲突的依赖项。

        在我的顶级 build.gradle 中,在 buildscript 中:

        classpath 'com.google.gms:google-services:3.0.0'
        

        在我的应用程序的 build.gradle 中引入了(自动)与之冲突的依赖项:

        compile 'com.firebaseui:firebase-ui-auth:2.2.0'
        

        有点令人困惑,因为我只有一个编译依赖项,并且正在摸不着头脑。

        我删除了 google-services gradle 插件,它解决了这个问题。我想我也可以找到正确的版本:)

        【讨论】:

          【解决方案6】:

          我在使用 firebase-ui:2.0.0 时遇到了这个错误。我设法通过降级到'com.firebaseui:firebase-ui:1.2.0' 来解决它,并在项目级别的 build.gradle 中添加了以下行:

          allprojects {
              repositories {
                  jcenter()
          
                  // Add the following
                  maven {
                      url 'https://maven.fabric.io/public'
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:

            感谢this post,只需检查您的 google 依赖项版本并将其升级到最新版本即可。

            我可以解决我的问题。问题是 BaseGameUtils 仍然 使用/引用旧版本的播放服务。添加了正确的 版本,现在可以使用了。猜猜我下一个会省略 BaseGameUtils 项目。

            【讨论】:

              【解决方案8】:

              这是因为您的某些库使用不同版本的其他库。

              检查您最后添加的库并排除。 对于我的“react-native-firestack”项目。

              compile(project(':react-native-firestack')){
                   exclude group: "com.google.android.gms" // very important
              }
              

              【讨论】:

                【解决方案9】:

                请将此代码添加到 android 的 build.gradle 中

                dexOptions {
                    preDexLibraries = false
                }
                

                【讨论】:

                • 错误:将字节码转换为dex时出错:原因:java.lang.RuntimeException:翻译已中断
                猜你喜欢
                • 2014-05-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-07-01
                • 2018-01-15
                • 2017-08-14
                • 1970-01-01
                相关资源
                最近更新 更多