Skip to content

Protobuf vs Protobuf-lite collision #2165

Closed

Description

@dmytroreutov

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository.
If you have a general question, need help debugging, or fall into some
other category use one of these other channels:

  • For general technical questions, post a question on StackOverflow
    with the firebase tag.
  • For general Firebase discussion, use the firebase-talk
    google group.
  • For help troubleshooting your application that does not fall under one
    of the above categories, reach out to the personalized
    Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 3.6.3
  • Firebase Component: Firestore
  • Component version: 22.0.0

[REQUIRED] Step 3: Describe the problem

Hello, I've faced a problem when I tried to add Firestore to my app.
My app has gRPC+protobuf(not lite) dependencies for client<->server interaction .
So, I have to exclude all possible sub dependencies of group: 'com.google.protobuf', module: 'protobuf-javalite to avoid duplicated classes.

But with firestore there is another problem, becaiuse it has group: 'com.google.firebase', module: 'protolite-well-known-types' within and from the other side grpc-protobuf has com.google.api.grpc:proto-google-common-protos.

I can exclude protolite-well-known-types from firestore dependency but it leads to runtime crash with such message:

jjava.lang.VerifyError: Verifier rejected class com.google.firestore.v1.Value: void com.google.firestore.v1.Value.mergeGeoPointValue(com.google.type.LatLng) failed to verify: void com.google.firestore.v1.Value.mergeGeoPointValue(com.google.type.LatLng): [0x19] register v4 has type Precise Reference: com.google.type.LatLng but expected Reference: com.google.protobuf.GeneratedMessageLite
     void com.google.firestore.v1.Value.mergeTimestampValue(com.google.protobuf.Timestamp) failed to verify: void com.google.firestore.v1.Value.mergeTimestampValue(com.google.protobuf.Timestamp): [0x19] register v4 has type Precise Reference: com.google.protobuf.Timestamp but expected Reference: com.google.protobuf.GeneratedMessageLite (declaration of 'com.google.firestore.v1.Value' appears in /data/app/com.example.app-zClj9DiMiOtZXvrAL6Wf3A==/base.apk!classes2.dex)

Otherwise, if I exclude com.google.api.grpc:proto-google-common-protos, i still receive messages about duplicate classes (f.e. com.google.protobuf.DescriptorProtos)

Can someone suggest any solution to avoid such collision?
deps.txt

Steps to reproduce:

I have attached output for dependencies task

Relevant Code:

implementation (libraryDependencies.firebaseFirestore)  {
        exclude group: 'com.google.protobuf', module: 'protobuf-javalite' //kek
        exclude group: 'com.google.firebase', module: 'protolite-well-known-types' //kek
    }

Activity

  1. self-assigned this
    on Nov 10, 2020
  2. var-const commented on Nov 11, 2020

    @var-const
    Contributor

    Hi and sorry you're experiencing issues. Unfortunately, Firestore doesn't support mixing Protobuf and Protobuf-lite. Note that exclude won't help in this case because both the dependencies are actively used by the corresponding library. Are you using any other APIs in your app (except for gRPC, which can use Protobuf-lite) that rely on Protobuf?

    If you're using other APIs, one potential workaround might be to call them via a Firebase Function (you could call Firestore via a Function as well, but that would eliminate the real-time benefits). Another option might be to fork the Firestore repo and see if you can make it work if you switch the dependency on Protobuf-lite with regular Protobuf.

    (note: this is similar to #607)

  3. dmytroreutov commented on Nov 11, 2020

    @dmytroreutov
    Author

    Hi @var-const. Thank you for your reply. Seems like I should either migrate anything to protobuf-lite or fork Firestore repo. I'll try one of them or both and reach out with results. One question, do your team ever consider about to get rid of strict protobuf-lite dependency? I remember +- same issues with https://github.com/google/tink which is used as a dependency for androidx:crypto. As I know, they removed lite dependency.

  4. wilhuff commented on Nov 11, 2020

    @wilhuff
    Contributor

    Firestore stores data in its offline storage in protobuf format so we can't just e.g. switch to JSON without losing writes that are in progress.

  5. dmytroreutov commented on Nov 11, 2020

    @dmytroreutov
    Author

    What I'm trying to say that Firestore can use protobuf well known types like com.google.protobuf.Timestamp or com.google.protobuf.LatLng but not depend on particular implementation ( com.google.protobuf.GeneratedMessageLite or com.google.protobuf.GeneratedMessageV3). I don't know is it a big problem, but such solution can help to simplify client usage of Firestore

  6. wilhuff commented on Nov 11, 2020

    @wilhuff
    Contributor

    Firestore doesn't directly depend on these things. Protobuf messages depend on each other in implementation specific ways, so when we depend on a Firestore Value message, that message depends on Timestamp. The verifier error you're seeing is that the protobuf-lite implementation of Value is incompatible with the full implementation of Timestamp.

  7. dmytroreutov commented on Nov 11, 2020

    @dmytroreutov
    Author

    That's was I meant to say. Firestore uses protobuf-lite implementation of Value``. And when I checked the code, I saw that
    Timestamp, LatLng, etc. types are serialised and deserialised using `com.google.protobuf.GeneratedMessageLite`. So I'm trying to figure out if it possible to have full protobuf implemetation of `Value` or have an abstraction which delegates processing of well-known types either as `GeneratedMessageV3` messages or `GeneratedMessageLite`

  8. wilhuff commented on Nov 11, 2020

    @wilhuff
    Contributor

    I doubt Firestore is sensitive to which implementation of the messages it actually uses; you just can't mix and match message implementations for dependent messages.

    Any abstraction that would allow these things to mix would have to be added to the generated message code, which is a significant undertaking.

    Meanwhile, note that this path you're going down only really addresses the first order problem of incompatibility of between a protobuf-javalite Value and a protobuf Timestamp. Once you resolve it there will be other problems because the message classes built into the firebase-firestore AAR depend on protobuf-javalite in other ways.

    You have essentially three options:

    • Migrate everything to protobuf-javalite, in which case you can use Firestore as-is
    • Fork the firebase-android-sdk, change dependencies to the full protobuf compiler and runtime, and rebuild everything that depends on those things. Make sure that when rebuilding protoc regenerates the message classes. Note that while we can answer questions about this, it's on a best effort basis and is formally unsupported by Firebase.
    • Partition your application so that each process only has one protobuf runtime in it. One way to do this would be to put server-style APIs that use the full runtime in a Cloud Function and invoke that from your Android app. It's possible that you could play classloader games as well, but this isn't anything we could help with.

    Note that protobuf-javalite is specifically designed for Android and that after proguarding the resulting binary is much smaller that the full runtime. If you have the choice I'd advise taking the first path and migrating to protobuf-javalite for Android.

    I'm going to close this issue because there really isn't anything we can do about this. Multiple Java protobuf implementations are an unfortunate fact of life in this ecosystem and we have to pick one to ship a self-contained library.

  9. locked and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions