react-native-google-sign-in icon indicating copy to clipboard operation
react-native-google-sign-in copied to clipboard

Build Failed: com.android.dex.DexException

Open BerndWessels opened this issue 7 years ago • 12 comments

I followed exactly all setup instructions on a brand new macOS with brand new react-native 0.45.1 and checked that all files are as described in the README. But running react-native run-android build fails with the following error:

Running dex in-process requires build tools 23.0.2.
For faster builds update this project to use the latest build tools.
Dex: Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
        at com.android.dx.command.dexer.Main.run(Main.java:277)
        at com.android.dx.command.dexer.Main.main(Main.java:245)
        at com.android.dx.command.Main.main(Main.java:106)
    
:app:transformClassesWithDexForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Is there a way to fix this?

BerndWessels avatar Jun 17 '17 09:06 BerndWessels

I get his also when just running the example app. A fix would be greatly appreciated.

grigored avatar Jun 17 '17 13:06 grigored

I followed this and I was able to run the example! https://stackoverflow.com/questions/38191170/multiple-dex-files-define-lcom-google-firebase-firebaseexception

grigored avatar Jun 17 '17 14:06 grigored

@grigored Hi, I can't fix it. Could you please provide your app/build.gradle file?

valerybodak avatar Jul 28 '17 08:07 valerybodak

This is the relevant part of what I have in app/build.graddle

android {
    compileSdkVersion 25
    buildToolsVersion '26'

    defaultConfig {
        applicationId "com.appId"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
///more stuff
}
dependencies {
    compile(project(":react-native-google-sign-in")) { // ADD this
        exclude group: "com.google.android.gms"
    }
    compile "com.google.android.gms:play-services-auth:10.0.1"
    compile project(':react-native-fbsdk')
    compile(project(':react-native-maps')){
        exclude group: 'com.google.android.gms', module: 'play-services-base'
        exclude group: 'com.google.android.gms', module: 'play-services-maps'
    }
    compile 'com.google.android.gms:play-services-base:10.0.1'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

apply plugin: "com.google.gms.google-services" // Add this after dependencies.

grigored avatar Jul 28 '17 11:07 grigored

And this is my top build.graddle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.google.gms:google-services:3.0.0' // Add this

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

grigored avatar Jul 28 '17 11:07 grigored

@grigored I'm still have the exception after run 'react-native run-android' :

Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/iid/zzb;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
        at com.android.dx.command.dexer.Main.run(Main.java:277)
        at com.android.dx.command.dexer.Main.main(Main.java:245)
        at com.android.dx.command.Main.main(Main.java:106)

It is my dependencies from app/build.gradle file:

dependencies {
    compile(project(":react-native-google-sign-in")) {
        exclude group: "com.google.android.gms"
    }
    compile project(':react-native-fbsdk')
    compile project(':react-native-hockeyapp')
    compile project(':react-native-sensors')
    compile project(':react-native-ble-manager')
    compile project(':react-native-localization')
    compile project(':react-native-audio')
    compile project(':react-native-beacons-manager')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.google.android.gms:play-services-auth:10.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile project(':react-native-device-info')
    compile project(':react-native-google-analytics-bridge')
}
apply plugin: "com.google.gms.google-services"

And it is my android/build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'net.hockeyapp.android:HockeySDK:4.1.2'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

valerybodak avatar Jul 29 '17 12:07 valerybodak

i'm no Android expert, so can't help you on this. But maybe you can try to start simple, run the example first, make sure that works, and then add the additional dependencies one by one and see which one causes the problem

grigored avatar Jul 29 '17 19:07 grigored

Finally, I've fixed it using 11.0.2 version for play-services-auth:

compile(project(":react-native-google-sign-in")) {
    exclude group: "com.google.android.gms"
}
compile "com.google.android.gms:play-services-auth:11.0.2"

valerybodak avatar Aug 11 '17 11:08 valerybodak

@grigored @valerybodak @BerndWessels Sorry for the delay, I am fully occupied at the moment so that I don't really have time to put on this project, but I did research a bit. This issue really seems to depend on your own project's module dependencies whether you have other libraries that also uses com.google.android.gms package group, which is very likely in most cases as it is very commonly used library. It only becomes a problem if there are conflicts on the versions for the same library. It happened to me too and I had to manually check project dpendencies and explicitly force versions on commonly used libraries. I used the following commands to see my android module dependencies (run them on your app/android folder):

./gradlew app:dependencyInsight --configuration compile

./gradlew -q app:dependencies

I currently don't have a better solution to this. To my knowledge there is no one for all solution to this issue as it will differ for each project settings. I wish Android package manager is smart enough to handle these conflicts automatically, but unfortunately it's not. You can search StackOverflow and there are tons of questions regarding the same issue.

If someone who's an Android expect has a better solution that is more automatic, then please share it with us!

joonhocho avatar Sep 08 '17 13:09 joonhocho

core problem is when you run react-native link, its linking twice, so you should remove entry in settings.gradle and build.gradle and mainApplication.java(imports/ initializing) better way to fix it is avoiding linking google-signin package twice.

karna41317 avatar Nov 09 '17 09:11 karna41317

@valerybodak 's solution also worked for me. I'm not actually even using this library but reviewing the dependencies as @joonhocho suggested, then excluding them with exclude group: "com.google.android.gms" got me past this issue.

jwrubel avatar Dec 20 '17 01:12 jwrubel

I implemented @joonhocho 's solution and worked for me.

  1. cd android/
  2. ./gradlew app:dependencyInsight --configuration compile
  3. ./gradlew -q app:dependencies > /some/path/file.txt
  4. Open the file with a text editor
  5. Looking for com.google.android.gms, view the version number used and put the same number in every place where appear com.google.android.gms:service on android/app/build.gradle For example: compile "com.google.android.gms:play-services-auth:NUMBER OF VERSION"

tlofano avatar Apr 10 '18 15:04 tlofano