Too many method references when I export android build

When I export android build, I got some errors below


trouble writing output: Too many method references: 74004; max is 65536.
You may try using --multi-dex option.
References by package:

how do I use ‘–multi-dex option’ ?


how to set on files in Unity side?
I know dex force jumbo flags can help. but I cant find it in Unity.

how do I avoid this situation?

Yes. I use many kinds of sdks. but I needs those.

This excellent post describes why this error happens in the first place: [DEX] Sky’s the limit? No, 65K methods is | by Sebastiano Gottardo | Medium

Unity is “hard-wired” to run the dex tool with a certain set of arguments. You cannot modify that unfortunately.

What you could do is output a code project (“Google Android Project”) and then build it from the command line using the appropriate options.

Another solution would be to limit the number of method references your game has to avoid this issue in the first place. This can be done in a number of different ways:

  1. Remove unused native Android plugins from your project.
  2. Use Gradle + ProGuard to strip any unused / unreferenced code automatically.
  3. Use Gradle + Multidex to create multiple dex files, overcoming the reference count limit (although his is not recommended, which is why I listed it as the last option).

This usually happens when google google-play-services.jar included, and this is really large library. Couple of month ago google separated this lib on submodules and you can try to include only required. If this not solved the issue, then you have to export google project from unity and build apk on your own. If you have any errors that dont allow you to export project then simply remove some jars from plugin folder and export, after add them back in android studio through graddle. Now its possible to enable multidex support for android 4.0.+. However if you need to support android 2.3.+ then its very tricky. You will need to limit dex file size(file can be found inside apk) to about 5mb each to allow every low performance device to load it. Why? Because enabled multidex option cuts on 10mb dex files and this apk wont install on some 2.3 devices

Check this post over at StackOverflow to learn how to enable multidex in Unity.