Multidex issue with Flutter
š± Solving the Multidex Issue with Flutter š
Are you encountering an error related to multidex while compiling your Flutter project in Android Studio? Don't worry, you're not alone! This blog post will address common issues and provide easy solutions to help you get past this hurdle.
š The Short Version:
The multidex issue is encountered when you add enough dependencies to your project. To enable multidex in a Flutter app, follow the steps below:
Open the
android/app/build.gradle
file in your project.Add the following dependency under the
dependencies
section:
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
Inside the
android
section, add the following configuration to yourdefaultConfig
block:
android {
defaultConfig {
multiDexEnabled true
}
}
By doing this, you are configuring your app for multidex, which allows your app to have more than 65,536 methods.
š§ Steps to Recreate the Issue:
To recreate the multidex issue, follow these steps:
Select
File/New/New Flutter Project
from the toolbar in Android Studio.Select "Flutter Application" and include Kotlin & Swift support.
Check if the app compiles and runs successfully.
Add the following dependencies in the
pubspec.yaml
file:
dependencies:
flutter_google_place_picker: "^0.0.1"
location: "^1.2.0"
Run
Packages Get
in Android Studio or executeflutter packages get
command in the project directory.Modify the
android/app/build.gradle
file as mentioned in the "Short Version" section.Finally, select
Run/Run
from the toolbar to see the error.
š¤ Other Things I've Tried:
If the above steps don't solve your multidex issue, you can try the following alternatives:
Replace the "compile" dependency in
build.gradle
with each of the following options:
compile 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.android.support:multidex:1.0.3'
Follow the multidex steps for each of your dependencies. Modify their respective
build.gradle
files, enable multidex, and add the multidex dependency.Modify the
minSdkVersion
to either 21 or 27 in thebuild.gradle
files of your project and its dependencies. Also, enable multidex for them.Enable minifying for your project.
Replace
location: "^1.2.0"
withgeolocation: "^0.2.1"
.Try not enabling multidex at all (skipping step 7 of the recreation process). This may result in a different 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
If none of these alternatives work for you, continue reading for more troubleshooting options.
š Flutter Doctor Output:
To help you further, here is a sample output of flutter doctor -v
command:
$ 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!
Make sure that your Flutter and Android SDK installations are up-to-date and properly configured.
š¢ Call-to-Action:
I hope this guide helped you understand and solve the multidex issue with Flutter. If you have any further questions or need additional assistance, feel free to leave a comment or get in touch. Let's fix this and keep Fluttering! š