Search code examples
flutterdart-pubflutter-dependencies

Flutter Build failed with an exception for Geolocation plugin


Whenever I'm trying to include flutter geolocation(https://github.com/loup-v/geolocation) plugin I'm getting following error.

Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)

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 3s
Finished with error: Gradle build failed: 1

My pubspec.yml dependancies list is

dependencies:
  flutter:
    sdk: flutter
  google_sign_in: "^3.0.2"
  firebase_auth: "^0.5.5"
  shared_preferences: "^0.4.1"
  cloud_firestore: "^0.7.2"
  firebase_storage: "^0.2.5"
  image_picker: "^0.4.1"
  progress_hud: "^0.2.2"
  cached_network_image: "^0.4.1"
  intl: "^0.15.6"
  path: "^1.5.1"
  geolocation: "^0.2.1"

My app>build.gradle code

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

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 27

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.bhramaan.bhramaan"
        minSdkVersion 16
        targetSdkVersion 27
        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'

What's going wrong?


Solution

  • There are three things I can suggest to help. The first is to do a full rebuild by first running flutter clean and then running again. Occasionally things get a little bit messed up between flutter & android, and that helps.

    The next thing I would do is check that you're getting the same version of all the Play services. For some reason gradlew sometimes messes that up. Run gradlew app:dependencies from the android (or ./gradlew if you're on mac/linux). If you're getting different versions, add them all as dependencies of the same version.

    For example, in my android/app/build.gradle, I had to set

    implementation 'com.google.android.gms:play-services-base:15.0.2'
    implementation 'com.google.android.gms:play-services-vision:15.0.2'
    

    even though Gradle should have sorted that out. In particular look out for com.google.android.gms:play-services-location as it's a dependency of the geolocation package. Same goes for the com.android.support libraries.

    If that still doesn't work, try running the gradle build directly, and in particular looking at the stack trace (gradlew app:build --stacktrace from the android folder) to see if it tells you anything. You could then post that, or take a look at the generic Android questions for a solution. It could be that you need to enabler multi-dex, although I don't think so.