25

Trying to use MultiDexApplication in my app, but the class is not recognized when I try to extend my application activity with it.

Here is my build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
    defaultConfig {
        applicationId 'com.myapp'
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 115
        versionName '4.8'
    }

    buildTypes {
        debug {
            debuggable true
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            debuggable false
            runProguard true
            zipAlign true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    lintOptions {
        checkReleaseBuilds false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.google.android.gms:play-services:6.1.11'

    compile 'com.android.support:appcompat-v7:21.0.0'

    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.viewpagerindicator:library:2.4.1@aar'
    compile project(':facebook')
}

You can see that I'm compiling on 21, using the latest build tools, and the latest google play services and support library.

Has anyone gotten this to work?

Snicolas
  • 37,840
  • 15
  • 114
  • 173
JMRboosties
  • 15,500
  • 20
  • 76
  • 116

4 Answers4

46

MultiDexApplication class is not part of appcompat-v7 library. It is being shipped in a separate jar (called android-support-multidex).

Find the android-support-multidex.jar under /sdk/extras/android/support/multidex/library/libs (available from revision 21 of support library) and copy it to your project's libs folder.

Update (11/5/2014):
The jar is now available in central repository:

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

For more info, see here.

Update (27/9/2021):
Jetpack (AndroidX) users should add this as a dependency:

dependencies {
  ...
  implementation 'androidx.multidex:multidex:2.0.1'
}
Alex Lipov
  • 13,503
  • 5
  • 64
  • 87
  • 2
    Please instead reference the multidex library via maven/gradle: dependencies { compile 'com.android.support:multidex:1.0.0' } // Note, the library version number may be greater by the time you read this. – PaulR Nov 05 '14 at 18:45
  • 1
    Updated library version : 'androidx.multidex:multidex:2.0.0' – RamithDR Sep 25 '18 at 10:38
  • 1
    please use , implementation 'androidx.multidex:multidex:2.0.0' – Islam Alshnawey Sep 27 '21 at 12:28
25

Although this question is quite old, I got this error in a multi-module setup when trying to build the different modules together as one APK for API < 21. I already refactored to AndroidX, but the multidex docs don't mention AndroidX yet.

If you are using AndroidX, make sure to replace the old multidex dependency

compile 'com.android.support:multidex:1.0.3'

with the new one

implementation 'androidx.multidex:multidex:2.0.0'
jossiwolf
  • 1,865
  • 14
  • 22
  • 4
    add this line to Manifest android:name="androidx.multidex.MultiDexApplication" – Ivan Vovk Sep 25 '18 at 12:14
  • @IvanVovk How MultiDex is installed is completely up to the developer - be it through `MultiDex#install` or through registering `MultiDexApplication` in the Manifest. This is only relevant for including the MultiDex dependency in the first place. – jossiwolf Sep 26 '18 at 13:59
  • 2
    Jesus Christ Google, why don't you update your Documentation: https://developer.android.com/studio/build/multidex ?? – A. Steenbergen Mar 19 '19 at 15:39
2

I have followed THIS blog post according to which MultiDexApplication should be included in r21 of support library.

My IDE had trouble resolving it also.

I made it work for now with the help of MULTIDEX github project by adding (you can see more details on the project's page):

android {
    dexOptions {
        preDexLibraries = false
    }
}

repositories {
  jcenter()
}

dependencies {
  compile 'com.google.android:multidex:0.1'
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex' // enable multidex

        dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
    }
}

and adding $project_dir/multidex.keep file with following contents:

android/support/multidex/BuildConfig.class
android/support/multidex/MultiDex$V14.class
android/support/multidex/MultiDex$V19.class
android/support/multidex/MultiDex$V4.class
android/support/multidex/MultiDex.class
android/support/multidex/MultiDexApplication.class
android/support/multidex/MultiDexExtractor$1.class
android/support/multidex/MultiDexExtractor.class
android/support/multidex/ZipUtil$CentralDirectory.class
android/support/multidex/ZipUtil.class

The github project page mentions also some consideration for the contents of your implementation of MultiDexApplication class:

  • The static fields in your application class will be loaded before the MultiDex#installbe called! So the suggestion is to avoid static fields with types that can be placed out of main classes.dex file.
  • The methods of your application class may not have access to other classes that are loaded after your application class. As workarround for this, you can create another class (any class, in the example above, I use Runnable) and execute the method content inside it.
gswierczynski
  • 3,813
  • 2
  • 21
  • 22
  • What does the `--multi-dex` additionalParameter do? – IgorGanapolsky Nov 13 '17 at 20:28
  • 1
    It's been a while, but I think it is what is enabling multi dex. By now multidex should work without this workaround. Please see Alex's updated answer https://stackoverflow.com/a/26586969/724146 – gswierczynski Nov 15 '17 at 10:32
0

I got the solution :)

Upgrade to jdk 8 and change JDK location in Android Studio in

File > Project Structure > SDK Location

Find and change JDK location and click OK