简体   繁体   English

使用IntelliJ中的Gradle导入Protobuf生成的类

[英]Import Protobuf generated classes using Gradle in IntelliJ

I am having trouble importing Protobuf generated classes using Gradle. 我无法使用Gradle导入Protobuf生成的类。

This is how my project tree looks like: 这就是我的项目树的样子:

项目树:

I've tried marking the packages as Source, I have tried all possible combinations of imports: 我已经尝试将包标记为Source,我已经尝试了所有可能的导入组合:

import generated.main.grpc.GreeterGrpc;
import main.java.HelloRequest;
import java.*;
import HelloRequest;

None of them works. 它们都不起作用。 Here is my build.gradle: 这是我的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 {}
        }
    }
}

Before I added 在我加入之前

generateProtoTasks.generatedFilesBaseDir = 'generated'

all of my generated classes would be added to build/generated/main/java 我生成的所有类都将添加到build / generated / main / java中

It may be a little different since I did this in an Android project, but what worked for me was adding classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1" in my root build.gradle and the following in the apps build.gradle. 自从我在Android项目中执行此操作后可能会有所不同,但对我classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1"在我的根build.gradle中添加classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1"以及以下内容应用build.gradle。

Also don't forget to run a gradle build as only running won't generate the classes automatically unless you have the build project automatically option selected in compiler options. 也不要忘了运行gradle这个构建因为只有运行不会自动生成类,除非你有build project automatically 选项在编译器选项中选择。

apply plugin: 'com.google.protobuf'

protobuf {
    generatedFilesBaseDir = "$projectDir/generated"
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.3'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java
            }
            task.plugins {
                grpc {}
            }
        }
    }
}

//Protobuf
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile 'com.google.protobuf:protobuf-java:3.0.0'

//GRPC
compile('io.grpc:grpc-protobuf:1.1.1') {
    exclude module: 'jsr305'
}
compile('io.grpc:grpc-okhttp:1.1.1') {
    exclude module: 'jsr305'
}
compile('io.grpc:grpc-stub:1.1.1') {
    exclude module: 'jsr305'
}

I've also made two basic but working examples using gRPC that may be able to help. 我还使用gRPC制作了两个基本但有效的例子,可以提供帮助。 Example Java project using Maven (if you're open to switching to Maven). 使用Maven的示例Java项目 (如果您打开切换到Maven)。 And a basic Example Android project using gradle although there are minor differences (I'm using java-lite and okhttp instead of netty). 并且使用gradle的基本示例Android项目虽然存在细微差别(我使用java-lite和okhttp而不是netty)。

I had same issues in intellij while in sbt everything worked just fine, its just problem of higlightning it in IDE. 我在intellij中遇到了同样的问题,而在sbt中一切工作都很好,它只是在IDE中突出显示它的问题。 I had to go to 'Project structure'->'Modules'->click on 'root'-> 'Depencencies'->'+' sign at the bottom-> Jars or directories -> add your generated dir as dependency. 我不得不去'项目结构' - >'模块' - >点击'root' - >'Depencencies' - >'+'标志在底部 - >罐子或目录 - >添加你生成的dir作为依赖。

If you want to generate for java, you have to mention a package name in your proto file like this: 如果要为java生成,则必须在proto文件中提及包名称,如下所示:

option java_package = "com.example";

Do that and generate again. 做到这一点并再次生成。

Now import like this: 现在导入如下:

`import com.example.HelloRequest;`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM