firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.28k stars 579 forks source link

Android Build Error: Duplicate Class with Protobuf When Adding Firebase In-App Messaging in gradle #5985

Closed ashifali3147 closed 6 months ago

ashifali3147 commented 6 months ago

Environment:

Problem:

I am working on an Android project that uses multiple Firebase libraries, which have been functioning without any issues. However, I recently attempted to implement Firebase In-App Messaging, and this has led to a build failure with an error indicating a "duplicate protobuf class".

Error:

Duplicate class com.google.protobuf.AbstractMessageLite found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder$LimitedInputStream found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.AbstractParser found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.ByteString found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.ByteString$1 found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.ByteString$ByteIterator found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)
Duplicate class com.google.protobuf.ByteString$CodedBuilder found in modules jetified-protobuf-java-2.5.0 (com.google.protobuf:protobuf-java:2.5.0) and jetified-protobuf-javalite-3.22.3 (com.google.protobuf:protobuf-javalite:3.22.3)

Steps to reproduce:

Trying to add this dependency implementation("com.google.firebase:firebase-inappmessaging-display:21.0.0")

Attempted Solutions:

  1. I tried excluding the protobuf classes in my Gradle file:
    implementation("com.google.firebase:firebase-inappmessaging-display:21.0.0") {  
    exclude group: 'com.google.protobuf' 
    }
  2. I incorporated the Firebase Bill of Materials (BOM) to manage versions automatically:
    implementation platform('com.google.firebase:firebase-bom:xx.x.x')
    implementation 'com.google.firebase:firebase-inappmessaging-display'

    Despite these attempts, the problem persists. My project's relevant dependencies are as follows:

    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath 'com.google.gms:google-services:4.3.15'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
        classpath 'com.google.firebase:perf-plugin:1.3.4'  // Performance Monitoring plugin
    }
    dependencies {
    implementation('com.google.firebase:firebase-messaging:22.0.0') {
        exclude group: 'com.google.firebase', module: 'firebase-analytics'
        exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
    }
    implementation 'com.google.firebase:firebase-analytics:20.1.2'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-core:20.1.2'
    implementation 'com.google.firebase:firebase-crashlytics:18.2.1'
    implementation 'com.google.firebase:firebase-iid:21.1.0'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.6'
    implementation 'com.google.firebase:firebase-perf:19.0.1'
    implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
    implementation 'com.google.firebase:firebase-appcheck-playintegrity:17.0.1'
    }
google-oss-bot commented 6 months ago

I found a few problems with this issue:

lehcar09 commented 6 months ago

Hi @ashifali3147, thank you for reaching out. I tried reproducing the issue, however, I'm having difficulty with due to compatibility issues. Could you share an MCVE to help us investigate the issue?

Aside from that, I noticed that you're not using the latest versions on Firebase products. Could you update to the latest version and see if the issue persists?

ashifali3147 commented 6 months ago

I already tried to update them with the Firebase BOM. Everything is working fine. But this issue still exists with in-app messaging. Also, previously, I was using Firebase PERF, and it's working with no issue, but after using BOM, I am getting the same protobuf duplicate class issue with PERF as well. 

ashifali3147 commented 6 months ago

IMG_20240522_232154.png

After adding In-App Messaging I am getting this conflicts.

lehcar09 commented 6 months ago

Thanks for the additional information. Based on the screenshot you shared, it looks to me that you also have both protobuf-java:2.5.0 and protobuf-javalite:3.14.0 dependecy. There is a known issue that you can't mix regular protobuf and proto-lite in the same app. You can also check this StackOverflow post.

Firebase uses protobuf-lite. That said, we recommend using the protobuf-lite to avoid dependency issue.

Here’s my updated app/build.gradle.kts file where I did not encounter any issue.

   implementation("io.grpc:grpc-protobuf-lite:1.57.2")
//    implementation("com.google.protobuf:protobuf-java:2.5.0")
   implementation("com.google.protobuf:protobuf-javalite:3.14.0")

   implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
   implementation("com.google.firebase:firebase-inappmessaging-display")

I'll go ahead and close this issue now. Let me know if there's any misunderstanding or we need to re-open for further investigation. Thanks!