Grpc cannot resolve symbol GreeterGrpc

Viewed 6264
3 Answers

The Protobuf plugin assumes Protobuf files (*.proto) are organized in the same way as Java source files, in sourceSets. The Protobuf files of a sourceSet are compiled in a single protoc run, and the generated files are added to the input of the Java compilation run of that sourceSet ().

sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

Thanks to @https://medium.com/@DivyaJaisawal/generate-java-code-from-proto-file-using-gradle-1fb9fe64e046

For me, first of all a problem (in Eclipse) was using single quotes around $ variables. My working gradle.build to compile proto files to Java classes in a custom directory was this:

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
    }
    generatedFilesBaseDir = "$projectDir/gensrc"
    generateProtoTasks {
        all()*.plugins { grpc {} }
    }
}

// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
    main {
        java {
            srcDirs "${protobuf.generatedFilesBaseDir}/main/java"
            srcDirs "${protobuf.generatedFilesBaseDir}/main/grpc"
        }
    }
}

Then Eclipse could not tell these were source files. I had to refresh the project:

refresh

Then add the custom files to the build path

build path

Related