当前位置: 首页 > 知识库问答 >
问题:

protobuf lite和protobuf java中的重复类

严元白
2023-03-14

我正在android实现中使用grpc和protobuf lite。但是protobuf lite没有谷歌时间戳,而我的protos有导入“google/protobuf/timestamp.proto”。所以我将实现'com.google.protobuf:protobuf-java:3.7.1'添加到包含google时间戳的gradle中。但在此之后,代码编译会出现错误。例如:在模块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。任何能解决这个问题的想法都将不胜感激。

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'
}

给定的错误为:

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)

共有1个答案

韩鸿波
2023-03-14

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

dependencies {
  ...
  protobuf 'com.google.protobuf:protobuf-java:3.7.1'
}

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

 类似资料: