首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >protobuf lite和protobuf java中的类重复

protobuf lite和protobuf java中的类重复
EN

Stack Overflow用户
提问于 2019-07-13 21:03:12
回答 3查看 10.5K关注 0票数 16

我在android实现中使用了grpc和protobuf lite。但是protobuf lite没有google时间戳,而我的protos有导入“google/protobuf/ time p.proto”。因此,我在包含google时间戳的gradle中添加了实现'com.google.protobuf:protobuf-java:3.7.1‘。但在此之后,代码编译会出现错误。例如:在模块protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1)和protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)中发现重复类com.google.protobuf.AbstractMessageLite。任何解决这个问题的想法都将不胜感激。

代码语言:javascript
复制
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            proto {
                srcDir 'src/main'
            }
            java {
                srcDir 'src/main'
            }
        }
    }
}

protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.7.1' }
    plugins {
        javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.20.0' // CURRENT_GRPC_VERSION
        }
    }

    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc { // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.0.0'

    // You need to build grpc-java to obtain these libraries below.
    implementation 'io.grpc:grpc-okhttp:1.20.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.22.1'
    implementation 'io.grpc:grpc-stub:1.20.0'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'com.google.protobuf:protobuf-java:3.7.1'
}

给定的错误为:

代码语言:javascript
复制
Duplicate class com.google.protobuf.AbstractMessageLite found in modules      protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractMessageLite$Builder$LimitedInputStream found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractParser found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.AbstractProtobufList found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.BooleanArrayList found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteBufferWriter found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteOutput found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteString found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
Duplicate class com.google.protobuf.ByteString$1 found in modules protobuf-java-3.7.1.jar (com.google.protobuf:protobuf-java:3.7.1) and protobuf-lite-3.0.1.jar (com.google.protobuf:protobuf-lite:3.0.1)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-16 07:29:43

缺少的类是一个known issue。Full proto和lite proto不能混合;它们使用不同的生成代码。不要依赖protobuf-java作为implementation依赖项,而是protobuf依赖项,这将导致gradle-protobuf- .protos为插件生成代码。

代码语言:javascript
复制
dependencies {
  ...
  protobuf 'com.google.protobuf:protobuf-java:3.7.1'
}

请注意,此解决方案仅对应用程序有效。如果你是一个库,这是危险的,因为你的库的用户可能会看到为众所周知的protos生成的代码的多个副本。

票数 13
EN

Stack Overflow用户

发布于 2020-08-27 13:02:59

这发生在我身上,因为我添加了这个依赖:

代码语言:javascript
复制
implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0'

最新版本的firestore出现问题。从2020年8月起使用版本21.4.2而不是21.5.0。

票数 4
EN

Stack Overflow用户

发布于 2021-07-26 13:50:40

在我的例子中,发生这种情况是因为混合的包依赖关系,特别是barcode_scan

这个错误意味着你正在为你的项目导入两个使用Protobuf的包,其中一个的发行版与另一个冲突。

如果您在Flutter上遇到此问题,可以重新考虑pubspec.yaml中的从属关系的版本号,并用精确的版本号替换"any“。例如,在“依赖关系:”下更改:

代码语言:javascript
复制
barcode_scan: any
To:

barcode_scan: ^2.0.0

我希望我能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57019439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档