Gradle APK build errors with Facebook Android SDK 4.x and Google Play Services

If you've tried to build an Android Studio project recently with the latest versions of the Facebook Android SDK (4.15) and Google Play Services SDK (9.4.0), you probably noticed that the two don't play nicely. Attempting to build an APK throws errors like the following:

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.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzr;

The problem stems from the Facebook Android SDK's dependency on the old version 8.4.0 of the play-services-ads package. To get your APK build working again, you'll need to manually declare this dependency in your project's build.gradle:

compile 'com.google.android.gms:play-services-ads:9.4.0'  
...
compile 'com.facebook.android:facebook-android-sdk:[4,5)'  
compile 'com.facebook.android:audience-network-sdk:[4,5)'  

This Stack Overflow post has a more detailed explanation of the problem.

MaroonMed

Read more posts by this author.

USA