Search code examples
javaproguardprotobuf-java

How to use protobuf with proguard


After adding protobuf dependency to a project that is using proguard for releases I run into the following issues:

Warning: ***.***.Api: can't find referenced method 'com.google.protobuf.Descriptors$FileDescriptor getDescriptor()' in library class com.google.protobuf.AnyProto
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any$Builder toBuilder()' in library class com.google.protobuf.Any
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any$Builder mergeFrom(com.google.protobuf.Any)' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information: can't find referenced method 'com.google.protobuf.Any buildPartial()' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any build()' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any$Builder mergeFrom(com.google.protobuf.Any)' in library class com.google.protobuf.Any$Builder
Warning: ***.***.Api$Information$Builder: can't find referenced method 'com.google.protobuf.Any buildPartial()' in library class com.google.protobuf.Any$Builder
Warning: ***.***Service: can't find referenced method 'com.google.protobuf.Any pack(com.google.protobuf.Message)' in library class com.google.protobuf.Any

Solution

  • In my case the problem was that another dependency already had a transitive dependency on protobuf-javalite and it conflicted with the protobuf-java I was adding.

    As per https://github.com/protocolbuffers/protobuf/issues/8104#issuecomment-887076083:

    protobuf-java and protobuf-javalite should never be included in the same project [...] if you're using Gradle, we recommend an approach like this: https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html#sec:excluding-transitive-deps

    Telling the system to exclude protobuf-javalite because you explicitly depend on protobuf-java is technically OK, but this might have negative side effects to performance or code size on mobile platforms, it also might be totally fine for you, your mileage may vary.

    The problem was solved for me after excluding protobuf-javalite like this

    configurations {
        all*.exclude group: 'com.google.protobuf', module: 'protobuf-javalite'
    }