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
123dependencies {implementation 'com.android.support:multidex:1.0.3'}
If you are an AndroidX user, the use this instead:
123<span class="pln">dependencies </span><span class="pun">{</span><span class="pln">implementation </span><span class="str">'androidx.multidex:multidex:2.0.1'</span><span class="pun">}</span> - Then add the following bolded snippet to the
android{...}
section of your build.gradle file
123456789<span class="pln">android </span><span class="pun">{</span><span class="pln">defaultConfig </span><span class="pun">{</span><span class="pln"></span><span class="pun">...</span><span class="pln">minSdkVersion </span><span class="lit">21</span><span class="pln">targetSdkVersion </span><span class="lit">28</span><span class="pln"></span><b><span class="pln">multiDexEnabled </span><span class="kwd">true</span></b><span class="pln"></span><span class="pun">}</span><span class="pln"></span><span class="pun">...</span><span class="pun">}</span> - Add the bolded snippet to the <application> tag of your AndroidManifest.xml file
12345678<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="tag"><manifest</span> <span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"https://schemas.android.com/apk/res/android"</span><span class="pln"></span><span class="atn">package</span><span class="pun">=</span><span class="atv">"com.example.myapp"</span><span class="tag">></span><span class="pln"></span><span class="tag"><application</span><span class="pln"></span><b><span class="atn">android:name</span><span class="pun">=</span><span class="atv">"android.support.multidex.MultiDexApplication"</span></b> <span class="tag">></span><span class="pln">...</span><span class="tag"></application></span><span class="tag"></manifest></span>
And now, when you try to build your project, the error should be gone.
- Add this to your Gradle dependencies
And that’s all for now.
If you have any questions or contributions, feel free to post them in the comment box below