Posted on

Android MultiDex

android multidex
Designed by Freepik

Now probably most of the apps have 65k+ methods, which means we need to use multidex, but what is it and why we might still get some weird errors. Let’s take a closer look at Android MultiDex


But first what I want you to do is look into what’s your APK contains of. Using Build/Analyse APK you can see files inside it

android multidex

Classes.dex is what all it’s about today. It contains all the compiled code from your app. And if you checked my previous post, you know how to decompile and go through Java code from the APK itself.

This dex has 55k methods and has just few dependencies and ProGuard enabled. If you want to read more about ProGuard, check this post. So as it follows, it’s very easy to reach 65k methods limit and it will cause you this error

Enable MultiDex

To fix it you need to add the following lines to build.gradle file

If you don’t have a custom Application class, then add this line to manifest

Otherwise extend MultiDexApplication

That should be it and now you can successfully build your app. And if you click Build/Analyse APK, you’ll actually see multiple .dex files

Possible Error

S0 we enabled MultiDex, but still you might get an error. The most confusing was getting error on different devices, both were >= L.  And it appears on app launch time. What you might get is java.lang.NoClassDefFoundError. But why does it happen?

So it matters in which .dex file your classes end up. The ones your app uses on startup time needs to be in the first one. If it’s not – you get that error.  And build tools should be doing the job by recongizing which classes should be in the first dex file. In most cases it works, but like with ProGuard, not always.

You need to create a file in apps’s root directory and add classes that you want to end up in the first dex file. I called it multidex-config.txt

Then declare it in build.gradle

And for ProGuard create file

Amd declare it with


Alright, this should solve all the problems with multidex. Don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you think this will benefit them!