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

Overview

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 multidex-config.pro 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!

 

 


  • Chandrashekar REddy

    Thank you.. saved my day

  • Benjamin Lacroix

    Hi! Thanks for this post, very helpful.
    But I still get this error when running on Android 4.1 / 4.2 devices …. I don’t understand why since I did everything you wrote in this article.. Could you help me ?
    https://stackoverflow.com/questions/46600992/multidexkeepfile-ignored-not-working

    • Hi, thanks. Quick solution – minifyEnabled false for every build type. Clean project, rebuild

      Does it happen in both release/debug builds?

      Try adding

      -keep me/blu/app/helper/CustomApp.class

      Do you have just one custom proguard rules file?

      I’ll add more detailed answer in few minutes

      • Benjamin Lacroix

        Thanks a lot for your answer!
        This happens for release and debug builds yep, but only for android prior to 5. I checked and my class that extends MultiDexApplication is on the classes2.dex (not the classes.dex), I don’t know how to change that and if it’s the main source of this problem.

        Well, I’m not using proguard at all for now ..

        • Yes, that’s the main cause. Classes that are loaded first (like Application should be in the first file)

          • Benjamin Lacroix

            Yep I followed all the steps, I also deleted build folders and .gradle folder to be sure, but the problem is still the same, even by installing the apk manually (not using Android Studio) :/

          • Sorry, busy now, buy I’ll get back to you in the evening!

          • Benjamin Lacroix

            Thanks a lot, I really appreciate!! 🙂

          • Benjamin Lacroix

            I tried by setting
            compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
            }
            but it didn’t work ..
            Do you have a last magic trick ? ^^

          • I guess you’ll just have to go through every Stack solution out there, man

          • Benjamin Lacroix

            Well, I did, during 3 days 🙁
            I only have this error since I extended MultidexApplication class because I have to init a few SDK there, everything was working properly before that