14

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?

abdalrahman alenzi
  • 159
  • 1
  • 1
  • 6

4 Answers4

20

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

AntEdote
  • 318
  • 3
  • 8
13

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

Rishal
  • 194
  • 5
10

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   
Smundo
  • 521
  • 5
  • 8
  • for a detailed explanation, see the Flutter offical docs at: https://docs.flutter.dev/deployment/android#enabling-multidex-support – Yonatan May 29 '23 at 09:34
0

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

Raj
  • 3,890
  • 7
  • 52
  • 80