95

Trying to use Firestore in my project. My project is a brand new one, but having problems running the app on my device without getting an error: Execution failed for task ':app:mergeDexDebug'.

My app is using AndroidX. I've added my google-services.json file, followed the steps etc.

Yaml file:

dependencies:
  cloud_firestore: ^0.13.3

android/build.gradle:

com.google.gms:google-services:4.3.3

Full error:

FAILURE: Build failed with an exception.

What went wrong: Execution failed for task ':app:mergeDexDebug'. A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

11 Answers 11

270

The problem is with multidex builder. Actually, this often happens when you have imported a lot of packages in your yaml file which cannot fit into a single .dex built hence you have to enable multidex.

Go to android/app/build.gradle and add the following lines of code:

dependencies {
  implementation 'com.android.support:multidex:2.0.1' //enter the latest multidex version
}
android {
    defaultConfig {
        multiDexEnabled true
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Infact, adding multiDexEnabled true fixed the issue. Don't understand why though, as I think firestore is using AndroidX. Thank you
You can find the latest multiDex version here: developer.android.com/jetpack/androidx/versions#multidex
omg thank you!! All I added was multiDexEnabled true but not the dependency and that fixed my issue.
After so many attempts this worked for me thank you @lordvidex
not working anymore
|
26

Fixed the issue, but don't understand why. Why do I need to enable multiDex when I believe Firestore is using AndroidX?

Anyway, the fix. Add multiDexEnabled true to your app/build.gradle

defaultConfig {
    // TODO: Specify your own unique Application ID 
    (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.example.poll"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    //This line here...
    multiDexEnabled true
}

Comments

25

I just met with this error, and it turns out it can be fixed easily. Of course, other answers on this are really great and it tells you how to fix it. However, I was wondering why this even happened. It turns out this comes from the minSdkVersion used. If this is set to 20 or lower, you need to configure your app for multiDex support.

   defaultConfig {
        applicationId "com.inspire.aaym"
        minSdkVersion 21
        targetSdkVersion 30
    }

So, if you app is not intended to be used in Android version prior to 5.0, setting the minSdkVersion number to 21 will easily fix this issue. If not, follow the workaround to enable mutiDex support.

Read more from the official document: https://developer.android.com/studio/build/multidex#mdex-gradle

Comments

9

You can follow the guidelines mentioned here

https://developer.android.com/studio/build/multidex

or to cut the story short and directly go to answer

Find android/app/build.gradle file and add the following lines of codes:

dependencies {
  compile 'com.android.support:multidex:1.0.3' 
  //find latest version from here https://developer.android.com/studio/build/multidex
}

android {
    defaultConfig {
        multiDexEnabled true
    }
}

Comments

2

When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture: https://developer.android.com/studio/build/multidex

add multiDexEnabled true in app/build.gradle defaultConfig at last.

defaultConfig{ ... multiDexEnabled true }

Comments

2

If you're coming from React Native and using react-native-firebase, try :

buildscript {
ext {
    buildToolsVersion = "33.0.0"
    minSdkVersion = 24 }

minSdkVersion 21 wouldn't work, replace it with 24.

Remove dex enabled configurations if you added any.

Source : https://github.com/invertase/react-native-firebase/discussions/7489

Comments

1

If you are using AndroidX,

Add below lines to app/build.gradle

multiDexEnabled true // to the defaultConfig {}

implementation 'androidx.multidex:multidex:2.0.1' // to the dependencies

To the AndroidManifest.xml (Optional)

<application
    android:name="androidx.multidex.MultiDexApplication"

Please note that you have to extends MainActivity to the MultiDexApplication. If you change android:name.

Comments

1

at android/build.gradle test different versions of minSdkVersion. Mine worked with minSdkVersion = 22.

Comments

0

in app/build.gradle make sure your minSdkVersion should be greater or equal to 21

defaultConfig {

    minSdkVersion 21

    targetSdkVersion 30

    versionCode flutterVersionCode.toInteger()

    versionName flutterVersionName

}

Comments

0

This error happens when test Device was changed! Clear Build folder from project and rebuild.

//it's depends on your version
//npm run clean
ns clean

//rebuild
//tns platform remove android
tns platform add android
tns build android
tns run android

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

just by adding multiDexEnabled true under "android/app/build.gradle" i have fixed this issue. after adding this do flutter pub get for flutter project or run gradle build for android native project

example:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.