Stackingcoder Stackingcoder - 4 months ago 25
Android Question

I can't build my app because of .dex file error. (Android)

I have a little problem here, if i want to build a APK in Android Studio
i get this error message:

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

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 '/usr/local/java/jdk1.8.0_77/bin/java'' finished with non-zero exit value 2


I found an answer on StackOverflow that said to compile this dependency in my Gradle file:

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


but that didn't work.

Here's my Gradle file:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.google.android.gms:play-services:9.0.2'
compile 'com.android.support:multidex:1.0.0'
}

Answer

This is a simple one - all you need is the dependency and to say multidex true. Your mind SDK must also be 14 or greater.

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

Then, configure your Application element within the Manifest:

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