4

Whenever I build by project and prepare for launch on my device, I keep getting this error:

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

Additionally, I get 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.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_91\bin\java.exe'' finished with non-zero exit value 2

And it says gradle build failed with two errors

So I have some questions:

  • What are dex files
  • I'm not using them directly, so why do I get the errors above?
  • What are in dex files?
  • Do these files have anything to say for the .APK file size?

These errors started appearing again after I stopped using proguard for debug builds as the StackTrace did not show and right before I activated proguard I had the error.

I had this error once before and I deleted the dex folder and it solved it but now it suddenly isn't enough anymore.

my gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.myproject"
        minSdkVersion 15
        targetSdkVersion 23

    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
}

dependencies {

    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.google.android.gms:play-services:9.2.0'
    compile 'com.google.android.gms:play-services-ads:9.2.0'
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    You should **never** use the full `play-services` dependency. Only use the [select Play services APIs you need](https://developers.google.com/android/guides/setup#split). Including the entire thing is 45k+ of the 64k method limit. – ianhanniballake Jul 05 '16 at 20:15

3 Answers3

4

The more dependencies you add - the higher your method count. Change your gradle to reflect this:

defaultConfig {
    ...
    minSdkVersion 15
    targetSdkVersion 23
    ...

    // Enabling multidex support.
    multiDexEnabled true
}

This is a straight forward fix, and only works if you minSdkVerison is 14 or above. Also add the following dependency:

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

Sometimes, the dependencies which you leverage will (individually) reference the same libraries - you may have many methods which are doubled, tripled, quadruple, etc... Check out this SO answer to resolve this and always reduce your APK size. gradle - library duplicates in dependencies

Community
  • 1
  • 1
apelsoczi
  • 1,102
  • 8
  • 11
  • Does it at all affect the size of the APK? – Zoe Jul 05 '16 at 20:13
  • Read the article in the error message in full - it is all very insightful! To answer this comment though: https://developer.android.com/studio/build/multidex.html#dev-build – apelsoczi Jul 05 '16 at 20:15
  • 1
    FWIW, you can *never* have multiple dependencies in your app from the same library - they are always deduped by Gradle. – ianhanniballake Jul 05 '16 at 20:42
1

From Google documentation :

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code.

To solve this you need to make following changes in your code :

build.gradle

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.0'
}

AndroidManifest.xml

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

When these configuration settings are added to an app, the Android build tools construct a primary dex (classes.dex) and supporting (classes2.dex, classes3.dex) as needed. The build system will then package them into an APK file for distribution.

Please note : If you already have class in your code which extends from Application class then just change it to extend from MultidexApplication.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0

The playservices lib is quite huge. Even a tiny app may suffer from that problem. After using the Proguard the problem will disappear due to the optimizations removing unused stuff. For development (debug builds) - limiting libs only to what you're really using usually helps. You can try narrowing the library or/and use 'exclude' filter in the compile() gradle statement.

For example, when I needed it for the location services, I've used:

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

at first and got that message. Replaced it with the

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

and got it resolved gracefully.

halxinate
  • 1,509
  • 15
  • 22