Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple dex files define Lcom/google/firebase/FirebaseException

I encountered a problem with the Firebase integration. First of all, I have added rules to the root-level build.gradle file:

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
}

And the module Gradle file:

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'

During the build of the project, I get the error:

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

Error reason is clear, but I didn't compile any library twice. Should I exclude FirebaseException class from the build process manually? If so, how? Perhaps this is a bug within the Firebase dependencies?

Thanks.

like image 281
Mark Korzhov Avatar asked Jul 04 '16 19:07

Mark Korzhov


5 Answers

I had this problem with react-native-google-signin module. As the instructions how to modify build.gradle are often not up to date, incomplete or just defined in multiple unrelated projects the project compiled only after copying the settings from the react-native-google-signin example project. It turns out the order of statements is important as well as exclude group command. The final result looked like this (in 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'

The top build.gradle included an additional gms classpath as usual:

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

After these changes build finished without any Multiple dex errors.

like image 176
mp31415 Avatar answered Oct 22 '22 11:10

mp31415


FireBase is a huge library, so you need to enable multidex support in your application.

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
}
like image 26
Kasun Wanniarachchi Avatar answered Oct 22 '22 10:10

Kasun Wanniarachchi


Looks like you have reached methods count limit. Try to remove firebase dependencies and check methods count for your app (for example, with this gradle plugin (if you don't remove these dependencies you won't be able to build your project at all, thus, to use the methods count plugin).

Firebase is a HUGE library - 17k+ methods. It depends on tons of stuff. One thing you can do is to check dependencies list by clicking this button on "methodscount.com": enter image description here

If you already have some of these in your project you can try to exclude them:

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

If this doesn't help then you might want to configure multidex for you project.

like image 3
Denis Kniazhev Avatar answered Oct 22 '22 11:10

Denis Kniazhev


I am using react-native-maps and react-native-google-signin.

And, I got Multiple dex files define Lcom/google/firebase/FirebaseException

Bellow my solution.

Open 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"
}

The version is 10.2.4

Continue open 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:+"
}

It uses version 9.2.1, and this is reason.

Change it to version 10.2.4 will be

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

Next, open build.gradle (app) and add a new one

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

Now you have.

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

Run command cd android & gradlew clean & cd .. util no error then run react-native run-android. Hope can help.

like image 2
Tuan Nguyen Avatar answered Oct 22 '22 10:10

Tuan Nguyen


In case it helps anyone, I was hitting a similar problem, it was caused by the Gradle plugin for Google services bringing in a dependency which conflicted with Firebase.

In my top level build.gradle I had, in buildscript:

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

Which was bringing in (automatically) dependencies which were conflicting with, in my app's build.gradle:

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

Little bit confusing as I only had one compile dependency and was scratching my head what could be conflicing.

I removed the google-services gradle plugin and it solved the issue. I suppose I could also just find the right version :)

like image 1
zedix Avatar answered Oct 22 '22 10:10

zedix