"Duplicate class found" error in Android Studio when building project

124 Views Asked by At

After adding the com.google.cloud:google-cloud-language:2.34.0 dependency, I keep getting the Duplicate class found error in the Build Output. Here are all my dependencies in my build.grandle.kts (:app) file:

dependencies {

    val roomVersion = "2.6.1"

    // Jetpack Compose
    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
    implementation("androidx.compose.material:material:1.6.0-rc01")

    // Firebase
    implementation(platform("com.google.firebase:firebase-bom:32.5.0"))
    implementation("com.google.firebase:firebase-auth:22.3.0")
    implementation("com.google.firebase:firebase-auth-ktx")
    implementation("com.google.firebase:firebase-firestore-ktx")
    implementation("androidx.browser:browser:1.7.0")
    implementation("com.google.firebase:firebase-analytics")
    implementation("com.google.firebase:firebase-storage")
    implementation("com.google.firebase:firebase-database")

    // Dagger - Hilt
    implementation("com.google.dagger:hilt-android:2.48")
    kapt("com.google.dagger:hilt-android-compiler:2.48")
    kapt("androidx.hilt:hilt-compiler:1.1.0")
    implementation("androidx.hilt:hilt-navigation-compose:1.1.0")

    // Material Icons
    implementation("androidx.compose.material:material-icons-core:1.5.4")
    implementation("androidx.compose.material:material-icons-extended:1.5.4")

    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.1")

    // Coroutine Lifecycle Scopes
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")

    // Coil
    implementation("io.coil-kt:coil-compose:2.4.0")

    // Retrofit
    implementation("com.squareup.retrofit2:retrofit:2.9.0")

    // OkHttp
    implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2")

    // JSON Converter
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")

    // Google Sign-In
    implementation("com.google.android.gms:play-services-auth:20.7.0")

    // Facebook
    implementation("com.facebook.android:facebook-android-sdk:16.3.0")

    // Admob
    implementation("com.google.android.gms:play-services-ads:22.6.0")

    // RevenueCat
    implementation("com.revenuecat.purchases:purchases:7.0.0")

    // App Compat
    implementation("androidx.appcompat:appcompat:1.6.1")

    // SendGrid
    implementation("com.github.jakebreen:android-sendgrid:1.4.0")

    // Material 3 Components
    implementation("androidx.compose.material3:material3:1.2.0-beta02")

    // Material 3 Navigation
    implementation("androidx.navigation:navigation-compose:2.7.6")

    // Room
    implementation("androidx.room:room-runtime:$roomVersion")
    annotationProcessor("androidx.room:room-compiler:$roomVersion")

    // To use Kotlin annotation processing tool (kapt)
    //noinspection KaptUsageInsteadOfKsp
    kapt("androidx.room:room-compiler:$roomVersion")

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$roomVersion")

    // optional - RxJava2 support for Room
    implementation("androidx.room:room-rxjava2:$roomVersion")

    // optional - RxJava3 support for Room
    implementation("androidx.room:room-rxjava3:$roomVersion")

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation("androidx.room:room-guava:$roomVersion")

    // optional - Test helpers
    testImplementation("androidx.room:room-testing:$roomVersion")

    // optional - Paging 3 Integration
    implementation("androidx.room:room-paging:$roomVersion")

    // DataStore
    implementation("androidx.datastore:datastore-preferences:1.0.0")

    // Google maps
    implementation("com.google.android.gms:play-services-maps:18.2.0")
    implementation("com.google.android.gms:play-services-location:21.0.1")
    implementation("com.google.maps.android:maps-compose-utils:4.3.0")

    // Google places
    implementation("com.google.android.libraries.places:places:3.3.0")

    // Zoomable
    implementation("net.engawapg.lib:zoomable:1.5.1")

    // Lottie
    implementation("com.airbnb.android:lottie-compose:6.0.1")

    // Glide
    implementation("com.github.bumptech.glide:glide:4.16.0")

    // Cloud Natural Language
    implementation("com.google.cloud:google-cloud-language:2.34.0")
    
}

I found that if I include this: exclude(module = "protobuf-java") when implement com.google.cloud:google-cloud-language:2.34.0 the error will be removed. However, if I include it, the Document and AnalyzeSentimentRequest methods below cannot be accessed and there is this error: Cannot access 'com.google.protobuf.GeneratedMessageV3' which is a supertype of 'com.google.cloud.language.v2.Document'. Check your module classpath for missing or conflicting dependencies

fun getSentimentScore(text: String?): Float {

    try {

        LanguageServiceClient.create().use { language ->

            val doc: Document = Document
                .newBuilder()
                .setContent(text)
                .setType(Document.Type.PLAIN_TEXT)
                .build()

            val request: AnalyzeSentimentRequest = AnalyzeSentimentRequest
                .newBuilder()
                .setDocument(doc)
                .build()

            Log.d("SENTIMENT", "THE SENTIMENT OF '$text' IS: ${language.analyzeSentiment(request).documentSentiment.score}")

            return language.analyzeSentiment(request).documentSentiment.score

        } //: LANGUAGE SERVICE CLIENT

    } catch (e: Exception) {

        Log.d("SENTIMENT", "COULD NOT GET SENTIMENT SCORE :${e.message}")
        return 0.0f

    } //: TRY - CATCH

} //: FUN

Any ideas how to fix this problem? Thank you in advance.

1

There are 1 best solutions below

0
On

You can check the dependencies with something like:

./gradlew :app:dependencies

Look for the library that uses protobuf-lite and try excluding that instead.