Monday, August 12, 2019

Resolving Android Studio "Cannot fit requested classes in a single dex file" error

After I added an additional library to an Android app, this error "null, Cannot fit requested classes in a single dex file (# methods: 66445 > 65536)" occurred while trying to build the application in Android Studio, as shown in the screenshot below.
 
The solution to this is to enable multi dex support in the Android app. The following steps show how to do this:
  1. In Android Studio, add in a new dependency to the androidx.multidex:multidex library to the app's build.gradle file.

    dependencies {
    ...etc...
    implementation 'androidx.multidex:multidex:2.0.1
    ...etc...
    }

  2. Then in the same app build.gradle file, toggle true the multiDexEnabled property.

    android {
      defaultConfig {
        ...etc...
        multiDexEnabled true
        ...etc...
      }
    }

Now, compiling the Android application should no longer result in the dex error.

3 comments:

  1. Can someone tell me why is this error caused?

    ReplyDelete
  2. Oil dey your head...
    It worked...
    Thank you so much....my God bless you...Amen

    ReplyDelete