When trying to start a project, I encountered the following 2 errors:

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: transformClassesWithDexForOkhttpDebug'. 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_60 \ bin \ java.exe '' finished with non-zero exit value 2

After google my first mistake, I found an interesting greeting on the official website:

If you have a built-in Android app, then you have a lot of code!

But what if my application is in fact not so clumsy and clearly does not need to be connected to multidex.

  • This is not only about your code, but also the code of all connected libraries. Can you vouch for them? - D-side
  • @ D-side Yes, of course, more than I can tell you that this application has not run for a long time and everything worked correctly before that. And the libraries there are not so much in fact. - Morozov

1 answer 1

  • Install the dexcount-gradle-plugin , it will show who uses how many methods and generate the html in the build folder.

  • Inspect your code, maybe you can get rid of some libraries, for example, guava (14,842). Google Play Services (29,460) should not be connected all at once.

  • Use proguard , which removes unused code.

If all else fails, use multidex

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

 defaultConfig { ... minSdkVersion 14 targetSdkVersion 21 .... multiDexEnabled true } 

 package ....; ... import android.support.multidex.MultiDex; public class MyApplication extends Application { .... @Override protected void attachBaseContext(Context context) { super.attachBaseContext(context); MultiDex.install(this); } } 

 <application ... android:name="android.support.multidex.MultiDexApplication"> ... </application> 
  • I immediately decided to install mulitdex. Since I didn’t work so closely with proguard, I didn’t want to connect plug-ins either. But the question is, you indicated that you can get rid of some libraries, namely GooglePlay Services. But what if I use maps in my application and they simply will not work. - Morozov
  • @Morozov you probably connected the entire library when you only need a map. Show app / gradle.build. - katso
  • compile 'com.google.android.gms: play-services: 9.8.0', I just know that they are connecting the library this way. - Morozov
  • one
    @Morozov try this compile 'com.google.android.gms:play-services-maps:9.8.0' compile 'com.google.android.gms:play-services-location:9.8.0' - katso
  • I understand that with prefixes -maps and -locations, we take only packages that we are interested in, and not the entire library? From 9.6.1 it did not work, it was updated to 9.8.0, it works correctly. - Morozov