作者:华仔很忙 | 来源:互联网 | 2023-02-03 13:39
我在使用Gradle导入Protobuf生成的类时遇到问题。
这是我的项目树的样子:
我尝试将软件包标记为Source,尝试了所有可能的导入组合:
import generated.main.grpc.GreeterGrpc;
import main.java.HelloRequest;
import java.*;
import HelloRequest;
它们都不起作用。这是我的build.gradle:
group 'andu'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
sourceCompatibility = 1.5
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'io.grpc:grpc-protobuf:1.0.0-pre2'
compile 'com.google.protobuf:protobuf-java:3.0.0'
compile 'io.grpc:grpc-stub:1.0.0-pre2'
compile 'io.grpc:grpc-netty:1.3.0'
compile 'io.grpc:grpc-protobuf:1.3.0'
compile 'io.grpc:grpc-stub:1.3.0'
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
srcDirs = ['src/main/java', 'generated/main/java']
}
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.3.0'
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated'
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
在我添加之前
generateProtoTasks.generatedFilesBaseDir = 'generated'
我所有生成的类都将添加到build / Generated / main / java中