Resolving Duplicate class error in React Native Android while integrating Native Modules

What is a Duplicate Class issue?

React native has evolved so much that you will really get helpful docs on external packages for most of the things. But there are some corner cases for which we do not find the npm modules to integrate. In those cases, we have to integrate SDK/package in the native side and then write a bridging code in order to call it from React-Native JS code.
  As your project grows and as you start integrating the new SDKs you will somewhere end up with this issue, “Duplicate class” in android. Basically, this issue says this module is already included in one of the SDK and the new SDK we are including also has the same modules and same classes and its a run time java error
  I was so panicked about this issue since I had got a big list of duplicate class issues as you see in the below image and error text below.

Java.lang.RuntimeException: Duplicate class 

com.google.android.gms.dynamite.descriptors.com.google.android.gms.vision.dynamite.face.ModuleDescriptor found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzca found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar 

(com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzcb found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)

  Duplicate class com.google.android.gms.internal.vision.zzcc found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzcd found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzce found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzci found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzcj found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzck found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar 

(com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzcl found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzcm found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.internal.vision.zzl found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-20.1.3-runtime.jar (com.google.android.gms:play-services-vision:20.1.3)


  Duplicate class com.google.android.gms.vision.face.ChimeraNativeBaseFaceDetectorCreator found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.vision.face.FaceDetectorV2Jni found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)


  Duplicate class com.google.android.gms.vision.face.NativeFaceDetectorImpl found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)

Solution for Duplicate Class Error in React Native

Do not worry about the big error message, basically, everything is a single issue but android is making us complicated in presenting. The solution to this issue is very simple but you need to understand the issue properly. Without understanding the issue and just trying with StackOverflow and Github answers it took about a week to fix this.
You will find plenty of the answers in GitHub and StackOverflow for this issue and, they are correct, but they have not elaborated the answer and think that people from the android background can solve this easily. But if you do not know much about the android part then please have a look at this solution.
For integrating a new SDK I got so many duplicate class issues as my project was big and had already many SDKs. In my list of Duplicate class issues, ALl issues are the same but for different modules. I will just one and explain about it.

Duplicate class com.google.android.gms.dynamite.descriptors.com.google.android.gms.vision.dynamite.face.ModuleDescriptor found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar (com.google.firebase:firebase-ml-vision-face-model:17.0.2) and jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)

So error says that there is a duplicate class in these two modules
Module1: com.google.android.gms.dynamite.descriptors.com.google.android.gms.vision.dynamite.face.ModuleDescriptor found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar(com.google.firebase:firebase-ml-vision-face-model:17.0.2)
Module 2:
jetified-play-services-vision-face-contour-internal-16.1.0-runtime.jar (com.google.android.gms:play-services-vision-face-contour-internal:16.1.0)

https://github.com/react-native-camera/react-native-camera/issues/3031#issuecomment-732135774

https://stackoverflow.com/questions/58808875/how-to-resolve-duplicate-class-exception-caused-by-com-google-firebasefirebase

As per the answers I found from Github, We need the exclude one of the groups and module from one of the modules and remember not both of them.

implementation platform('com.google.firebase:firebase-bom:26.1.0'){
exclude group: 'com.google.android.gms'
}

The hardest part for me with an issue I found was identifying which one is a module and which one is a group. That’s why it took me a week to find the solution. If you know which one is module and group then it just works with one shot.
Now we know that out of two modules we need to exclude only one module and not both. Considering the first module out of two.
com.google.android.gms.dynamite.descriptors.com.google.android.gms.vision.dynamite.face.ModuleDescriptor found in modules jetified-firebase-ml-vision-face-model-17.0.2-runtime.jar(com.google.firebase:firebase-ml-vision-face-model:17.0.2)
In this part we need to identify module and the group to exclude,
Now consider only this part com.google.firebase:firebase-ml-vision-face-model:17.0.2
Module: com.google.firebase
Group: firebase-ml-vision-face-model

The rest of the description and version numbers and all just ignore it.
Now go to your android app/build.gradle and exclude this module configuration

That’s it. Re-run your project and you will find this issue anymore

Posted by:
Anand S,
Software Engineer, NIUM India Pvt Ltd