21

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)
stkent
  • 19,772
  • 14
  • 85
  • 111
sahar
  • 323
  • 1
  • 2
  • 5

3 Answers3

17

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.

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • 3
    Thats exactly the problem Im facing now. Im using protoBuf in my lib module and it is working fine with debug build but somehow when I run release build it fails with this error message: AGPBI: {"kind":"error","text":"Program type already present: com.google.protobuf.Any$1","sources":[{}],"tool":"D8"} com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives. Any solution to it? – Panda World Feb 27 '20 at 22:06
  • @PandaWorld I am facing the same issue like yours. Did you find the solution? – Putra Nov 14 '20 at 12:32
  • This helped me move further but did not resolve the issue. Now I'm getting "Type com.google.protobuf.Any$1 is defined multiple times: /Users/windmaple/Desktop/demo/app/build/intermediates/project_dex_archive/debug/out/com/google/protobuf/Any$1.dex, /Users/windmaple/Desktop/demo/app/build/intermediates/external_libs_dex/debug/mergeExtDexDebug/classes.dex". Not sure how to get through this .... – Wei Wei Jan 13 '22 at 08:18
6

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.

Yash Jain
  • 442
  • 7
  • 19
  • 1
    I believe this is happening because `com.google.truth:truth:0.44` depends on `com.google.protobuf:protobuf-lite:3.0.1`, but `com.google.firebase:firebase-config:19.2.0` depends on `com.google.protobuf:protobuf-javalite:3.1.0`. I'm trying to figure out if I can substitute the `truth` dependency. – Heath Borders Aug 27 '20 at 19:35
  • Thx Yash, downgrading the firestore version worked for me. I was working with Bill of Materials version 25.9.0 and it didn't help either. I dropped the BOM and went for the individual Firebase artifacts and for firestore I used version 21.4.2 and it is working fine. I also noted the new version of firestore 21.6.0 is also introducing the bug – Tonnie Sep 06 '20 at 15:34
0

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.

zakiblacki
  • 187
  • 1
  • 10