android multidex: enabled but not helping

312 Views Asked by At

I updated my app to use multidex

multiDexEnabled true

and upped my minSdkVersion to 21:

ext {
        buildToolsVersion = "30.0.3"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 30
        javaVersion = JavaVersion.VERSION_1_8
    }

so when I start building my app I first get this

> Configure project :app
[SafeDK] Your project has been updated to support Multi-Dex!
With Multi-Dex support you can use as many methods as you like in your application (no 65K method limit)

but later the build fails with:

[SafeDK-ERROR] Main Dex exceeded 65K methods or field references, Exception: Main Dex exceeded 65K methods or field references

why is that? how is this contradiction possible?

1

There are 1 best solutions below

0
Top4o On

According to android docs:

If your minSdkVersion is set to 21 or higher, multidex is enabled by default and you do not need the multidex library.

So you do not need:

multiDexEnabled true

Still if you face this issue make sure:

You also have:

dependencies {
 implementation 'androidx.multidex:multidex:2.0.1'  //with androidx libraries
 //implementation 'com.android.support:multidex:1.0.3'  //with support libraries
}

In the manifest:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.multidex.myapplication">
        <application
            ...
            android:name="androidx.multidex.MultiDexApplication">

            <!-- If you are using support libraries use android:name="android.support.multidex.MultiDexApplication" -->

            <!--If you are using your own custom Application class then extend -->
            <!--MultiDexApplication and change above line as-->
            <!--android:name=".YourCustomApplicationClass"> -->

            ...
        </application>
    </manifest>

Finlly:

If you are using your own Application class, change the parent class from Application to MultiDexApplication.

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    MultiDex.install(this);
}