Android gives error “Cannot fit requested classes in a single dex file”

We Are Going To Discuss About Android gives error “Cannot fit requested classes in a single dex file”. So lets Start this Java Article.

Android gives error “Cannot fit requested classes in a single dex file”

  1. Android gives error “Cannot fit requested classes in a single dex file”

    Add dependency in build.gradle file:
    implementation 'androidx.multidex:multidex:2.0.1'
    In build.gradle's defaultConfig section add:
    multiDexEnabled true

  2. Android gives error “Cannot fit requested classes in a single dex file”

    Add dependency in build.gradle file:
    implementation 'androidx.multidex:multidex:2.0.1'
    In build.gradle's defaultConfig section add:
    multiDexEnabled true

Solution 1

Add dependency in build.gradle file:

implementation 'androidx.multidex:multidex:2.0.1'

In build.gradle‘s defaultConfig section add:

multiDexEnabled true

More details here: Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536

Original Author Gokul Nath KP Of This Content

Solution 2

Running RN 0.62 and this worked for me with successful build.

Update build.gradle
enter image description here

classpath('com.android.support:multidex:1.0.3')

Update defaultConfig

enter image description here

multiDexEnabled true

After those 2 changes, restart the gradle build again.

Original Author Gajen Sunthara Of This Content

Solution 3

Multidex is not always the solution to the problem, is true that it will generate more dex files to fit your method count, but be sure to not import more methods that you need because this in long term will make your builds slower than before.

For example, if you just need to use the location library from play services, you have two options

First one is implementing the whole play-services libraries that will come with location

implementation 'com.google.android.gms:play-services:11.8.0'

These whole libraries could have more than 40.000+ methods (is only an estimative, I don’t really know the total count), being close to reaching the 65536 limit methods.

Instead you should be targeting only the libraries you will use instead of the whole bundle of libraries

in this case

implementation 'com.google.android.gms:play-services-location:11.8.0'

could have just 50 – 100 methods to work with, which will be better at build time than loading a whole bunch of methods from the whole library package that you won’t ever use.

this is just a tip to avoid getting

Cannot fit requested classes in a single dex file.

For minSdkVersion above android 5.0 API 20 +

Android 5.0 and higher uses a runtime called ART which natively
supports loading multiple dex files from application APK files. ART
performs pre-compilation at application install time which scans for
classes(..N).dex files and compiles them into a single .oat file for
execution by the Android device. For more information on the Android
5.0 runtime, see Introducing ART.

If you are targeting lower devices (Android 4.1 API 16) or before Android 5 (API 20)

Versions of the platform prior to Android 5.0 use the Dalvik runtime
for executing app code. By default, Dalvik limits apps to a single
classes.dex bytecode file per APK. In order to get around this
limitation, you can use the multidex support library, which becomes
part of the primary DEX file of your app and then manages access to
the additional DEX files and the code they contain.

You will need to use multidex in this last case

Original Author Gastón Saillén Of This Content

Solution 4

Update 2021 Android-X and Android Studio 4.XX:

Adding multiDexEnabled in the default config of your app-level Build.Gradle is enough

multiDexEnabled true

See image for reference

enter image description here

Original Author Hitesh Sahu Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment