Solving error on problem with MultiDex on Android

This article aims to shed some light on the problem of the MultiDex on Android.

You are facing the problem of MultiDex when you compile your APK and you get the following error:
“Too many field references: 68309; max is 65536.”

Some explanations

An APK contains DEX files (Dalvik Executable / .dex). These files contain bytecode, the language interpreted by the Android virtual machine.
DEX files have a number of up to 65,536 methods.
The virtual machine of Android is historically Dalvik then from Android 5.0 (Marshmallow) it is replaced by ART (Android Runtime).
Dalvik reads DEX files directly while ART compiles them into a single .oat file.

By default, an APK is limited to one DEX file per application. If your application contains more than 65,536 methods then several DEX files are needed and you need to configure the MultiDex.

Nowadays, we encounter this error frequently because of the large number of libraries that we use in our Android projects.

However correcting the problem of MultiDex induces a few edge effects, the main thing is that the application is slower on versions prior to Android 5.0.
That’s why Android recommends cleaning up the libraries and using ProGuard before configuring the MultiDex.

Set up MultiDex

If your application contains more than 65,536 methods then a configuration is needed. You must add multiDexEnabled to your app / build.gradle file:

android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 28
multiDexEnabled true
}
...
}

However if your app is for versions prior to Android 5.0 then you have to do 2 things:

1 / Add the MultiDex support library
2 / Use the MultiDexApplication class

1 / Add the MultiDex support library in app / build.gradle

dependencies {
compile 'com.android.support:multidex:1.0.3'
}

2 / Use the MultiDexApplication class

If you have not redefined the Application class for your app, all you need to do is use the MultiDexApplication class in the Manifest file of your project:

<?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 instead you have already redefined the Application class in the Manifest, you can inherit the class as follows:

public class MyApplication extends MultiDexApplication { ... }

And if your Application class already inherits another class, in this case you must redefine the attachBaseContext () method:

public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}

Conclusion

Despite these side effects, I advise you to install the MultiDex, it will avoid you to monitor your number of methods every time you install a new library and you will not need to think about it.
Android versions that use Dalvik and experiencing slowdowns now account for about 10% of activity on Google Play Store, this is in my opinion more than enough for no worries.

Large companies such as Uber, Facebook, Twitter etc … have adopted another strategy. They deploy an application called “lite” which are lighter in functionality and whose main purpose is to work better on older devices.

Source :

https://developer.android.com/studio/build/multidex

https://stackoverflow.com/a/38023900

Spread the love