Search code examples
javaandroidgradleprotocol-buffers

How can I determine where duplicate class comes from in Android Studio / gradle?


I am trying to optimize my Android app to target API 33 or API 34 For that purpose, I had to update libraries and dependencies for protobuf and grpc. I see that the javalite version of protobuf is now included in the main library and the use of lite version is changed. So I changed my build.gradle accordingly. But I get the following error indicating that a class is duplicate (with different versions ?)

Type com.google.protobuf.Any$1 is defined multiple times: C:\Users\jenny\.gradle\caches\transforms-3\f587bdbbf1ee885f5ee14d4cd9b8bf8b\transformed\jetified-protobuf-javalite-3.22.3.jar:com/google/protobuf/Any$1.class, C:\Development\workspace\android_studio\VectorCTRLPRO\app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class

I made several changes to my build.gradle to exclude some java and/or javalite versions of protobuf but I keep getting the error. I also tried building after invalidating cache.

As you'll see in my build.gradle (below) , I am in fact using 'com.google.protobuf:protoc:3.23.2' but the error mentions version 3.22.3

So, if I can find what library uses 3.22.3 , I can exclude it (maybe ?) and get rid of this issue. How can I find it ?

my build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.jenny.test.app"
        minSdkVersion 20
        targetSdkVersion 33
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true

            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'

        }
    }
    lintOptions {
        disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
        textReport true
        textOutput "stdout"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.7.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'io.grpc:grpc-okhttp:1.56.1'
    implementation ('io.grpc:grpc-protobuf-lite:1.56.1') {
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
    }
    implementation 'io.grpc:grpc-stub:1.56.1'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    protobuf ('com.google.protobuf:protobuf-java:3.23.2') {
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
        exclude group: 'com.google.protobuf', module: 'protobuf-javalite'
    }
    protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
    implementation 'com.google.android.gms:play-services-ads:22.2.0'
    implementation 'org.jcodec:jcodec:0.2.5'
    implementation 'org.jcodec:jcodec-android:0.2.5'
    implementation 'org.jcodec:jcodec-javase:0.2.5'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation 'com.google.android.ads.consent:consent-library:1.0.8'
    implementation 'com.google.android.play:core:1.10.3'
    implementation 'com.google.guava:guava:28.2-android'
    implementation project(path: ':nativetemplates')

    constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
            because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
        }
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
        }
    }
}

configurations.implementation {
    exclude group: 'com.google.guava', module: 'listenablefuture'
}


protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.23.2' }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.56.1' }
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins{
                java { option 'lite' }
            }
            task.plugins {
                grpc { option 'lite' }
            }
        }
    }
}

Solution

  • I had a very similar issue.

    @ejona86 from Github helped me with this issue here: https://github.com/google/protobuf-gradle-plugin/issues/731

    It turns out that the problem is with the old "googleapis-common-protos:0.0.3" artifact. I updated it to version 2.23.0 but it still has issues.

    Then I was advised to manually inject descriptor.proto doing; 1.

    //protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
    protobuf ('com.google.api.grpc:proto-google-common-protos:2.23.0') {
        exclude group: 'com.google.protobuf'
    }
    
    1. add the file to the source: app/src/main/proto/google/protobuf/descriptor.proto

    After these changes, my issue is gone. Thanks to @ejona86