Multidex issue in Unity

Narendra Pal
Jul 20, 2017 · 4 min read

Whenever any game is developed using Unity, then there is a provision to build those applications for multiple platform such as Android, iOS, Windows, x-box, Blackberry etc.

I often found many unity developers with lack of experience in Android, but due to awesome features provided by unity, they export their project for multiple platforms.

If the project is exported into Android. There are chances for getting multidex issue.

About Multidex

So now questions arises, why multidex issue occurs in case of unity development? Is unity builds .dex files as Native Android coding?

No, The unity builds apk which contains .so files which is not get counted for multidex. The multidex issue in unity arises due to additional jar, .aar (google-play-services, firebase, etc) files used in Unity project. In solution to this, we know that unity has the provision to export the project for gradle build system. But its quite difficult to achieve this. So, let me try to handle the same thing with some simple steps, which will reduce unity developers pain in case of this multidex issue :D

Lets try with an example.

  • Create a new unity project now. “Test Multidex App”
  • Create a Sample scene file which contains two buttons and a sample script file. And add too many .jar and .aar files on Assets/Plugins/Android file path to get multidex issue.
  • And finally we got multidex error!
  • Actually at this point we cannot be sure that its multidex error. So to get the exact error detail, go to Console tab, click on right corner dropdown and select Open Editor Log.

This line in editor.log says that method count is now 69K+ which is more that 65536. This is what we call as Multidex issue.

Let’s try to resolve this.

  1. Export the project to Gradle

After click on this option, your project will get exported for Android project. Your project will now look like this

2. You have to install Android Studio in your system to open the exported project. Open Android Studio and select Import project (Eclipse, ADT, Gradle, etc)

Once opened you may face Gradle Sync Issue.

If you are not getting this then its good. But if someone with some bad luck find this issue like me! :D then here is the solution for this. Navigate to build.gradle file as shown in below screenshot and replace the classpath ‘com.android.tools.build.gradle.2.1.0’ with classpath ‘com.android.tools.build.gradle.2.2.3’ and click Try Again or Sync Project.

build.gradle

Voila!! Issue has been resolved. But wait, whats this, I am still not able to run my project in Android Studio. “The number of method references in a .dex file cannot exceeds 64K” The same error of multidex here with Android Studio as well. But whats written below the error line “Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

Lets navigate to the suggested site. Check Configure your app for multidex section. To know your apps minimum sdk version, open the build.gradle file inside AndroidManifest.xml. Check for android:minSdkVersion inside <uses-sdk></uses-sdk> In my case its 16.

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="26"
/>

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

android {
defaultConfig {
...
minSdkVersion 16
targetSdkVersion 26
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}

Also add this line in your AndroidManifest.xml inside <application> tag.

<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>

If you run your project now, you may face OutOfMemoryError. There are many solutions for this issue but the simplest one is writing dexOptions attribute in build.gradle file of your module.

android {
..
..

defaultConfig {
....
}
dexOptions {
javaMaxHeapSize "4g"
}

}

Finally you will be able to run our Unity game in Android Studio.

Wait! What about signing the apk for uploading on play store?

— You can generate signed apk by selecting Build → Generate Signed APKand release your apk on play store.

I have also written about Adding support library for Android in Unity. Check it out!

)

Narendra Pal

Written by

Senior Engineer(Mobile) at Zapr Media Labs. https://www.linkedin.com/in/narendra-pal-a761334a/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade