Gradle Build Error: Dex files cannot exceed 64k

Whenever I create a project and prepare to run on my device, I keep getting this error:

Error: the number of method references in the .dex file cannot exceed 64 KB. Learn how to fix this problem at https://developer.android.com/tools/building/multidex.html

In addition, I get this error:

Error: execution completed 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 '' completed with nonzero exit value 2

And he says gradle build failed with two errors

So, I have a few questions:

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

These errors started to appear again after I stopped using proguard for debugging collections, since StackTrace did not show, and right before I activated proguard, I had an error.

I had this error once before, and I deleted the dex folder, and she solved it, but now this all of a sudden is not enough.

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' } 
+5
source share
3 answers

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

 defaultConfig { ... minSdkVersion 15 targetSdkVersion 23 ... // Enabling multidex support. multiDexEnabled true } 

This is a direct fix and only works if you have minSdkVerison equal to 14 or higher. Also add the following dependency:

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

Sometimes the dependencies you use will (individually) reference the same libraries - you can have many methods that double, triple, quadruple, etc ... Check this SO answer to solve this problem and always reduce the size of the APK, gradle - duplicate libraries in dependencies

+4
source

From the google documentation:

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

To solve this problem, you need to make the following changes to 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 parameters are added to the application, the Android build tools create the primary dex (classes.dex) and support (classes2.dex, classes3.dex) as needed. Then the build system will pack them into an APK file for distribution.

Note: If you already have a class in your code that extends from the Application class, just change it to extend from MultidexApplication.

+1
source

The utility libs are pretty huge. Even a small application can suffer from this problem. After using Proguard, the problem will disappear due to optimized removal of unused material. For development (debug builds), limiting limits only to what you really use usually helps. You can try to narrow the library or / and use the exclude filter in the compile () gradle statement.

For example, when I need this for location services, I used:

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

At first I received this message. Replaced it with

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

and he received it gracefully.

0
source

All Articles