Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MultiDex with a custom Application class?

Tags:

android

Ok so I am making an App that has uses the .App class to get Context statically, the class does not work unless placed in manifest under <application android:name=.App but the issue is I clocked 65k so I have multiDex and MultiDex too needs to be in Manifest under the <application android:name=.MultiDex otherwise my App will not run, how can I get past this problem without affected multidex which is my worst fear looking at how much trouble I went through just to make it work?

like image 998
Sindy McMore Avatar asked Nov 08 '15 02:11

Sindy McMore


People also ask

What is multidex class?

In Android, the compilers convert your source code into DEX files. This DEX file contains the compiled code used to run the app. But there is a limitation with the DEX file. The DEX file limits the total number of methods that can be referenced within a single DEX file to 64K i.e. 65,536 methods.

What is a multidex application?

Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration. So, the feature is: it allows your complex app to compile. The scenarios for using it are when your app fails to compile due to hitting the 64K DEX method reference limit.

Is multidex needed?

Multidex support library is enabled by default in API level higher than 21 (Android 5 and higher). Therefore, you do not need to add the Multidex support library.

Where do I put multiDexEnabled true?

Step 3: Working with build.gradle(module) fileGo to build. gradle file, inside that you will find the scope { } of defaultConfig then inside that scope just add multiDexEnabled = true as shown below then click sync now.


2 Answers

Check out this page:

Note: If your app uses extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. For more information, see the MultiDexApplication reference documentation.

In other words, use your own App class but add the following:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
like image 70
Sijmen Mulder Avatar answered Sep 18 '22 09:09

Sijmen Mulder


BaseAppplication is custom Application class

public class BaseApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            // your custom code here
        }



    // Add multidex Code or other Application Class here 
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
like image 45
Bhadresh Avatar answered Sep 22 '22 09:09

Bhadresh