Multidex is a tool that is required for Android projects that exceed DEX limit – over 64K methods. You can read from the official Android documentation for developers what this dex limit is and how to enable Multidex in Android projects. The problem is that in Unity projects it is a bit tricky to implement setup required for Multidex.
There are two ways to overcome DEX limit.
- Reduce the total number of references called by your app code, including methods defined by your app code or included libraries.
- Enable Multidex – We strongly recommend enabling Mutidex, but if for some reason it is not an option for you, then you can contact our support and we will explain how you can minimize appmediation Android SDK to have fewer methods, but it will also affect your performance metrics.
It is also possible to enable multi dex files build even without Gradle build system, but it is more complicated so we are not going to describe how to do it here. If you are still interested then you can google “Multidex without Gradle”.
In Unity 5.4 and below Gradle is not supported, so you would first have to export project to AndroidStudio while also migrating to Gradle platform. After your project is exported you can just follow official Android documentation on how to enable Multidex. After you enabled Multidex you now will have to make debug/release builds using AndroidStudio (or Gradle directly).
In Unity 5.5 and above, Unity started supporting Gradle build system. So you don’t need to export your project, you can enable Multidex directly. To do that there are 3 steps that you would have to do:
- Enable Gradle build system.
- Change Gradle settings.
- Initialize Multidex.
Enable Gradle build system
To build your Android build with Gradle in Unity:
- In the Unity Editor, open the Build Settings window (menu: File > Build Settings…)
- In the Platform list, select Android
- Set the Build System drop-down to Gradle (new)
Change Gradle settings
- To change Gradle settings you first need to get default Gradle settings file specifically for your Unity version. In Unity 2017.2 an up all you need to do to get default Gradle file is to use the Custom Gradle Template checkbox under Player Settings. In other versions of Unity, you would have to copy mainTemplate.gradle file in the Unity installation folder (try searching for mainTemplate in Unity installation folder) to your Assets/Plugins/Android/mainTemplate.gradle. Now open this file in any text editor.
- Add multiDexEnabled true as a new line inside the defaultConfig object.
- If minimum Android API level is 20 or lower (Check your Android Player Settings to check Min. API Level). Add compile ‘com.android.support:multidex:1.0.1’ as new line inside dependencies object.
- In some cases, you might get an error that shrinking/minification is not supported with Multidex. In this case, you would have to remove all minifyEnabled and useProguard lines from Gradle
Example of mainTemplate.gradle. Don't copy it directly.
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' } } allprojects { repositories { flatDir { dirs 'libs' } } } apply plugin: 'com.android.application' dependencies { compile 'com.android.support:multidex:1.0.1' compile fileTree(dir: 'libs', include: ['*.jar']) **DEPS**} android { compileSdkVersion **APIVERSION** buildToolsVersion '**BUILDTOOLS**' defaultConfig { targetSdkVersion **TARGETSDKVERSION** applicationId '**APPLICATIONID**' multiDexEnabled true } lintOptions { abortOnError false } aaptOptions { noCompress '.unity3d', '.ress', '.resource', '.obb' } **SIGN** buildTypes { debug { //minifyEnabled **MINIFY_DEBUG** //useProguard **PROGUARD_DEBUG** proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD** jniDebuggable true } release { //minifyEnabled **MINIFY_RELEASE** //useProguard **PROGUARD_RELEASE** proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD** **SIGNCONFIG** } } }
Initialize Multidex
You need to do this only if your Android Player settings have minimum API level set to 20 or lower.
Update your manifest file located in Assets/Plugins/Android/AndroidManifest.xml. If you don’t have this manifest file you would have to create it. You can find default AndroidManifest.xml in Unity installation location. But you would also have to manually apply project settings of your app: package name, permissions, configuration options and other information. For more information about the Android Manifest file, refer to the Android Developer documentation on Android Manifests.
If you already had AndroidManifest.xml in Assets/Plugins/Android/ and it already had ‘android:name’ parameter in your <application> tag, then you would have to refer to Android documentation to find the solution on how to initialize Multidex in your project.
If you just created a new manifest file or your manifest file <application>
tag didn’t have any android:name
parameter you just add this parameter like this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:name="android.support.multidex.MultiDexApplication" > ... </application> </manifest>