Home » Android » android – Flutter google_sign_in error : Unable to merge dex

android – Flutter google_sign_in error : Unable to merge dex

Posted by: admin June 15, 2020 Leave a comment

Questions:

Dear Flutter community,

I am banging my head on a seemingly simple task.
I want to add firebase authentication to my app. It worked on iOS but as I tried to implement it for android, I systematically get the error :

Launching lib/main.dart on Android SDK built for x86 in debug mode…
Initializing gradle… Resolving dependencies… Running ‘gradlew
assembleDebug’… Configuration ‘compile’ in project ‘:app’ is
deprecated. Use ‘implementation’ instead. registerResGeneratingTask is
deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use
registerGeneratedFolders(FileCollection) registerResGeneratingTask is
deprecated, use registerGeneratedFolders(FileCollection) Configuration
‘compile’ in project ‘:google_sign_in’ is deprecated. Use
‘implementation’ instead.

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:app:transformDexArchiveWithExternalLibsDexMergerForDebug’.

    java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge
    dex

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

  • Get more help at https://help.gradle.org

BUILD FAILED in 34s Finished with error: Gradle build failed: 1

I reproduced the error using 'flutter create' then adding Firebase capabilities following the codelab
https://codelabs.developers.google.com/codelabs/flutter-firebase/#4

Here is the only modification to pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  google_sign_in: 0.3.1 # ONLY MODIFICATION
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.0

general build.gradle:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.1.0' #ONLY MODIF
    }

}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

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

App build.gradle :

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withInputStream { stream ->
        localProperties.load(stream)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.3'

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.mycompany.test"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

}

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

here is the flutter doctor output :

[✓] Flutter (on Mac OS X 10.13.2 17C88, locale fr-BE, channel master)
• Flutter at /Users/sergebesnard/flutter
• Framework revision 4d2c2aaaa1 (6 days ago), 2017-12-27 07:30:31 -0800
• Engine revision 7c126001d8
• Tools Dart version 1.25.0-dev.11.0
• Engine Dart version 2.0.0-edge.9e8a3e2d31621c1bdf6139d068e7898a2ac2ab5a

[✓] Android toolchain – develop for Android devices (Android SDK
27.0.2)
• Android SDK at /Users/sergebesnard/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-27, build-tools 27.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)

[✓] iOS toolchain – develop for iOS devices (Xcode 9.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.2, Build version 9C40b
• ios-deploy 1.9.2
• CocoaPods version 1.3.1

[✓] Android Studio (version 3.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b08)

[✓] IntelliJ IDEA Community Edition (version 2017.2.5)
• Flutter plugin version 18.0
• Dart plugin version 172.4155.35

[✓] Connected devices
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 8.0.0 (API 26) (emulator)

I am obviously new to Android development, and would prefer not to have to become expert to get the tutorial working. Every solution I found required tinkering with the .gradle files and only apply to react-native.

Thank you for your help !

How to&Answers:

This error means that some of your dependencies use a different version of the google play services.

You’d have to downgrade/upgrade plugins to unify the version used

Answer:

I also had the same issue and I read that you had to include multiDexEnabled true in the app\build.gradle defaultConfig section. This worked for me.

Answer:

Solved : incompatible versions used between plugin and framework.
Google SignIn plugin 0.4.0 and above should be used with new projects created with flutter create in its latest version.

The codelab is no longer valid and should be updated.

Answer:

Change minSdkVersion in android/app/build.gradle from 16 to 21

Final Versions

  • pubspec.yaml
    • cloud_firestore: ^0.8.2+3
  • android/build.gradle
    • classpath 'com.google.gms:google-services:3.2.1' // Google Services plugin
  • android/app/build.gradle
    • targetSdkVersion 27
    • minSdkVersion 21

Answer:

This link solved the same issue for me.

First I set dependencies in my pubspec.yaml to

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2 

and ran flutter packages get in my IDE’s terminal.

Also I had to change the minimum target SDK version:

  1. Open android/app/build.gradle, then find the line that says
    minSdkVersion 16.
  2. Change that line to minSdkVersion 21.
  3. Save the file.

This alone may fix your problem; however I had to also do the following because some of my dependency versions were mismatched.

I had to open android/app/build.gradle, then add the following line as the last line in the file:
apply plugin: 'com.google.gms.google-services'

Next, I had to open android/build.gradle, then inside the buildscript tag, add a new dependency:

buildscript {
   repositories {
       // ...
   }

   dependencies {
       // ...
       classpath 'com.google.gms:google-services:3.2.1'   // new
   }
}

After this my app finally ran on the android emulator.

The link has a more complete walkthrough if you get stuck.

Also, to note, I did not have to set multiDexEnabled to true.

Answer:

The problem here may be that the plugins you are depending on have conflicting gradle dependencies.

This SO answer here https://stackoverflow.com/a/49901209/90918 provides a description of how to solve this problem by finding the conflicting dependencies and overriding them.

Quoting the original answer:

Your two packages seem to disagree on their transitive dependencies.
One wants 11.6.+, the other wants 11.+ of some play-services
dependencies. Since both 11.6.2 and 11.8.0 are out there, this is
going to end up with a conflict.

If you run ./gradlew androidDependencies in your android/ folder,
you get a listing of the result of dependency resolution, containing,
among others, the following:

+--- :flutter_google_place_picker (variant: release)
+--- com.google.android.gms:play-services-location:[email protected]
+--- com.google.android.gms:play-services-places:[email protected]
+--- com.google.android.gms:play-services-maps:[email protected]
+--- com.google.android.gms:play-services-base:[email protected]
+--- com.google.android.gms:play-services-tasks:[email protected]
+--- com.google.android.gms:play-services-basement:[email protected]

These 11.6.2 and 11.8.0 packages are not going to work together. To
resolve this, you need to patch your dependencies to be consistent
with each other, or add a dependency override to the top level of your
android/app/build.gradle file and hope for the best:

configurations.all {
    resolutionStrategy {
        force 'com.google.android.gms:play-services-places:11.8.0'
        force 'com.google.android.gms:play-services-location:11.8.0'
    }
}

Answer:

The reason is you are using the different version of dependencies. To fix this go to your project/App/src/ and open Build.gradle. Then Add multiDexEnabled true Line inside the default config section.

Ex:Like this
defaultConfig {
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
multiDexEnabled true
}

Answer:

I had added admob_flutter: ^0.3.4 in my flutter app but didn’t used anywhere. Removing from pubspec.yaml file works for me.