Recently I’ve been struggling with multidexing in android and this question comes to my mind:
To enable multiDex in android project
If you only and only add this line:
// gradle build config
multiDexEnabled true
The app will compile and works fine.
But the real solution is to:
- Add
multiDexEnabled true
- Add the support library
androidx.multidex
- Override
Application
class.
Why adding only multiDexEnabled true
works and project runs successfully? What about the other two steps remained
Am I not considering something?
BTW, my minSdkVersion
is 15
2
Enable multidex for apps with over 64K methodsHere is the link by Google itself.
When your app and the libraries it references exceed 65,536 methods, you encounter a build error that indicates your app has reached the limit of the Android build architecture:
trouble writing output:
Too many field references: 131000; max is 65536.
You may try using --multi-dex option.
Older versions of the build system report a different error, which is an indication of the same problem:
Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
Both these error conditions display a common number: 65536. This number represents the total number of references that can be invoked by the code within a single Dalvik Executable (DEX) bytecode file. This page explains how to move past this limitation by enabling an app configuration known as multidex, which allows your app to build and read multiple DEX files.
About the 64K reference limit
Android app (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code. In the context of computer science, the term Kilo, K, denotes 1024 (or 2^10). Because 65,536 is equal to 64 X 1024, this limit is referred to as the ’64K reference limit’.
Multidex support prior to Android 5.0
Versions of the platform prior to Android 5.0 (API level 21) use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can add the multidex support library to your project:
dependencies {
def multidex_version = "2.0.1"
implementation 'androidx.multidex:multidex:$multidex_version'
}
If you aren’t using AndroidX, add the following support library dependency instead:
implementation 'com.android.support:multidex:1.0.3'
- Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
-
Depending on whether you override the Application class, perform one of the following:
If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:
<?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>
If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:
public class MyApplication extends MultiDexApplication { ... }
Or if you do override the Application class but it’s not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
1
This happens when your App has exceeded the methods/dex size limit (64K).
As your minSdk is 15, you have to add MultiDex manually which is not required on Api 21+.
Do the following for supporting MultiDex on Pre-21
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
Make your application class extend MultiDexApplication
public class Application extends MultiDexApplication {}
Or else you can use Proguard to strip off unused classes and methods if you don’t want to use MultiDex at all..
2