sahar
sahar

Reputation: 333

Duplicate class in protobuf lite and protobuf java

I'm using grpc with protobuf lite in android implementation. but protobuf lite doesn't have google time stamp, and my protos has import "google/protobuf/timestamp.proto". so i added implementation 'com.google.protobuf:protobuf-java:3.7.1' to gradle that contains google time stamp. but after that code compilaition has errors. such as :Duplicate class com.google.protobuf.AbstractMessageLite found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1). any idea to fix this would be appreciated.

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            proto {
                srcDir 'src/main'
            }
            java {
                srcDir 'src/main'
            }
        }
    }
}

protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.7.1' }
    plugins {
        javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.20.0' // CURRENT_GRPC_VERSION
        }
    }

    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc { // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.0.0'

    // You need to build grpc-java to obtain these libraries below.
    implementation 'io.grpc:grpc-okhttp:1.20.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.22.1'
    implementation 'io.grpc:grpc-stub:1.20.0'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'com.google.protobuf:protobuf-java:3.7.1'
}

given error is:

Duplicate class com.google.protobuf.AbstractMessageLite found in modules      protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and 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 protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractParser found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractProtobufList found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.BooleanArrayList found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteBufferWriter found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteOutput found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteString found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteString$1 found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)

Upvotes: 22

Views: 19546

Answers (3)

zakiblacki
zakiblacki

Reputation: 233

In my case this happened because of mixed package dependencies specifically barcode_scan

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.

Upvotes: 0

Yash Jain
Yash Jain

Reputation: 461

It happened to me because I added this dependency:

implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0'

There was a problem with the latest version of firestore. Use version 21.4.2 instead of 21.5.0 as of August 2020.

Upvotes: 7

Eric Anderson
Eric Anderson

Reputation: 26414

The missing classes is a known issue. Full proto and lite proto can't be mixed; they use different generated code. Do not depend on protobuf-java as an implementation dependency, but as a protobuf dependency which will cause gradle-protobuf-plugin to generate code for the .protos.

dependencies {
  ...
  protobuf 'com.google.protobuf:protobuf-java:3.7.1'
}

Note that this solution only really works for an application. If you are a library, it is dangerous because users of your library may then see multiple copied of the generated code for the well-known protos.

Upvotes: 17

Related Questions