Gradle无法找到Google Protobuf包。

4

我正在尝试将Google protobuf与Netty集成,但是在进行编译时,Gradle首先会下载Google protobuf(至少在第一次尝试时),但在编译期间只会提示:

  /src/main/java/GameMoveOuterClass.java:1536: error: package com.google.protobuf.GeneratedMessageV3 does not exist
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable

这是我的build.gradle文件:

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        mavenCentral()
   }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}

dependencies {
    compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
    compile group: 'com.google.protobuf', name: 'protobuf-java', version: '2.4.1'
}


jar {
    manifest {
        attributes("Main-Class": 'server.Server',
        "Class-Path": configurations.compile.collect { it.getPath() }.join(' '))
    }
}

如果有人知道出了什么问题,请告诉我。谢谢。
4个回答

4

您正在使用版本为2.4.1的protobuf,该版本不包含GeneratedMessageV3

请升级到新版本的protobuf,例如3.0.0,其中包括此类。

dependencies {
    compile group: 'io.netty', name: 'netty-all', version: '4.1.5.Final'
   compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.0.0'
}

2

使用maven中央库高级搜索,搜索com.google.protobuf.GeneratedMessageV3,看起来该类位于com.google.cloud:google-cloud-nio:xxx或者com.trueaccord.scalapb:protobuf-runtime-scala_yyy:zzz。我猜您需要将其中之一添加到您的类路径中。


1
我不熟悉Gradle,但是看起来你正在将新的protobuf生成代码与旧的protobuf库混合使用,这是不支持的。仅最近添加了GeneratedMessageV3类(大约在3.0左右),因此引用该类的新生成代码无法链接到不包含它的旧库。

0
在我的情况下,我的应用模块都应该添加。
implementation 'com.google.protobuf:protobuf-javalite:3.9.1'

即使 app 依赖于 library


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接