The number of method references in a .dex file exceed 64K

baldraider picture baldraider · May 25, 2016 · Viewed 13.4k times · Source

Currently working on my android application after including play services and firebase library in my project I'm getting this error and unable to run my code

:app:prePackageMarkerForDebug :app:transformClassesWithDexForDebug To run dex in process, the Gradle daemon needs a larger heap. It currently has approximately 910 MB. For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB. To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties. For more information see https://docs.gradle.org/current/userguide/build_environment.html Error:The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html :app:transformClassesWithDexForDebug FAILED Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

My build.gradle file is here :

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "xyz.in.network"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        multiDexEnabled true
    }
}
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile project(':libs:ViewPagerIndicator')
   compile 'com.google.android.gms:play-services:9.0.0'
   compile 'com.android.support:appcompat-v7:23.4.0'
   compile 'com.android.support:design:23.4.0'
   compile 'com.google.android.gms:play-services-maps:9.0.0'
   compile 'com.google.android.gms:play-services-location:9.0.0'
   compile 'com.android.support:cardview-v7:23.4.0'
   compile 'com.getbase:floatingactionbutton:1.10.1'
   compile 'com.squareup.picasso:picasso:2.5.2'
   compile 'com.android.volley:volley:1.0.0'
   compile 'com.google.firebase:firebase-messaging:9.0.0'
   compile 'com.android.support:multidex:1.0.1'
   }

   apply plugin: 'com.google.gms.google-services'

And my manifestfile is here

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="android.support.multidex.MultiDexApplication"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
<activity android:name=".Util.DisconnectedNetwork"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Transparent"></activity>

    <service android:name=".FCM.FirebaseMessagingHandler">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service android:name=".FCM.FirebaseRegistrationTokenHandler">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

After increasing the heap size to 2048M. Gradle give this error

Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

I follow all the instruction given on android developer site but still got this problem. How to solve this problem?

Answer

Hugo Gresse picture Hugo Gresse · May 25, 2016

You need to enable multidex in the android default config then:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.example.case"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 43
        versionName "4.0.13"

        // Enabling multidex support.
        multiDexEnabled true
    }

When you are building you application in a daily routine, you normally use the debug default flavor. So if you application has more than 65k method, you need ot enable it on all flavor.

As a side note, you may want to use Proguard on the debug build so you won't have to enable multiDex on it.

Full app/build.gradle (documentation)

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

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

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

Last part: add the MultiDex application in the manifest (or as a parent of your own Application

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