protobuf lite 和 protobuf java 中的重复类

sah*_*har 15 android timestamp protocol-buffers grpc

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

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'
}
Run Code Online (Sandbox Code Playgroud)

给定的错误是:

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'
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*son 11

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

dependencies {
  ...
  protobuf 'com.google.protobuf:protobuf-java:3.7.1'
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这正是我现在面临的问题。我在我的 lib 模块中使用 protoBuf ,它在调试构建中工作正常,但不知何故,当我运行发布构建时,它失败并显示以下错误消息: AGPBI: {"kind":"error","text":"Program type already present: com.google.protobuf.Any$1","sources":[{}],"tool":"D8"} com.android.builder.dexing.DexArchiveMergerException:合并 dex 存档时出错。有什么办法解决吗? (2认同)

Yas*_*ain 6

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

implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0'
Run Code Online (Sandbox Code Playgroud)

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