[SOLVED] Cannot fit requested classes in a single dex file. Try supplying a main-dex list. # methods: XXXXX > 65536

05Jul, 2019

You usually would not encounter this error if you work on small projects.  However, this error tends to show up as your project grows.

What Causes this Error?

The error occurs when your project has more than 65536 method definitions.  In any Java project, the number of methods a developer is allowed to create is usually limited to 65536.  Now, you might be thinking:

There’s just no way I could have created that many methods already.

Well, technically, you are right.  You most likely did not create that many methods.  But you need to understand that when your Java project is getting built, it basically takes into account the methods defined in each and every library you use.  So basically, you would run into the same issue if you create a new project with only the main method and import a library that has over 65536 methods.

Solutions

  • Remove Unused libraries:  This is an obvious solution right?  There could be some libraries present in your project that are not in use.  You can remove them and the total number of methods in your project should reduce.
  • Identify and remove unnecessary methods:  Another obvious solution right?  Methods make your code neater but desperate times calls for desperate measures.  In order to identify unnecessary methods, I use the following rules:
    • Methods that are called only from 1 portion of the code
    • Methods whose body contains only 1 line
  • Enable Multidex support:  In summary, multidex support enables your app to create multiple .dex bytecode file for a single apk instead of the default single .dex file for an apk.  This solution is guaranteed to get rid of this error.  Follow the instructions below to enable Multidex support.
    • Add this to your Gradle dependencies

      If you are an AndroidX user, the use this instead:
    • Then add the following bolded snippet to the android{...} section of your build.gradle file
    • Add the bolded snippet to the <application> tag of your AndroidManifest.xml file

      And now, when you try to build your project, the error should be gone.

And that’s all for now.

If you have any questions or contributions, feel free to post them in the comment box below


Why not Relax and let us bring your App to life. Click here to begin

Leave a Reply

Your email address will not be published. Required fields are marked *