Hi,
I'm trying to use grpc-java in an Android app to hit an API that makes uses of protobufs. It seems when using java-lite and then adding this line to a .proto file import google/protobuf/empty.proto, I'm receiving this error:
ERROR: Cause: protoc: stdout: . stderr: google/protobuf/empty.proto: File not found.
helloworld.proto: Import "google/protobuf/empty.proto" was not found or had errors.
Full .proto file:
// Copyright 2015, gRPC Authors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
import "google/protobuf/empty.proto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
App level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 29
defaultConfig {
applicationId "io.grpc.android.helloworldexample"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'InvalidPackage', 'HardcodedText'
textReport true
textOutput "stdout"
}
}
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.25.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
// You need to build grpc-java to obtain these libraries below.
implementation 'io.grpc:grpc-okhttp:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-protobuf-lite:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-stub:1.25.0' // CURRENT_GRPC_VERSION
implementation 'javax.annotation:javax.annotation-api:1.2'
}
Project level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.8"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenLocal()
}
}
Any help would be greatly appreciated!
~there was a similar question in stackoverflow answered by grpc java maintainer.
basically empty.proto is not provided and you may need to copy the definition.~
hmm actually the previous answer doesn't seem correct. the empty.proto should be included as it included as resources in the protobuf library. possibly bug from the protobuf-gradle-plugin. i'll take a look into it
the empty.proto definition(proto file) is not in the com:google.protobuf:protobuf-lite. you need to add empty.proto definition, in app/build.gradle
protobuf 'com.google.protobuf:protobuf-java:3.10.0'
btw, protobuf team fixed this issue in new protobuf-javalite artifact which will be part of grpc 1.26.0. so, in newer version of grpc (will be release in couple weeks), this problem will be gone.
the
empty.protodefinition(proto file) is not in thecom:google.protobuf:protobuf-lite. you need to addempty.protodefinition, inapp/build.gradle
protobuf 'com.google.protobuf:protobuf-java:3.10.0'btw, protobuf team fixed this issue in new
protobuf-javaliteartifact which will be part of grpc 1.26.0. so, in newer version of grpc (will be release in couple weeks), this problem will be gone.
Thanks for the help! I look forward to this update 馃檪