'Flutter :The shrinker may have failed to optimize the Java bytecode

I'm trying to integrate cloud firestore to and android app but all I get is this error every single time

Launching lib/main.dart on Android SDK built for x86 in debug mode... Note: /home/tr/DevTools/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.13.4+2/android/src/main/java/io/flutter/plugins/firebase/cloudfirestore/CloudFirestorePlugin.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. D8: Cannot fit requested classes in a single dex file (# methods: 76095 > 65536) com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: The number of method references in a .dex file cannot exceed 64K

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:mergeDexDebug'.

    A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

  • 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 6m 10s [!] The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the --no-shrink flag to this command. To learn more, see: https://developer.android.com/studio/build/shrink-code Exception: Gradle task assembleDebug failed with exit code 1 Exited (sigterm)



Solution 1:[1]

You just have to change the minsdkversion to 21 instead of 16.

In android\app\build.gradle

 defaultConfig {
        applicationId "com.company.example"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }

It worked or me. ;)

Solution 2:[2]

In the app's build.gradle

 defaultConfig {
        applicationId "com.company.test"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }

Change the minSdkversion from 16 to 21, this worked in my case

Solution 3:[3]

I have experienced similar problem while coding with flutter but BUILD FAILED in 9s Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done 11.0s [!] The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the
--no-shrinkflag to this command. To learn more, see: https://developer.android.com/studio/build/shrink-code Gradle task assembleDebug failed with exit code 1

But i have managed to run my app and this is how i did it.

1.I located android/app/build.gradle file 2. Then access below code in the gradle file

    buildTypes {
    release {

        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

}

and changed it to

buildTypes {
    debug {
        minifyEnabled true

        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

}

The app was able to run in Android emulator

Solution 4:[4]

There are two different answers to this issue but I think the most appropriate is the one given by @Sarang Pal This is the official explanation by Google:

Troubleshooting Android build fail:

If you're planning to develop using an Android device or emulator, you'll need to handle multidex support -- otherwise, your build will fail with "Cannot fit requested classes in a single dex file."

By default, Flutter supports Android SDK v16 (Jelly Bean, released 2012), but multidex doesn't really work with Jelly Bean (though, it's possible). Configuring Jelly Bean to work is beyond the scope of this codelab, so we'll change the minimum target SDK version from v16 to v21 (Lollipop, released 2014).

To change the minimum target SDK version:

  1. Open android/app/build.gradle, then find the line that says minSdkVersion 16.
  2. Change that line to minSdkVersion 21.
  3. Save the file.

https://codelabs.developers.google.com/codelabs/flutter-firebase/index.html#3

Solution 5:[5]

navigate to {your-app-name}\android\app\build.gradle

// change your minSdkVersion from 16 to 21

defaultConfig {
    applicationId "com.example.testapp"
    minSdkVersion 21 
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName

}

Solution 6:[6]

Simply change minSdkVersion to 21 then it will be working

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

Solution 7:[7]

Finally, I managed to solve this issue in the easiest way!

Important: Just go to app:level module android/app/build.gradle and place multiDexEnabled true

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

Android applications by default have SingleDex support which limits your application to have only 65536 methods(references). So multidexEnabled = true simply means that now you can write more than 65536 methods(references) in your application.

Try this, it may work and finally resolve this issue [!] The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the --no-shrink flag to this command.

Hope!!! it helps.

Solution 8:[8]

Go to android/app/build.gradle and then copy and paste this and change the application id.

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

Solution 9:[9]

I tried every step from above but without success.

Finally, I solved with these commands inside the terminal:

$: flutter clean

$: flutter pub get

Everything worked like a charm.

Solution 10:[10]

At android\app\build.gradle file: Just change minSdkVersion 16 to 21

minSdkVersion 21

And it will work fine.

Solution 11:[11]

Here is the process of how to fix this.

1: Change some codes in located android/app/build.gradle to this

buildTypes {
    debug {
        minifyEnabled true

        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

2: Change minSdkVersion 16 to 21

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

enter image description here

Solution 12:[12]

To change the minimum target SDK version:

  1. Open android/app/build.gradle, then find the line that says minSdkVersion 16.
  2. Change that line to minSdkVersion 21.
  3. Save the file.

Solution 13:[13]

Here's how i fixed mine:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

Solution 14:[14]

In my case, I have added these lines to app level gradle file in Android to disable minify i.e. shrinking of code

buildTypes {
    release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
        shrinkResources false
        minifyEnabled false
    }
}

Solution 15:[15]

enter image description here

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.placementapp"
        minSdkVersion 23 //replace 23
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

Solution 16:[16]

I'd also faced this error, while I was working with 'cloud firestore' for the first time, You should change your "Android Level build.gradle"

Android Level 'build.gradle'

Make Sure :

defaultConfig {
   "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}


dependencies {
    implementation 'com.android.support:multidex:1.0.3'
}

Solution 17:[17]

i had ran

flutter create .

in order to generate windows, and web folders but since my project was setup to use java as the native instead of Kotlin the the generator created a Kotlin Folder in /android/src/main which caused confusion i think i just deleted it and it worked for me.