Search code examples
javaandroidandroid-manifestandroid-multidex

Extend MultiDexApplication in MyApp class


First of all, I was trying to create the class App that extends MultiDexApplication, like this :

public class App extends MultiDexApplication {

    private static Application application;

    public static Application getApplication() {
        return application;
    }

    public static Context getContext() {
        return getApplication().getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(getContext());
    }

}

But i was getting a message advising :

The <application> com.packageName.App is not registered in the manifest less... (Ctrl+F1)

warning

The documentation tells nothing about it.

So, how can I register it at manifest if it's already have the name writed?

 <application 
        android:name="android.support.multidex.MultiDexApplication"
        ...

By this docs, I simply stopped to use this named application given and use : android:name=".App"

And this is the class App created :

public class App extends Application {

    private static Application application;

    public static Application getApplication() {
        return application;
    }

    public static Context getContext() {
        return getApplication().getApplicationContext();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        application = this;
        MultiDex.install(getContext());
    }

}

Note that I'm calling MultiDex.install(this); at onCreate

Is this the corresponding method discussed here ?


Solution

  • I don't think you have to inherit MultiDexApplication. So you have your App class, you call MultiDex.install, you have your app class under <application/> tag in your manifest (full path or relative path - does not matter).

    Please also make sure your app module's build.gradle file has multiDexEnabled true under android/defaultConfig ?

    PS.: typical (relative path) way to specify App under manifest:

    
        <application
            android:name=".App"