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

13,530

Solution 1

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>

Solution 2

I was encountering a similar error with my app, and so I installed a plugin in Android Studio called Methods Count (http://www.methodscount.com/). It shows the number of method references each dependency uses, and it showed me compile path

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

had over 69k references on it's own

I changed it to show one of your dependencies instead:

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

and it shows 69,520 method references as dependencies for this compile path.

It is likely you aren't using the extent of the compile path, and may be able to specify more focused paths to eliminate a chunk of used method references and get way under the 65k maximum. A list of individualized services are here.

In my case, I can only imagine I added that gms services line about a year ago when I incorporated Firebase into my app, but can't find the same reference on the Firebase website.

Just realizing a comment on your question says a similar thing about breaking up the dependency into only what you need.

Solution 3

you have mutlidex enabled for release flavour, you shoud have this line also for debug flavour

debug {
    multiDexEnabled true
}

release {
    shrinkResources true
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    multiDexEnabled true
}
Share:
13,530
baldraider
Author by

baldraider

Updated on June 03, 2022

Comments

  • baldraider
    baldraider about 2 years

    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?