App requires Multidex support in flutter?

37.9k Views Asked by At

I'm getting this error while building my app in Flutter. it seems the problem that the multidex is not enabled even though I did everything needed to enable it. this the error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeProjectDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
     Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
     Type androidx.activity.R$attr is defined multiple times: C:\Users\Abdul rehman\OneDrive\Desktop\CSC451\Capstone Project\harfanah\build\app\intermediates\project_dex_archive\debug\out\4b5c628c7fbb96e2839e63f71f8803802c039de25c33dd9c9475187dc45e6e1a_1.jar:classes.dex, C:\Users\Abdul rehman\OneDrive\Desktop\CSC451\Capstone Project\harfanah\build\app\intermediates\project_dex_archive\debug\out\9bd7ed05083b8d3edbf142363966d832d9f0694faefda4d175e3ad8ad18fc06d_1.jar:classes.dex

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 30s
[!] App requires Multidex support
    Flutter multidex handling is disabled. If you wish to let the tool configure multidex, use the --mutidex flag.
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

I have already checked all of the solutions. enabled Multidex support and used the latest version:

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.harfanahApplication.harfanah"
        minSdkVersion 21
        targetSdkVersion 32
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }
.
.
.

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation("androidx.multidex:multidex:2.0.1")
}

I also added these to gradle.properies

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true

Still it is not working. any suggestions?

6

There are 6 best solutions below

1
AntEdote On BEST ANSWER

Have you tried "flutter clean" and then rebuild? You can let the Flutter CLI tool do it for you with "flutter build --multidex".

1
Rishal On

use flutter clean and flutter pub get

then you will get a notification on whether you need to install it or not. type y (yes) to it .then use flutter run

1
Smundo On

I had the same issue on a Mac M1(2021). To debug on an android device, I extended @AntEdode's solution a bit:

flutter build apk --multidex --debug

During build, the same error will pop up, however the SDK will now ask whether or not you want to install multidex:

[!] 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]: 

After confirmation - voila:

✓  Built build/app/outputs/flutter-apk/app-debug.apk.

To run on your emulator:

flutter run --multidex   
0
Raj On

I got a similar error while debugging on the device directly. Since it was showing an error in VS Code debug console, it didn't show the option to enable multidex support.

cannot prompt without a terminal ui

But if you run flutter run in terminal/git-bash etc, it will prompt you with an option to enable it. Once enabled, the app is built and installed successfully.

enter image description here

1
Micheal Moris On

Multidex is often required in Android projects when you have a large number of methods and exceed the 64k method limit imposed by the Dalvik Executable (DEX) format. In a Flutter project, you might encounter this issue when using numerous plugins or adding many dependencies. Here's how to add Multidex to your Flutter project:

  1. Open your Flutter project in VS code or your preferred IDE.
  2. Navigate to the android -> app -> build.gradle.
  3. Inside the android/app/build.gradle file, add the following lines to the android block:
android {
    ...
    defaultConfig {
        ...
        multiDexEnabled true
    }
    ...
}
  1. Restart your app.
0
Maaz On

Run flutter run --debug in your android studio's terminal, and select an Android device.

You will be prompted to enable multidex support, press y.

Flutter tool will enable multidex support and retry the build, and you are good to go!