The shrinker may have failed to optimize the Java bytecode
Asked Answered
K

19

27

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)
Kreegar answered 18/3, 2020 at 18:10 Comment(1)
Please provide more details, what are you trying to perform? Are you following a tutorial or doc? Did it work before? Were changes done to your code before these Error messages appeared?Voluntaryism
T
34

You just have to change the minsdkversion to 24 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. ;)

Taam answered 16/7, 2020 at 10:1 Comment(0)
U
86

In the build.gradle for the app:

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.

Ultimate answered 27/3, 2020 at 7:33 Comment(0)
T
34

You just have to change the minsdkversion to 24 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. ;)

Taam answered 16/7, 2020 at 10:1 Comment(0)
N
24

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

Nuzzle answered 19/3, 2020 at 1:1 Comment(1)
this one worked for me...ty for sharing friend! I had the minifyEnabled in false and the shrinkResources in false as well...but after changing them to true, the app compiled....This solution was very important because updating the minSDK would have affected millions of my users...thanks again!Gruff
S
9

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

Stauffer answered 12/4, 2020 at 3:14 Comment(0)
A
6

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

}
Aristippus answered 4/6, 2021 at 10:44 Comment(0)
W
5

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
    }
Wrasse answered 11/1, 2021 at 5:56 Comment(0)
K
4

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.

Keneth answered 12/11, 2021 at 14:33 Comment(0)
E
3

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.

Euh answered 21/12, 2020 at 18:29 Comment(0)
F
3

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
    }
}
Fruitage answered 3/6, 2021 at 9:52 Comment(0)
J
2

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
}
Jamisonjammal answered 16/12, 2020 at 23:14 Comment(0)
B
2

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.

Blowsy answered 8/5, 2022 at 19:13 Comment(1)
i had same problem, Worked for me. just deleted kotlin folder.Tranquillity
M
1

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

minSdkVersion 21

And it will work fine.

Monomial answered 8/9, 2020 at 9:3 Comment(0)
C
1

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.
Customhouse answered 14/11, 2020 at 13:8 Comment(0)
C
1

Here's how i fixed mine:

android {
    defaultConfig {
        multiDexEnabled true
    }
}
Chromophore answered 19/11, 2020 at 14:52 Comment(0)
C
1

So, I faced this problem because i created a MainActivity.java file manually as my flutter project did not create it.

I tried all the things in documentation like

         minifyEnabled true
         shrinkResources true
         minSdkVersion 21

but they didn't work.

I analyzed the code and deleted MainAcivity.kt Kotlin file. Now it works.

Communalism answered 18/1, 2024 at 18:2 Comment(2)
Thank you bro, i had the same situation. After deleting the MainActivity.kt it's works for me.Grant
Yeah this worked for me too. The problem is that there where TWO MainActivity files due to android studio stupidly creating both and trying to mindmeld themVaisya
W
0

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"
    }
Waspish answered 5/5, 2020 at 17:23 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Richy
T
0

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'
}
Twilley answered 16/12, 2020 at 3:40 Comment(0)
C
0

I had the same issue, I just changed my minSdkVersion from 24 to 26: minSdkVersion 26

Cherry answered 5/3, 2024 at 15:59 Comment(0)
S
0

What helped me in my older project when upgrading to target compileSdkVersion 34, was upgrade also the gradle version in android/build.gradle from 7.2.0 to 7.2.2 like this: enter image description here

This might work only for specific project configurations but worth trying

Sestos answered 10/7, 2024 at 16:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.