Android MultiDex

kapil sharma
3 min readJan 26, 2023

--

If your app has a minSdk of API 20 or lower and your app and the libraries it references exceed 65,536 methods, you encounter the above build error that indicates your app has reached the limit of the Android build architecture.

Dex

Dex

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app.

Does my app have 65,536+ methods?

  • Methods are written by you + Android Framework methods + Third party library(eg Volley, Retrofit, Facebook SDK etc) methods.

Why 65k methods limit?

You can reference a very large number of methods in a DEX file, but you can only invoke the first 65536, because that’s all the room you have in the method invocation instruction.

The limitation is on the number of methods referenced, not the number of methods defined. If your DEX file has only a few methods, but together they call 70,000 different externally-defined methods, you’re going to exceed the limit.

MultiDex

Getting past the 65k limit requires that you configure your app build process to generate more than one DEX file, known as a multidex.

Now when you build your app, the Android build tools construct a primary DEX file (classes.dex) and supporting DEX files (classes2.dex, classes3.dex, and so on) as needed. The build system then packages all DEX files into your APK.

At runtime, instead of searching only in the main classes.dex file, the multidex APIs use a special class loader to search all of the available DEX files for your methods.

How to Configure MultiDex?

Note: If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you don't need the multidex library.

If your minSdkVersion is set to 20 or lower, then you must use the multidex library and make the following modifications to your app project:

Depending on whether you override the Application class, perform one of the following:

  • If you don’t override the Application class, edit your manifest file to set android:name in the <application> tag as follows:
  • If you do override the Application class, change it to extend MultiDexApplication, as follows:
class MyApplication : MultiDexApplication() {...}

If you override the Application class but it's not possible to change the base class, then instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

class MyApplication : SomeOtherApplication() {

override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
}

Multidex difference prior to Android 5.0 and later

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. To get around this limitation, add the multidex library to the module-level build.gradle

Android 5.0 (API level 21) and higher uses a runtime called ART that natively supports loading multiple DEX files from APK files. ART performs pre-compilation at app install time, scanning for classesN.dex files and compiling them into a single OAT file for execution by the Android device. Therefore, if your minSdkVersion is 21 or higher, multidex is enabled by default and you don't need the multidex library.

multidex-config.pro

if any class that’s required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError. You must manually specify the additional classes required in the primary DEX file by declaring them with the multiDexKeepProguard property in your build type. If a class is matched in the multiDexKeepProguard file, then that class is added to the primary DEX file.

You can create a file called multidex-config.pro that looks like this:

-keep class com.example.MyClass
-keep class com.example.MyClassToo
android {
buildTypes {
getByName("release") {
multiDexKeepProguard = file("multidex-config.pro")
...
}
}
}

Avoid the 64K limit

  • Review your app’s direct and transitive dependencies
  • Enable code shrinking to run R8 for your release builds.

--

--

No responses yet