13

I'm trying to add Multidex support to my app. But I get an error:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication" on path: DexPathList[[zip       file "/data/app/com.dfn.partner-2.apk"],nativeLibraryDirectories[/data/app-lib/se.android-2, /vendor/lib, /system/lib]]

My gradle have:

defaultConfig {
   multiDexEnabled true
}

and

depenencies {
   ...
   implementation 'androidx.multidex:multidex:2.0.1'
   ...
}

My AndroidManifest.xml have :

<application
  android:name="androidx.multidex.MultiDexApplication"
  ... >
...
</application>

What could be wrong?

Vladyslav Panchenko
  • 1,517
  • 1
  • 19
  • 23
  • It appears to be looking for the androidx version of the multidex support library. Someone else might be able to give a more complete answer as to what is going wrong here but try changing the mutlidex dependency to the androidx version `androidx.multidex:multidex:2.0.1`. When you do this you will likely need to update the `android:name` of your application in the manifest to `androidx.multidex.MultiDexApplication` as well. – George Mulligan Jan 24 '19 at 15:15
  • @GeorgeMulligan I was mistaken in the question, I use `androidx.multidex.MultiDexApplication` and `'androidx.multidex:multidex:2.0.1'` in the application and an error appears – Vladyslav Panchenko Jan 24 '19 at 15:27
  • @GeorgeMulligan I just tried different options. – Vladyslav Panchenko Jan 24 '19 at 15:28
  • There is a similar question [here](https://stackoverflow.com/q/53203590/1435985). For your APK can you see if the multidex related classes are also found outside of the primary dex file (classes.dex) using the APK analyzer in Android Studio? – George Mulligan Jan 24 '19 at 16:05
  • Did u have used custom application class ?? – Md.ibrahim khalil Jan 24 '19 at 16:07
  • @Md.ibrahimkhalil yes – Vladyslav Panchenko Jan 24 '19 at 16:13
  • See the answer. – Md.ibrahim khalil Jan 24 '19 at 16:19

6 Answers6

19

I got this error after adding multidex until I cleaned:

./gradlew clean

weston
  • 54,145
  • 21
  • 145
  • 203
  • 1
    After I added multidex from androidx I got his error. "Clean Project", then "Syncing project with Gradle files", then run again and it worked. – Jorge Gil Oct 07 '19 at 19:28
17

I cleaned the project:

Build -> Clean Project

and then I went to:

File -> Invalidate Caches / Restart

Compile, now should work!

Muhammad Ahmed
  • 144
  • 1
  • 8
itzo
  • 1,220
  • 14
  • 18
  • This worked for me. You need to do them in exactly the same order as written in the answer. – Murat Feb 05 '21 at 16:37
2

For api level < 21, platform uses Dalvik runtime for executing application code. For such cases, multidex library should be a part of the primary DEX file of your app which can then manage access to the additional DEX files and the code they contain.

In your case, looks like Multidex library is not present in the primary dex file and hence during startup, you app is giving you error: java.lang.ClassNotFoundException: Didn't find class "androidx.multidex.MultiDexApplication"

You need to explicitly specify these multidex classes in either multiDexKeepFile or multiDexKeepProguard to mark those as required in primary dex.

multiDexKeepFile

android {
    buildTypes {
        release {
            multiDexKeepFile file('multidex-main-dex-list.txt')
            ...
        }
    }
}

Content of multidex-main-dex-list.txt goes as below:

androidx.multidex.MultiDexApplication

multiDexKeepProguard

android {
    buildTypes {
        release {
            multiDexKeepProguard file('multidex-main-dex-list.pro')
            ...
        }
    }
}

Content of multidex-main-dex-list.txt goes as below:

-keep class androidx.multidex.MultiDexApplication

nitika
  • 21
  • 5
  • Oddly enough, I didn't have keep class multidex in prior to migrating to AndroidX libraries but after migrating to AndroidX library I needed to add -keep class androidx.multidex.MultiDexApplication to ensure the application runs on pre android 5 devices – user1652110 Jan 01 '21 at 00:08
1

if you are using androidx use depedency below

dependencies {
    // ...
    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
}

for support library use

dependencies {
    // ...
    implementation 'com.android.support:multidex:1.0.3'
}
TooCool
  • 10,598
  • 15
  • 60
  • 85
Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
1

In my case I had to replace in my build.gradle.kts (module: app):

implementation("com.android.support:multidex:1.0.3")

with

implementation("androidx.multidex:multidex:2.0.1")

and add the following in my gradle.properties:

android.useAndroidX=true
rsc
  • 10,348
  • 5
  • 39
  • 36
0

If you're using vector as images, change android:src to app:srcCompat in your Xml

Terranology
  • 610
  • 9
  • 15