-
Notifications
You must be signed in to change notification settings - Fork 1
Unity Android Enable Multidex
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”.
To enable Multidex you have to:
- Enable Gradle build system.
- Change Gradle settings file.
- Initialize Multidex.
Starting with Unity 2017.3 it is enabled by default. 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)
- 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 yourAssets/Plugins/Android/mainTemplate.gradle
. Now open this file in any text editor. - Add
multiDexEnabled true
as a new line inside thedefaultConfig
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
anduseProguard
lines from Gradle
// 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**
}
}
}
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 Manifest.
If you already had AndroidManifest.xml in Assets/Plugins/Android/ and it already had ‘android:name’ parameter in your 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>