How to use google/api/annotations.proto with Android

Viewed 1930

I have a problem with proto file in my project I had import in my proto file:

import "google/api/annotations.proto";

I am getting following error when building the project.

Import "google/api/annotations.proto" was not found or had errors.

How can I use this import in my project? Should I add something to my build.gradle?

1 Answers

On non-Android, you could add this dependency to your build.gradle:

compile 'com.google.api.grpc:proto-google-common-protos:1.12.0'

However, Android uses Protobuf "Lite" instead of full Protobuf and there's not a pre-generated library with Lite for this proto. There is an open issue about this.

However, a workaround discussed for the well-known protos can be used here as well. Namely, use a protobuf dependency instead of a compile dependency. This will generate the code as part of your build.

protobuf 'com.google.api.grpc:proto-google-common-protos:1.12.0'

Unfortunately, this solution only really works for applications. If two libraries use this "solution" they must never be included into the same application as they will have duplicated (and potentially have different versions of) the generated classes.

Related