多个 dex 文件定义了 Lcom/google/firebase/FirebaseException。

24

我在 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'

在项目构建过程中,我遇到了以下错误:

Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/FirebaseException;

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

谢谢。

9个回答

14

我曾经在使用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错误。


3
我也遇到了react-native-push-notification库的这个问题。 - Brian F
由于AirBnB的react-native-maps,我也遇到了这个问题。 - mxcl
谢谢,我已经创建了一个PR => https://github.com/joonhocho/react-native-google-sign-in/pull/25 - SaroVin

5

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
}

1
谢谢,这对我有用!!! 我在使用RNFirebase的Firestore时遇到了问题。 - Ashiq Muhammed

3

看起来你已经达到了方法计数限制。尝试移除firebase依赖项并检查应用程序的方法计数(例如,使用this gradle plugin)。如果不删除这些依赖项,则无法构建项目,因此要使用方法计数插件。

Firebase是一个庞大的库-17k+方法。它依赖于大量的东西。您可以通过在“methodscount.com”上单击此按钮来检查依赖项列表: enter image description here

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

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

如果这不起作用,那么您可能需要为您的项目配置multidex

2

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

但是,我遇到了Multiple dex files define 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 & gradlew clean & cd .. ,确保没有错误后再运行 react-native run-android 命令。希望能对您有所帮助。


1
如果有人需要帮助的话,我遇到了类似的问题,是由于Gradle插件为Google服务引入的依赖项与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 插件,问题就解决了。我也可以找到正确的版本 :)


0

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

检查您最后添加的库并将其排除在外。对于我的项目来说,那就是 'react-native-firestack'。

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

0
感谢这篇文章,只需检查并升级您的Google依赖版本到最新发布版。
我解决了我的问题。问题是BaseGameUtils仍在使用/引用旧版本的play-services。添加了正确的版本,现在它可以工作了。猜想下一个项目我会省略BaseGameUtils。

0
我在使用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'
        }
    }
}

-2
请将以下代码添加到 Android 的 build.gradle 文件中。
dexOptions {
    preDexLibraries = false
}

错误:将字节码转换为dex时出错: 原因:java.lang.RuntimeException:翻译已被中断 - User

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接