74

I'm using the below settings through gradle:

compileSdkVersion 21
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.2
ANDROID_BUILD_SDK_VERSION=21

I also have the following settings in my gradle file:

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

I always get the error UNEXPECTED TOP LEVEL EXCEPTION.
But when I make the 21.0.0 to 20.0.0 it works fine...but i'm not able to access any of the Android API 21 options. Is there something i'm doing wrong here? How do I get it to compile without this exception? I do not have the support jars anywhere else outside of other grade projects (facebook etc.).

Here is the full stack trace:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
    at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502)
    at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:283)
    at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302)
    at com.android.dx.command.dexer.Main.run(Main.java:245)
    at com.android.dx.command.dexer.Main.main(Main.java:214)
    at com.android.dx.command.Main.main(Main.java:106)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
KVISH
  • 12,923
  • 17
  • 86
  • 162

7 Answers7

148

This message sounds like your project is too large.

You have too many methods. There can only be 65536 methods for dex.

Since the gradle plugin 0.14.0 and the Build Tools 21.1.0 you can use the multidex support.

Just add these lines in the build.gradle:

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

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

Also in your Manifest add the MultiDexApplication class from the multidex support library to the application element

<?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="android.support.multidex.MultiDexApplication"> 
        ...
    </application>
</manifest>

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

Graham
  • 7,431
  • 18
  • 59
  • 84
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
45

1) add dependency to application build.gradle file

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

2) 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);
  }
}

3) Add multidex support to defaultConfig at build.gradle

defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        // Enabling multidex support.
        multiDexEnabled true
    }
...
georgehardcore
  • 975
  • 14
  • 12
  • Perfect! @georgehardcore – benchuk Sep 07 '15 at 13:35
  • @georgehardcore: i used second point, in my android eclipse project.but still it is showing same error. – Nageswara Rao.CH Oct 05 '15 at 11:13
  • try to split google-play-services library if you are uisng it. For example, if you use analytics and gcm just plug them as separate modules with base module like: compile 'com.google.android.gms:play-services-base:8.1.0' compile 'com.google.android.gms:play-services-analytics:8.1.0' compile 'com.google.android.gms:play-services-gcm:8.1.0' – georgehardcore Oct 22 '15 at 13:25
  • @georgehardcore not sure if step #2 would be needed, 'cos it didn't help me on my application. So I skip it and the rest of the steps worked great! – Vu Nguyen Dec 07 '15 at 23:16
  • For me it worked but without changing Application class to MultiDexApplication in the manifest nor MyApplication class – Amt87 Jun 19 '16 at 10:11
3

Before messing around with MultiDex

It either solution is too big (use MultiDex) or you compile and include in your project some not needed stuff. I got this issue on my tiny project. Here is what I did to fix it:

I removed this from build.gradle:

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

And I manually removed my build folder from application root. Problem fixed.

Andrei
  • 42,814
  • 35
  • 154
  • 218
2

Follow this guide to workaround 65K limit.

I solved this issue in gradle by setting multiDexEnabled true and extending my application with class MyApp extends MultiDexApplication

Jeffin
  • 1,099
  • 12
  • 23
Iñaqui
  • 251
  • 4
  • 9
0

I simply commented out the compile filetree(include: ['*.jar'], dir: 'libs') in the build.gradle file (as suggested above) and rebuilt the project. Compiled without any errors. I did not need to move any folders.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
Earl Allen
  • 29
  • 5
  • 1
    Disregard my comment - subsequent rebuilds generated the same original error even when I manually deleted the build folder. – Earl Allen Sep 07 '16 at 04:08
0

First make sure that minification is enabled in app/build.cradle:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        signingConfig signingConfigs.config
    }
}

If this does not solve the issue, enable multidex build for your app.

atomsymbol
  • 370
  • 8
  • 11
0

I faced this issue when i had compiled the entire package of Google Play Services APIs into my app. I solved the issue by using selective API.

For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

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

with these lines:

compile 'com.google.android.gms:play-services-fitness:11.0.2'
compile 'com.google.android.gms:play-services-wearable:11.0.2'

Issue is explained in following link.

Jeffin
  • 1,099
  • 12
  • 23
Gaurav Gupta
  • 575
  • 1
  • 5
  • 17