'Updated firebase dependency and got duplicated protobuf classes error

I'm getting this error after updating one of my firebase SDKs

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class com.google.protobuf.AbstractMessageLite found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.AbstractMessageLite$Builder$LimitedInputStream found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.AbstractParser found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.AbstractProtobufList found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.BooleanArrayList found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
  Duplicate class com.google.protobuf.ByteBufferWriter found in modules jetified-protobuf-javalite-3.11.0.jar (com.google.protobuf:protobuf-javalite:3.11.0) and jetified-protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
...

My app now depends on protobuf-lite and protobuf-javalite, which triggers the duplicate class error.



Solution 1:[1]

Recently, Firebase Android has migrated from depending on the old protobuf-lite to depend on protobuf-javalite (github issue | PR implementing the change).

You need to update all the firebase SDKs affected by this change together, otherwise the old SDKs will pull the wrong dependency into your app.

Here is the list of affected SDKs and their versions supporting protobuf-javalite

- com.google.firebase:firebase-firestore:21.5.0
- com.google.firebase:firebase-inappmessaging:19.1.0
- com.google.firebase:firebase-inappmessaging-display:19.1.0
- com.google.firebase:firebase-config:19.2.0
- com.google.firebase:firebase-abt:19.1.0
- com.google.firebase:firebase-perf:19.0.8

EDIT: Added firebase-perf to the list, as it transitively depends on config and also needs to be updated.

Solution 2:[2]

In my case the problem was androidx.security:security-crypto:1.0.0-alpha02 library that depends on protobuf-lite which conflicts with protobuf-java-lite being used by Firebase. Updating this library helped.

You can check if other libraries depend on conflicting protobuf-lite by calling ./gradlew app:dependencies in Terminal. Then cmd+f and search for proto.

Solution 3:[3]

Solution 4:[4]

This error means that you are importing two packages that use Protobuf for your project, one of them has a distribution that's conflicting with the other.
If you encountered this problem on Flutter, you can reconsider the version number of your dependencies in pubspec.yaml, and replace "any" by an exact version number.
For example, under "dependencies:" change:

barcode_scan: any

To:

barcode_scan: ^2.0.0

I hope I helped.

Solution 5:[5]

try to degrade

implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.google.firebase:firebase-config:19.1.4'

It works with

implementation 'androidx.mediarouter:mediarouter:1.1.0'
implementation 'com.google.android.gms:play-services-cast-framework:18.1.0'

Solution 6:[6]

For me I just downgraded my com.google.firebase:firebase-firestore:21.5.0 -> com.google.firebase:firebase-firestore:21.4.3

Solution 7:[7]

I solved this problem by manually searching in the Gradle dependency tree and updating the problematic SDK afterward.

For Android, use this line

gradle app:dependencies > dependency_tree.txt

or if you have a gradle wrapper:

./gradlew app:dependencies > dependency_tree.txt

where app is your project module.

Then manually search for proto for problematic SDK within the dependency_tree.txt file.

Solution 8:[8]

The easiest solution just add the boom library for firebase and remove library version for libraries it will resolve the library conflicts

 // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:29.3.1')

 // When using the BoM, you don't specify versions in Firebase library dependencies

  // Declare the dependency for the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Marcin Adamczewski
Solution 3
Solution 4 Nejmeddine Jaafar
Solution 5 Yashovardhan99
Solution 6 Gilbert Parreno
Solution 7 matox
Solution 8 Adnan Naeem