2

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'
}
Jeeter
  • 5,887
  • 6
  • 44
  • 67
Soufyane Kaddouri
  • 315
  • 3
  • 6
  • 14
  • Check this topic: http://stackoverflow.com/questions/26925264/android-support-multidex-library-implementation – Rafal Jul 12 '16 at 21:51
  • Android developers link that you mentioned is covering all the stuff about 64k limit. And yeap, you definitely need to include multidex support library. But at first just enable proguard and have a look how it will help you, – Yurii Tsap Jul 12 '16 at 21:54
  • Please [edit] to show your build.gradle file and we can tell you to remove some huge library. Including many libraries is an anti-pattern in Android development – OneCricketeer Jul 12 '16 at 22:07
  • On a related note, check this http://stackoverflow.com/questions/36698816/gradle-what-is-a-non-zero-exit-value-and-how-do-i-fix-it – OneCricketeer Jul 12 '16 at 22:08
  • Read the second blue box on this page. https://developers.google.com/android/guides/setup – OneCricketeer Jul 13 '16 at 00:07
  • Possible duplicate of [Unable to execute dex: method ID not in \[0, 0xffff\]: 65536](http://stackoverflow.com/questions/15209831/unable-to-execute-dex-method-id-not-in-0-0xffff-65536) – Alex Lipov Jul 17 '16 at 08:07

2 Answers2

3

You need to enable multidexing in your application, done by setting the multiDexEnabled flag in your build.gradle to true. Keep in mind that your minimum SDK version 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>
Jeeter
  • 5,887
  • 6
  • 44
  • 67
apelsoczi
  • 1,102
  • 8
  • 11
0

Try this inside your android { // } build.gradle file:

dexOptions {
    javaMaxHeapSize "4g"
    incremental true
    preDexLibraries = false
}

Then in your dependencies, add this:

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

Update

If you are extending Application in your project, switch to extend this:

public class MyApp extends MultiDexApplication{

   //inside here, override

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

Finally, set your application name in the AndroidManifest file as

<application 
   android:name=".MyApp">

This should save you from this issue; good luck!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • The manifest still needs updated to use the MultidexApplication class. Simply adding the gradle dependency doesn't fix the problem – OneCricketeer Jul 12 '16 at 22:09
  • You don't need to extend that class, you can directly use in the Manifest. For reference, your extension [doesn't do anything extra](https://android.googlesource.com/platform/frameworks/multidex/+/master/library/src/android/support/multidex/MultiDexApplication.java) – OneCricketeer Jul 12 '16 at 22:16
  • This case applies to a situation where you already extend Application class but yes, you don't need to if you have no customer application class – Eenvincible Jul 12 '16 at 22:18