65

I'm trying the new MultiDex Support on my app and so far I've managed to compile my app correctly, but when running it, I get the following exception:

java.lang.RuntimeException: Unable to instantiate application android.support.multidex.MultiDexApplication: java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/me.myapp.main-2.apk"],nativeLibraryDirectories=[/data/app-lib/me..main-2, /vendor/lib, /system/lib]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
    at android.app.ActivityThread.access$1500(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5086)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.multidex.MultiDexApplication" on path: DexPathList[[zip file "/data/app/me.myapp.main-2.apk"],nativeLibraryDirectories=[/data/app-lib/me.myapp.main-2, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    at android.app.Instrumentation.newApplication(Instrumentation.java:998)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:502)

This is my gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}


dependencies {
    compile 'com.android.support:multidex:1.0.0'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.android.support:appcompat-v7:21.0.0'
}

And my AndroidManifest.xml:

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

I don't understand what the problem is. I think I'm doing everything according to the documentation. Is there something else i am missing? I made sure I had the latest support library and repo installed from the SDK manager.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35
  • Did you also ensure you have the latest Build Tools version? Could you also specify whether the stack trace is from the debug or release build? – PaulR Nov 05 '14 at 18:38
  • 1
    @PaulR I have the 21.1 Build Tools version and the stack trace belongs to the debug version. – Roberto Betancourt Nov 05 '14 at 18:55
  • @dustedrob Strange. Verify first that MultiDexApplication is part of your maindexlist. This text file can be found under /app/build/intermediates/multi-dex/{debug/release}/maindexlist.txt – Alex Lipov Nov 05 '14 at 20:53
  • @AlexLipov MultiDexApplication is in the maindexlist.txt file. – Roberto Betancourt Nov 05 '14 at 21:14
  • @dustedrob Great. Can you decompile your main dex file (extract it from your output apk) to verify that MultiDexApplication class is actually exists there? You can find in this answer how to do that: http://stackoverflow.com/a/4177581/1233652 – Alex Lipov Nov 05 '14 at 21:25
  • @AlexLipov I couldn't find MultiDexApplication anywhere in the decompiled file – Roberto Betancourt Nov 05 '14 at 21:44
  • @dustedrob Can you reproduce it on a new project, or it happens only on a specific one? Can you share the project (if not, maybe remove all the proprietary code, and share just a project with a simple activity)? – Alex Lipov Nov 06 '14 at 06:09
  • The answer below by Andrew Chen solved it. Apparently, it's related to the Gradle version. – Roberto Betancourt Nov 06 '14 at 19:03
  • For AndroidX use android:name="androidx.multidex.MultiDexApplication" – Sviatoslav Zaitsev May 31 '19 at 13:56
  • Clean -> Rebuild helps when you think you followed all the steps but still you are getting the same error. – Rohit Singh Sep 19 '19 at 02:07

15 Answers15

67

The solution didn't help me because I was using jetpack version ie androidx. libraries.

Followed official doc.
And
I had to change name to androidx....Multidex.

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

Hope It helps other people looking for adding multidex with jetpack.

Man
  • 2,720
  • 2
  • 13
  • 21
  • 6
    Your answer is the correct one, many people copy and paste the code from the documentation and don't notice that they are using androidx instead. – matshidis Jul 09 '19 at 09:29
  • Your solution also worked with Unity build 2018 with Mono, thanks – VectorX Feb 22 '20 at 22:37
  • You are absolutely right @Man, It happens to me several times copying support library from Official documentation only to realise I'm using Jetpack – Ally Apr 24 '20 at 12:07
  • This will only *work* if you are not extending Application (as it states in the docs). If you are, you need to do this instead: Application extends MultiDexApplication – Steve Moseley Jan 01 '22 at 04:25
26

In my case its solved by changes below code in Manifest from

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

to

android:name="androidx.multidex.MultiDexApplication"
Pooja
  • 475
  • 5
  • 14
19

My configuration:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.+' // 0.14.1, 2014-11-6
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.0'
}

android {
    compileSdkVersion = 21
    buildToolsVersion = "21.1.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        multiDexEnabled true
    }
}

Unfortunately, I have the same problem. But I found a strange situation:

build/intermediates/dex/debug:

-rw-rw-r--  1 andrew andrew  2221176 Nov  6 20:18 classes2.dex
-rw-rw-r--  1 andrew andrew  8357596 Nov  6 20:18 classes.dex

unzip apk, build/outputs/apk:

-rw-rw-r-- 1 andrew andrew 8357596 Nov  6 20:18 classes2.dex
-rw-rw-r-- 1 andrew andrew 2221176 Nov  6 20:18 classes.dex

In apk, the main classes of classes.dex should be bigger than classes2.dex, but its not. I do also dex2jar & unzip jar to check classes, the application class is not there in classes.dex, its in classes2.dex contrarily.

However, I should have fixed it. Here is my patched android gradle plugin you can try:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://github.com/yongjhih/android-gradle-plugin.m2/raw/master/' }
    }
    dependencies {
        classpath 'com.infstory.tools.build:gradle:0.14.+'
    }
}

The patch is in: https://github.com/yongjhih/android-gradle-plugin/commit/9c2212e3b1b4c6e1f7b47f2086aba1903a6258bf or https://android-review.googlesource.com/#/c/113331/

issue: https://code.google.com/p/android/issues/detail?id=78761

The official patch is https://android-review.googlesource.com/#/c/113201/ that already been merged, I think it might be fixed in next version.

Already been fixed 0.14.2 (2014/11/10). (from http://tools.android.com/tech-docs/new-build-system)

Release notes:

0.14.2 (2014/11/10)

Fix potential multi-dex issue where the dex files could be renamed during packaging, leading to the wrong main dex file being used.
Fix versionNameSuffix support
Fix BuildType.initWith to copy shrinkResources flag
setup default proguard rule file if none are provided (SDK/tools/proguard/proguard-android.txt)
BuildType.pseudoLocalesEnabled flag to include fake locales in apk.
Andrew Chen
  • 447
  • 7
  • 14
  • Hey @Andrew Chen and @Roberto Betancourt, i tried adding the dependency and the repositories, but I'm getting an error on build which is `Error(1,0) TEST_SDK_DIR` open file. – XY-JOE Nov 22 '17 at 21:09
  • @XY-JOE, Do you use latest android-gradle plugin? 2.3.3 or 3.0.1 – Andrew Chen Jan 10 '18 at 01:58
  • 1
    @AndrewChen i'm using the gradle 2.3.3 but i don't know exactly what i did exactly but i no longer have this error. Thanks for your help – XY-JOE Jan 25 '18 at 11:42
  • 1
    I am even using AndroidX multidex v2.0.0 and implementing Application as MultidexApplication. Still crashes on lower API <= 19 devices – Neon Warge Aug 15 '18 at 00:17
9

Setting up your app project to use a multidex configuration requires that you make the following modifications to your app project, depending on the minimum Android version your app supports.

If your minSdkVersion is set to 21 or higher, all you need to do is set multiDexEnabled to true in your module-level build.gradle file, as shown here:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
    }
    ...
}

However, if your minSdkVersion is set to 20 or lower, then you must use the multidex support library as follows:

  • Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

    android {
        defaultConfig {
            ...
            minSdkVersion 15 
            targetSdkVersion 26
            multiDexEnabled true
        }
        ...
    }
    
    dependencies {
      compile 'com.android.support:multidex:1.0.1'
    }
    
  • Depending on whether you override the Application class, perform one of the following:

    • If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapp">
          <application
                  android:name="android.support.multidex.MultiDexApplication" >
              ...
          </application>
      </manifest>
      
    • If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

      public class MyApplication extends MultiDexApplication { ... }
      
    • Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

      public class MyApplication extends SomeOtherApplication {
        @Override
        protected void attachBaseContext(Context base) {
           super.attachBaseContext(base);
           MultiDex.install(this);
        }
      }
      
  • I have minSdkVersion is set to 21 and after build the app crashed with bug could not find class android.support.multidex.MultiDexApplication – Oliver D Nov 29 '19 at 01:12
7

I recently had this issue. Despite no change to my configuration this error started occurring. I tried all of the suggested solutions including deleting the virtual device and creating a fresh one.

However, I did notice that if I built an APK and dragged it into the emulator to install, it worked fine.

In the end cleaning the project and re-running and then it worked.

sorter
  • 513
  • 6
  • 8
  • 1
    I did exactly what they mentioned on the documentation still I was getting the same error. I did "clean project"->"rebuild project" and it solved the problem. Thanks – Rohit Singh Sep 19 '19 at 02:05
7

For me, the answer was to switch to the androidx versions

android:name="androidx.multidex.MultiDexApplication"

implementation "androidx.multidex:multidex:2.0.1"

Martijn Coevert
  • 131
  • 2
  • 4
3

I was using the latest Android Studio and following the official document to add support to multidex https://developer.android.com/studio/build/multidex.html . But I still got the same exception. I spent a lot of time solving it. At last, I found the cause was I enabled "Java Exception Breakpoints - Any exception", and "Exception Breakpoints - When any is thrown". After I disabled them, the app ran without problems.

Just follow the official document about multidex https://developer.android.com/studio/build/multidex.html . It works.

Vince Yuan
  • 10,533
  • 3
  • 32
  • 27
2
Delete ./gradle .idea and build directory
Clean project
Uninstall app if already install on device and again install
Makvin
  • 3,475
  • 27
  • 26
2

In my case I just changed the build.gradle and I leaved the AndroidManifest like that:

android:name=".MainApplication"

I am using React Native.

miqrc
  • 1,964
  • 2
  • 18
  • 24
2

i jaust changed in manifest application

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

to android:name="androidx.multidex.MultiDexApplication"

and it's working fine

rabie
  • 41
  • 4
1

This worked for me.

File -> Sync Project with Grandle Files

just aguy
  • 47
  • 8
0

Very strange, I got same error message, but after I fixed some gradle lines it worked. Maybe it helps somebody:

Project build.gradle:

from:

..

dependencies {
        // Android Gradle Plugin
        classpath 'com.android.tools.build:gradle:3.2.1'
        // Analytics
        **classpath 'com.google.gms:google-services:3.0.0'**
}

..

to: ..

dependencies {
        // Android Gradle Plugin
        classpath 'com.android.tools.build:gradle:3.2.1'
        // Analytics
        **classpath 'com.google.gms:google-services:3.2.1'**
}

..

In the module build.gradle:

from:

..

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:support-compat:26.1.0'
    implementation 'com.android.support:appcompat-v7:26.1.0'
    **implementation 'com.google.android.gms:play-services-analytics:12.0.1'**
}

..

to:

..

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:support-compat:26.1.0'
    implementation 'com.android.support:appcompat-v7:26.1.0'
    **implementation 'com.google.android.gms:play-services-analytics:16.0.5'**
}

..

There was no other changes in my code, but the fixed version worked on 4.x emulators too. Only the ** marked lines are changed.

Miki
  • 440
  • 4
  • 7
0

The Problem is in Gradle File Please Increase the Version That's it

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'

    }
}
0

android { compileSdkVersion 30 buildToolsVersion "30.0.1"

defaultConfig {
    applicationId "com.nandeproduction.dailycost"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}}

[Solution] Use this special code

compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
}
Thilina Chamika
  • 206
  • 3
  • 4
0
  1. Go to manifest -> inside application tag -> directly put your class name with dot or check the package of that file and add that manually.

    android:name=".AppController"

Muhammad Ali
  • 1,055
  • 6
  • 11