I’ve been struggling for multiple days now trying to enable Multidex in order to build my project with Appodeal (which breaks the 64k methods limit) with Unity 2019.3 (betas 7 to 10). I’ve been through many issues, that I’m going to try to quickly describe here, as I’m sure it could spare some users some tears!
What I’ve done
I’ve created a cutsom mainTemplate.gradle file, in which I’ve added ‘multiDexEnabled true’ in the default config, and ‘com.android.support:multidex:1.0.3’ as a dependency.
When doing that, I’ve hit the issue described in this post : “More than one file was found with OS independent path ‘androidsupportmultidexversion.txt’”. cc @justtime
By exporting the project and using the command gradle androidDependencies, I’ve been able to see that my compiled project contained both ‘com.android.support:multidex:1.0.3’ and ‘androidx.multidex:multidex:2.0.0’ dependencies. I honestly don’t know where the androidX version comes from… anyone has a guess?
To fix this issue, I’ve pinned the multidex dependency as ‘androidx.multidex:multidex:2.0.0’, allowing the jar resolver to merge multidex deps properly, and I added android:name=“androidx.multidex.MultiDexApplication” to the application tag in my custom AndroidManifest.xml.
At this point, not more weird gradle error… except that multidex is not enabled at all, as I encounter the following issue again: "D8: Cannot fit requested classes in a single dex file.". I’m back to the start… How is that possible? I can see that androidx.multidex is properly added as a dependency, I have properly set multiDexEnabled to true and edited my manifest file as described above.
Possible hint?
In this post@JuliusM talks about the recent changes to the build.gradle files made to get Unity working as a library. I’ve dig a bit into that, but I’ve basically no experience regarding Android development and build config. Should multiDexEnabled true be set in the launcher’s gradle.build file instead of the libreary’s gradle file? If yes, how should developers proceed without having to export their game to Android Studio?
I’ve honestly lost days on this, so any help would be greatly appreciated guys! Anyone from @PavelLU 's team maybe? Thanks a lot in advance.
Hi,
Officially Unity does not support multidex. Instead we suggest to use Proguard’s minify option.
I have not tested this, but my guess would be it has to be specified in both places. The only official way to modify launcher’s build.gradle file is by exporting the project and doing modifications afterwards. You could modify the template in the Unity install directory, but that is not supported and it will affect all of your projects built with that Unity version.
I’ve been using multidex in my previous Unity games without encountering any issue: it works well since Gradle is officially implemented.
I’m actually using Proguard minification, but Appodeal SDK is still above the 65k limit. They offer a version of their SDK that doesn’t require Multidex, but according to their tech team, this will soon be deprecated.
So I guess it’s of utmost importance for them to find a way to ensure compatibility with Unity 2019.3! The easy installation of their solution is a strong selling point, so having their users modify the Unity install of export the project might be an issue… I’ll send them the link to this discussion.
Nevertheless, it seems that many users require multidex. would it be possible to imagine Unity adding a new checkbox near the “Custom gradle file” option, to automatically patch both Gradle files in order to activate Multidex?
It’s not only Appodeal SDK issue. This can be achieved with different SDKs too. Just using Firebase and Mopub is enough to require multidex.
I hope Unity will soon fix this and bring back full multidex compatibility (having Unity Cloud Build in mind).
I have same problem. I need to use multidex. just add Facebook, Firebase, and some mediation SDK and you are our of limits. Please fix it. Local solution is not enough I can’t modify files in unity install directory on UCB.
Pretty much using any plugins alongside a few of the Firebase plugins quickly hits the dex limit. Just dropping support for devices running versions of the SDK lower than 21 isn’t really a good option either.
If Unity can’t fix multidex support directly then a good potential workaround could be for them to let us switch between gradle building as an library and as an application, as 2019.2 and lower build as applications which allow implementation of multidex.
I’m talking about apply plugin: 'com.android.application' in the gradle mainTemplate which is used in 2019.2 and lower vs apply plugin 'com.android.library' which is forced to be used in 2019.3 and higher.
If alternatively anyone knows how to possibly build multidex for an android library to support android APIs lower than 21 that would also be great and solve everything too ;).
Hi, I managed to get it work even on lower minApi version (mine is set to 19) with Unity 2019.3.
You just need to edit the “launcherTemplate.gradle” file that is located at your Unity install location inside “Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates”. Just add the multiDexEnabled true and implementation ‘com.android.support:multidex:1.0.1’ here and it should work. Also you probably need to open your text editor as admin otherwise the changes won’t be saved.
Sadly this doesn’t seem to work either, from my testing the builds seemed to BUILD fine but launching the app on any devices running android 5.0 or lower would just log a bunch of class not found errors in logcat and plugins would fail to initialise or work correctly. (And running the app on android 5.0 or later would work fine)
Let me know if your builds do actually run on devices lower than android 5 though
I don’t have any Android 4.4.x device, but I tried in in emulator (Nox with Android version 4.4.2) and the game launched without any problems. Do you use proguard? Maybe it’s stripping some classes needed for the android 4.4
The game may launch without any problems but are your plugins working correctly, e.g admob ads showing, firebase functions working correctly etc (depending what plugins you’re using) (and are you actually using any plugins? It’ll probably work fine multidexing a project without any plugins)
Are you seeing any class not found errors in logcat?
At the time of testing we had a fresh android project setup with the latest version of the full firebase suite and latest admob github plugin. I may need to come back and revisit this if you have it working, maybe the later versions of 2019.3 changed something? - However I’m currently in the middle of a project and don’t have time to mess with upgrading and fixing multidex right now.
Having an option in Editor should be the correct fix but until then, as JuliusM said you can add multidexEnabled to the exported gradle file using an IPostGenerateGradleAndroidProject script.
OneSignal SDK also had problems with this gradle template change in 2019.3 ( https://github.com/OneSignal/OneSignal-Unity-SDK/issues/270?fbclid=IwAR05rwPu5RfxsIWFzsFhKkzNrqwpDqGyMhjA8cdjc2oJlLKUDymKzZdulxo )
You can use their solution and add code to change the “launcher/build.gradle” and add “\nmultiDexEnabled true\n” in it.
MultiDex need to be enabled in launcher/build.gradle. But unity since 2019.3 has a bug that not allow us to override launcherTemplate.gradle. That bug was fixed in 2020.1.0b2 (yesterday) and not even backported into 2019.3.6 (today) yet
i think no one is testing new build of unity for android and iOS. Its disaster, for android i am using 2019.2.x and for iOS 2019.3.x. Cmon guys do like its was before one Engine to rule them all !
For anyone looking for solution.
You have to move your
implementation ‘androidx.multidex:multidex:2.0.1’
and
multiDexEnabled true
from mainTemplate.gradle to launcherTemplate.gradle and it should work.
You should activate these files first here in the Project Setting->Player->Publishing Settings