How to solve Google Play Services version collision in gradle dependencies

Today I spent whole day debugging my Android build process in React Native. When I build app, everytime when I used react-native-maps component, my app crashed. After few minutes of googling, I found this issue is related to version Google Play Services (com.google.android.gms) that collide between react-native-maps and react-native-fcm.

I tried few simple fixes from few guides. I changed my android/app/build.gradle from this:

My origin dependencies

To this:

As you can see, I am excluded react-native-maps internal package dependecies on Google Play Services (com.google.android.gms) and force of use same version as com.google.firebase:firebase-core (Firebase is part Google Play Services so you need same version too !important). I run my build, but it failed with this error:

Failed on android with com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzpq

Or after few small changes I got this:

Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.8.0.

It wasn't able to find solution whole day

Solution

I was trapped between these two errors whole day. But I finally found the solution and created universal guide how to solve this problem. You need to search over your native dependecies in node_modules for their build.gradle and in this files look for dependecies starting with com.google.android.gms or com.google.firebase. After that you need to exclude these internal dependecies and force use of same version everywhere.

In my case it was modules react-native-fcm, react-native-maps, react-native-device-info and this is how their build.gradle look:

react-native-maps
react-native-fcm
react-native-device-info

As you can see, react-native-maps has dependency on com.google.android.gms:play-services-base/maps of version 9.8.0. React-native-fcm has dependency on com.google.firebase:firebase-core/messaging of version + (in simple, + means latest available version) and react-native-device-info has dependency on latest version of com.google.android.gms:play-services-gcm

So I excluded all these internal dependencies and force of use of the same version. This is my final android/app/build.gradle:

Don't forget run gradlew clean command before build!!!

Enjoy.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.