Multidex issue with Flutter

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 'com.android.support:multidex:1.0.3'
}
  1. Enable Jetifier.

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

android.useAndroidX=true
android.enableJetifier=true

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'
    }
}

Update for beginner devs in 2022 and beyond:

If you’e OK with your project having a minimum Android API level of 21 (it will run on 98% 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.