Ionic run android fails with: com.android.dex.DexIndexOverflowException

Up until about 2-3 days ago, I could run ionic run android and test my app on my device. Now when I try and run or build, I get this error:

:transformClassesWithDexForRelease
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:484)
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:261)
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:473)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:161)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ‘:transformClassesWithDexForRelease’.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.8.0_73\bin\java.exe’’ finished with non-zero exit value 2

Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
FAILED

BUILD FAILED

Total time: 33.291 secs

So from extensive Googling, it seems that the underlying Java code has more than 64,000 or so methods and that the issue seems to be the Google Play Services, which is a massive library. They suggest using only what you need (e.g. gcm, firebase etc.) but that’s not the sort of thing we (easily) have control over in a Cordova / Ionic project.

Has anyone else come across this, and how did you fix it?

I asked on StackOverflow, but got no answers there, so I’m coming here.

So I finally found the answer. There’s a Cordova plugin that adds in multidex support, bypassing the 64k method limit.

Simply install it via cordova plugin add https://github.com/jwall149/cordova-multidex --save and next time you build, the issue should be resolved.