Geeky Singh
Geeky Singh

Reputation: 1013

unexpected top-level exception:

I tried to add Google play store services in my build.gradle file and the sync was successful. Now when I tried to run the application, I am getting following exception in Android Studio:

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:502)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':app:dexDebug'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' finished with non-zero exit value 2

Prior to adding play service, it was working fine. My build.gradle file looks as below:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig {
        applicationId 'com.xyz.app'
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "1.0.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'me.neavo:volley:2014.12.+'
    compile 'com.google.code.gson:gson:2.2.+'
    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
}

I searched the issue on Google and found that issue states that exceeded the limit of 65,536 methods in dex file

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

But my project is not that big enough.

Please tell me what I am doing wrong and suggest the approach to resolve it. Thanks.

Upvotes: 2

Views: 637

Answers (4)

Veer3383
Veer3383

Reputation: 1825

Dont include the entire play services dependency,

replace

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

with only modules you are using.

Eg.

 compile 'com.google.android.gms:play-services-location:8.4.0'

Rerfer this for your refrence,

incase you need to include the play services all modules,

refer to Radix's answer

Upvotes: 3

Nishant Srivastava
Nishant Srivastava

Reputation: 4791

You are exceeding your max method count limit of 65K . One solution for this is by using multidex option in gradle.

Modify your build.gradle as below

android {
  ...

  defaultConfig {
    ...
    minSdkVersion 14
    targetSdkVersion 23
    ...

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

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

Reference Link

Other solution would be to optimize your methods via proguard, which can be enabled via

minifyEnabled true

section.

Plus you can optimize your dependencies to avoid exceeding the 65K method count limit. Checkout this site, to get the instructions to install a plugin right into the Android Studio which would display the method count of each dependency in your build.gradle side by side. Taking that in view you can decide which dependency to keep and which one can be replaced with something thats less method hungry.

Upvotes: 4

tosif24
tosif24

Reputation: 93

Refer to this link.

Sometimes large library dependencies can also cause symbol size to go up. This link talks about avoiding 65K limit, by means like proguard.

or you can simply employ multidexing, which will create 2 classes.dex files for you, upon reaching large symbol size.

Upvotes: 1

santoXme
santoXme

Reputation: 802

basically this is caused because you exceed the limits of method.you have two options for solve this error .

1)Decrease the no of methods in your project .Basically the library you added in your projects methods also count in your project methods. so don't use whole package of that library use the module that you uses in your project:For example Google play services you added whole package add the module you want to use like map etc..

2)Second one Enable the multidex for enable multidex : Now, all you need to do is override this in your Application class:

public class YouApplication extends Application {

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

}

Add multidex support library in your gradal

 android {
compileSdkVersion 22
buildToolsVersion "23.0.0"

     defaultConfig {
         minSdkVersion 14 //lower than 14 doesn't support multidex
         targetSdkVersion 22

         // Enabling multidex support.
         multiDexEnabled true
     }
}

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

In your manifest add the MultiDexApplication class from the multidex support library to the application element like this

<?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>

I hope this will Help you....

Upvotes: 2

Related Questions