Import Protobuf generated classes using Gradle in

2019-06-24 17:41发布

问题:

I am having trouble importing Protobuf generated classes using Gradle.

This is how my project tree looks like:

I've tried marking the packages as Source, I have tried all possible combinations of imports:

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

None of them works. Here is my 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

回答1:

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.

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.

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. Example Java project using Maven (if you're open to switching to Maven). And a basic Example Android project using gradle although there are minor differences (I'm using java-lite and okhttp instead of netty).



回答2:

I had same issues in intellij while in sbt everything worked just fine, its just problem of higlightning it in 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.



回答3:

If you want to generate for java, you have to mention a package name in your proto file like this:

option java_package = "com.example";

Do that and generate again.

Now import like this:

`import com.example.HelloRequest;`