square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.04k stars 7.3k forks source link

duplicate class when using recommended protobuf-lite #2954

Open beigirad opened 5 years ago

beigirad commented 5 years ago

In protobuf documents it's recommended to use protobuf-lite instead of protobuf

Android projects: no default output will be added. Since Protobuf 3.0.0, protobuf-lite is the recommended Protobuf library for Android, and you will need to add it as a codegen plugin.

But protobuf-convertor has a dependency for protobuf and when we add protobuf-lite to project, in building time occurred this duplicate error:

AGPBI: {"kind":"error","text":"Program type already present: com.google.protobuf.AbstractMessageLite","sources":[{}],"tool":"D8"}
:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: /Users/farhad/Desktop/prototest/app/build/intermediates/transforms/dexBuilder/debug/0.jar, /Users/farhad/Desktop/prototest/app/build/intermediates/transforms/dexBuilder/debug/1.jar,....
  Program type already present: com.google.protobuf.AbstractMessageLite
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.

and we should exclude default protobuf dependency from protobuf-convertor like this:

    implementation ('com.squareup.retrofit2:converter-protobuf:2.3.0'){
        exclude group: 'com.google.protobuf'
    }

In my opinion it's a bug and requires description to prevent confusion among users.

JakeWharton commented 5 years ago

it's recommended to use protobuf-lite instead of protobuf

This is only true for Android and not the JVM. Our supplied protobuf converter works for both. We can add once sentence to the README for this, but it's not a bug.

beigirad commented 5 years ago

Yes. That's required for android developers.

JakeWharton commented 5 years ago

It's only recommended, not required. Regular protobuf works fine as well.

quibi-jlk commented 4 years ago

I'd like to note that while regular protobuf does work fine, you'll end up hitting duplicate class errors because of this when using other Android libraries that prefer protobuf-lite (we hit this with the Chromecast play services framework dependency). It would definitely be good to update the docs on this one.

knpwrs commented 4 years ago

As a developer who is newer to Android running into this issue was extremely confusing. Here is where I ended up:

    implementation('com.squareup.retrofit2:converter-protobuf:2.9.0') {
      exclude group: 'com.google.protobuf'
    }