Search code examples
androidandroid-multidex

What's the difference between extending MultiDexApplication and MultiDex.install(this)?


From the documentation, I read that there are two ways to support MultiDex in devices below API 21:

  1. Having the Application class extend MultiDexApplication, and
  2. Using MultiDex.install(this) in the Application class's onAttachBaseContext(Context base) function in case the Application extends something else.

Are they basically the same with extending MultiDexApplication calling MultiDex.install(this) in the onAttachBaseContext() by default, or are there differences between both methods?


Solution

  • they are two ways to enable multidex for your app and they are completely same

    if you want to create a class for application just for enable multidex you can just put MultiDexApplication for application name in AndroidManifest and don't need to do more because in MultiDexApplication overrided attachBaseContext() , look :

    public class MultiDexApplication extends Application {
        public MultiDexApplication() {
        }
    
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    

    and if you have application class end it extends just Application you also can change extends to MultiDexApplication instead of ovverride attachBaseContext() method , otherwise you have application class that it doesn't extends from Application, so you have to override attachBaseContext() and your custom Application class