156

I'm getting the following error compiling with gradle using Flutter in Android Studio:

Dex: Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;

[... stacktrace omitted for brevity ...]

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzcew;

The short version

This only occurs if I add enough dependencies, as might be expected. I've enabled multidex and added the multidex dependency in the Android project build.gradle file as per instructions (https://developer.android.com/studio/build/multidex.html), but wasn't sure what to do about step 2 in "Configure your app for multidex" for a Flutter App, or even whether the omission of that step was the problem.

Steps to recreate:

  1. Select File/New/New Flutter Project from the toolbar
  2. Select "Flutter Application"
  3. Include Kotlin & Swift support
  4. Check the app compiles and runs
  5. Add the following to dependencies in pubspec.yaml:

    dependencies:
      flutter_google_place_picker: "^0.0.1"
      location: "^1.2.0"
    
  6. Hit Packages Get in Android Studio or run flutter packages get in the project directory
  7. Modify android/app/build.gradle to add the following sections in the appropriate places:

    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    android {
        defaultConfig {
            multiDexEnabled true
        }
    }
    
  8. Select Run/Run from the toolbar

Other things I've tried

  1. Replacing the "compile" dependency in build.gradle with each of the following:

    compile 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:multidex:1.0.1'
    implementation 'com.android.support:multidex:1.0.3'
    
  2. Following the multidex steps for each of my dependencies; i.e. modifying their build.gradle files, enabling multidex and adding the multidex dependency.

  3. Modifying minSdkVersion to each of 21 and 27 in each of the build.gradle files for my project and its dependencies, and enabling multidex for them.
  4. Enabling minifying for my project.
  5. Replacing location: "^1.2.0" with geolocation: "^0.2.1"
  6. Not enabling multidex at all (i.e. skipping step 7 of recreating). This results in the following error:

    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
    

Flutter doctor output

$ flutter doctor -v
[√] Flutter (Channel beta, v0.2.8, on Microsoft Windows [Version 10.0.16299.371], locale en-GB)
    • Flutter version 0.2.8 at D:\flutter
    • Framework revision b397406561 (2 weeks ago), 2018-04-02 13:53:20 -0700
    • Engine revision c903c217a1
    • Dart version 2.0.0-dev.43.0.flutter-52afcba357

[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at C:\Users\Dave\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: D:\AndroidDev\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at D:\AndroidDev
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] Connected devices (1 available)
    • Android SDK built for x86 64 • emulator-5554 • android-x64 • Android 5.1.1 (API 22) (emulator)

• No issues found!
Zoe
  • 27,060
  • 21
  • 118
  • 148
SpoonerNZ
  • 1,705
  • 2
  • 11
  • 8

17 Answers17

261

If you don't have experience with developing android application this information can be helpful otherwise you won't find anything new.


In most cases, enough will do the first step

How to enable multidex for flutter project.

  1. Enable multidex.

Open [project_folder]/app/build.gradle and add following lines.

defaultConfig {
    ...

    multiDexEnabled true
}

and

dependencies {
    ...

    implementation 'androidx.multidex:multidex:2.0.1'
}
  1. Enable Jetifier.

Open [project_folder]/android/gradle.properties and add following lines.

android.useAndroidX=true
android.enableJetifier=true
kalucki23
  • 172
  • 4
  • 15
Yuri Misyac
  • 4,364
  • 2
  • 16
  • 32
  • 1
    Instead of extends SomeOtherApplication I added extends FlutterApplication – temirbek Apr 05 '19 at 06:10
  • I am implementing this in Java but I am also using the FacebookLogin plugin, and when I implement your answer it says "The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first." – hysabone.com Apr 19 '19 at 10:27
  • @BarbaricGamester did you add `FacebookSdk.sdkInitialize()` in `onCreate()` method of `CustomApplicationClass` ? – Yuri Misyac Apr 19 '19 at 14:34
  • @YuriMisyac i have an error ` error: '{' expected [ ] public class App extends FlutterApplication() { [ ] ^ [ ] 1 error ` – Ujang Karnadi May 15 '19 at 07:57
  • @UjangKarnadi show me please full app class with imports. – Yuri Misyac May 15 '19 at 08:00
  • 1
    @YuriMisyac I am using java changing `FlutterApplication() {` to `FlutterApplication {` fix my issue, please update your code. and it work thanks, – Ujang Karnadi May 15 '19 at 08:42
  • Hey @YuriMisyac I've followed your steps, I no longer get the error OP had. However, when I reach the message `Installing build\app\outputs\apk\app.apk...`, I see that the app launches in my phone and crashes instantly with no output in the console, this one also stays stuck at the `Installing build\app\outputs\apk\app.apk...` message – Amine Jul 31 '19 at 15:35
  • 4
    Thank you for your great answer! But with Flutter 1.7, I *only* needed steps 1 & 2 for the multi-dex to work. The app would actually crash if I applied steps 3 & 4! – Karsten Silz Aug 22 '19 at 07:37
  • In 1.17.0 I didn't have to do step two for Q. Both options were already enabled in `gradle.properties` – tyirvine May 20 '20 at 14:28
  • Thank you, the first step fixed my error! – federico__ Sep 07 '22 at 12:31
204

Update for beginner devs in 2023 and beyond:

If you’e OK with your project having a minimum Android API level of 21 (it will run on 99.3% of Android devices), all you have to do is this:

Using a text editor, open your "app level build.gradle file" which exists at [your project]\android\app\build.gradle.

There is a line that begins with "minSdkVersion."

Change it from:

defaultConfig {
        // ...
        minSdkVersion [whatever it says here, even if it is not a number]
        // ...
        }

to:

defaultConfig {
            // ...
            minSdkVersion 21
            // ...
            }

Obviously the 21 can be higher if you want, but not lower.

YOU ARE DONE.

You don't have to make ANY other modifications in order for multidex to work correctly.

Apparently, the default minSDK setting for new Flutter projects is STILL 16, so after adding enough dependencies in pubspec.yaml, many new developers will run into the multidex error and go searching online, potentially getting bogged down in old, confusing information which only applies to projects with a minimum level set to less than 21.

That was the simplest solution, but there are other ways:

Jahidul Islam's answer here, and Francis Nduba Numbi's answer here.

They have their own pros and cons so be sure to read the comments under their answers.

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
17

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:11.8.0@aar
+--- com.google.android.gms:play-services-places:11.6.2@aar
+--- com.google.android.gms:play-services-maps:11.6.2@aar
+--- com.google.android.gms:play-services-base:11.8.0@aar
+--- com.google.android.gms:play-services-tasks:11.8.0@aar
+--- com.google.android.gms:play-services-basement:11.8.0@aar

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'
    }
}
SpoonerNZ
  • 1,705
  • 2
  • 11
  • 8
Mikkel Ravn
  • 641
  • 1
  • 5
  • 7
  • Hi Mikkel. Thanks a lot for taking the time to answer. I've modified your answer slightly to better reflect what worked for me, if it's incorrect, please let me know. Is it fair to say that if I force resolution to a play services version that could meet requirements of all plugins, there *should* be no problem? – SpoonerNZ Apr 18 '18 at 13:57
  • Yes, 11.8.0 should be backwards compatible to 11.6.x. – Mikkel Ravn Apr 18 '18 at 13:59
  • Cool. Thanks for explaining and giving me the tools (./gradlew androidDependencies) to better investigate related issues in future. – SpoonerNZ Apr 18 '18 at 14:02
6

Starting from Flutter 2.10

The multidex support is enabled automatically. If your app supports Android SDK versions below 21, and it exceeds the 64K method limit, simply pass the --multidex flag to flutter build appbundle or flutter build apk and your app will support multidex.

iqfareez
  • 555
  • 7
  • 21
5

in your app folder inside android

defaultConfig {
    ...

    multiDexEnabled true
}

Also check out: Enable multidex for apps with over 64K methods

blackgreen
  • 34,072
  • 23
  • 111
  • 129
S.R Keshav
  • 1,965
  • 1
  • 11
  • 14
2

If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex library.

However, if your minSdkVersion is set to 20 or lower, then you must use the multidex library and make the following modifications to your app project:

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

android {
    defaultConfig {
        ...
        minSdk = 15 
        targetSdk = 28
        multiDexEnabled = true
    }
    ...
}

dependencies {
    implementation("androidx.multidex:multidex:2.0.1")
}

Depending on whether you override the Application class, perform one of the following: If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="androidx.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

public class MyApplication extends MultiDexApplication { ... }

Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and callMultiDex.install(this) to enable multidex:

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

for more info check out this blog

Sourav Rana
  • 141
  • 1
  • 2
1

Just change the line in app\build.gradle

defaultConfig{
multiDexEnabled true
}

Do not add any dependencies I first added multidex dependencies but program said cannot find it I then added maven to build.gradle but made no change

Actually you need to change false to true nothing else Android studio will do rest

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
1

In <projectName>\android\app\build.gradle Make sure to set minSdkVersion to 21

 defaultConfig {
        ...
        minSdkVersion 21 // or 20 may be fine
        ...
    }

There is no need to set multiDexEnabled to true as if the app only targets Android 21 or higher (minSdkVersion) then multidex is already enabled by default.

Mostafa Wael
  • 2,750
  • 1
  • 21
  • 23
1

Update build.gradle to minSdkVersion 21

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple DEX files from APK files.

Support older versions of Android

In case you want to support Multidex prior to Android 5.0 (API level 21), check out the above documentation.

enter image description here

genericUser
  • 4,417
  • 1
  • 28
  • 73
1

I solved this only by running flutter run --multidex then reply with y to auto enable it. If it isn't working try removing anything you added intended to solve the issue and try again.

0

In your app level build.gradle file

  1. Increase your minSdkVersion from 16 to 20.

  2. Enable multiDex.

    defaultConfig {
         ...
    
         minSdkVersion 20   //Copy this
         multiDexEnabled true //Copy this  
     }
    
David Buck
  • 3,752
  • 35
  • 31
  • 35
Akhil Shukla
  • 491
  • 5
  • 10
0

in build.gradle

defaultConfig 
{
minSdkVersion 21
multiDexEnabled true
}


dependencies {
implementation("androidx.multidex:multidex:2.0.1")
}

In AndroidManifest.xml

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

I just wanted to debug my app and the simplest solution that worked for me was this:

1- In android studio terminal type:

 flutter run --multidex 

Then you will see some Errors and at the end of errors, there is a suggestion like this:

BUILD FAILED in 40s
Running Gradle task 'assembleDebug'...                             41.8s
[!] App requires Multidex support
    Multidex support is required for your android app to build since the number of methods has exceeded 64k. You may pass the --no-multidex flag
    to skip Flutter's multidex support to use a manual solution.

    Flutter tool can add multidex support. The following file will be added by flutter:

        android/app/src/main/java/io/flutter/app/FlutterMultiDexApplication.java

Do you want to continue with adding multidex support for Android? [y|n]:

2 - here type y and press enter to allow adding multidex support

with this way I could debug my app on api 19. so it's not needed to increase minSdkVersion to 21.

Zahra
  • 2,231
  • 3
  • 21
  • 41
0

In your app level build.gradle file

change your minSdkVersion flutter.minSdk to 19, then enable multiDex


defaultConfig {
     ...

     minSdkVersion 19   //Copy this

     multiDexEnabled true //Copy this  
 }
Scientist
  • 1
  • 1
-1

I solved this error by changing the min SDK version. And how you can change this on Flutter 2.8 and plus version. Follow this link

How to change Flutter version on Flutter 2.8 and plus versions

Aizan Sagheer
  • 349
  • 3
  • 8
-1

If you have conflict with newer package of stripe, then you have to "comment out" in pubspec.yaml this line: stripe_payment: ^1.1.4

Adi
  • 1
  • 1
-2

If you are installing via USB be sure to click on install button when the device asks for permission, It was the solution in my case.

Vimal Swaroop J
  • 135
  • 1
  • 10