On some device configuration, when running the Gradle build, it can happens that it fails with the following error :
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
About my case, it happened to me when trying to build an app using Google Play Services library. And I solved the issue after a few attempts, thinking it could be a generation issue due to my Android OS version 4.2; because I didn’t have problem on Android 6.0.
The origin of the error was simplier than what I thought, but hell, you have to know it. You can’t guess at all something like this.
Too much Google Play Services were included in the Gradle build file. Android have a Methods limit of 65536 and then, it’s advises by Google to simply include the submodules you actually NEED from the Google Play Services. So I basically needed 3 of it.
In my build.gradle
I had :
dependencies { .... compile 'com.google.android.gms:play-services:9.5.0' }
And I just replaced play-services by the 3 of the following submodules :
dependencies { .... compile 'com.google.android.gms:play-services-location:9.5.0' compile 'com.google.android.gms:play-services-gcm:9.5.0' compile 'com.google.android.gms:play-services-ads:9.5.0' }
At the next try of building the application, all went well 😉
Apparently this error can not be avoided in some cases, so maybe you want to compile also a dependency called « multidex » so it can increase the Methods limit higher than the 65k:
build.gradle :
dependencies { .... compile 'com.android.support:multidex:1.0.0' }
Hope it can help someone 😉