There won’t be a single Android developer, today, who hasn’t dealt with an error message displaying:
After experiencing these error messages, most of us might think that when I (or my team members) haven’t coded these many methods in our application then how come one gets such an error message. Most of you might consider it as a bug of Eclipse IDE as all are, still, developing Android applications in Eclipse IDE.
So what exactly does these error messages imply?
These numbers, actually, imply the total number of references that the code can invoke within a single .dex file or Dalvik Executable file.
Yes, it means your executable file can’t contain more than 65536 methods in it. The number of methods specified here is not just the number of methods that you have coded in your application. This particular number includes the methods from the libraries that you have included in your application.
To get a much better picture of the number of methods that your application contains, let us consider the libraries that are mostly used for developing our applications.
That’s just Android libraries, alone, which takes over thirty thousand methods in your application. Over thirty thousand method occupies half the limit. Now that is scary.
Now consider other libraries used along with the above mentioned ones.
That should give you a clear picture of how many methods are there, approximately, in a tiny application that you coded.
If you’re curious about how I got these numbers, here is a code snippet for checking how many methods are there in a .jar file. (The total number is higher than what you might expect, but based on our experience, it’s reasonably close to what you actually have.)
Can we solve the issue?
Sure, you can solve the issues. And the best solution for solving this issue is to move your application development to Android studio from other IDEs. But first let’s see how to solve it within Eclipse IDE so that it can buy you some time for the present sprint you are working on.
While in Eclipse IDE, at present, there isn’t any actual way out to solve it. A workaround will be to extract your .jar files, used as libraries in your application, delete the unwanted packages and classes from the extracted code and repackage the wanted code as another .jar file. Now, import this .jar file into your application. Do note that this might not always work if you have many other libraries, also, included in your application.
Authentic way of getting past this limit requires you to configure build process of your application to generate more than one DEX file, known as a multidex configuration. For that, you will have to initially, migrate your application code to Android studio, from your existing IDE. In Android studio, you can solve the multidex issue using Gradle.
Multidex support before Android 5.0
Versions of the platform, earlier to Android 5.0, use the Dalvik runtime for executing application code. In normal scenario, Dalvik limits applications to a single classes.dex bytecode file per APK file. In order to get rid of this limitation, we can use the multidex support library, which becomes part of the primary DEX file of your application and then manages access to the additional DEX files and the code they contain.
The Gradle build script dependency identifier for this library is as follows:
Multidex support for Android 5.0 and higher
Android 5.0 and higher uses a runtime called ART which, natively, supports multiple dex file loading 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.
Configuring your application for Multidex with Gradle
Setting up your project to use a multidex configuration requires you to make a few modifications in your application development project. The following steps should be performed to achieve the same:
Change your Gradle build configuration to enable multidex
Modify your application Gradle build file configuration to include the support library and enable multidex support, as shown in the following Gradle build file snippet:
You can specify the multiDexEnabled setting in the defaultConfig, buildType, or productFlavor sections inside your Gradle build file.
Modify your manifest to reference the MultiDexApplication class
In your manifest, add the MultiDexApplication class from the multidex support library under the application tag.
If your application, already, extends an application class, then you can specify that particular class name under the application tag name. In that case, you will have to add MultiDex.install(this) inside your class to enable multidex support.
If you are working on MultiDex and you have any suggestions, please do write us a comment.
Android Developer (SSE), RapidValue