The number of method references in a .dex file cannot exceed 64k

Gradle build fails with error "The number of method references in a .dex file cannot exceed 64k "

As the error suggests you cannot have more than 64k methods in one classes.dex file. One way around this is to use enable MultiDex in your build.gradle

 
multiDexEnabled true
 

However, enabling MultiDex has some disadvantages. Also, if your minimum API is lower than 21 you will need to perform some additional steps, otherwise even though your app will compile it may crash on some older devices. Read this Google Developer article for more info.

Better solution is to reduce number of methods if at all possible. While you are unlikely to have over 64k methods in your average app, this limit includes all libraries and dependencies. Very often the issue is caused by a massive Google Play Services library. Luckily since version 6.5 you no longer have to include the whole Play Services library into your app and can select only those APIs that are relevant to you.

In an example below I have commented out full Google Play Service library and instead added only Ads and Analytics APIs.

 

Feb 2017
Android Studio 2.2.3

Newsletter

Subscribe to receive occasional updates on new posts.
Your email will not be used for any other purpose and you can unsubscribe at any time.
Please wait