如何解决 Android Studio 上的“Type com.google.protobuf.Any$1 is Define multiple times”问题

问题描述 投票:0回答:2

我正在升级我的 Android 应用程序以支持 API 33。 为此,我还升级了 protobuf 和 grpc 库。 如果我直接从 Android Studio 构建并运行到我的手机上,一切都会升级并且应用程序可以运行。 但是,如果我尝试构建用于发布应用程序的捆绑包,则会收到以下错误。

Type com.google.protobuf.Any$1 is defined multiple times: C:\Users\ilker\.gradle\caches\transforms-3\156cd40978d20d5791c06b9e4b80c4fc\transformed\jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class, C:\Development\workspace\android_studio\VectorCTRLPRO\app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class

我的build.gradle(应用程序级别)是这样的:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 125
        versionName "12.5"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
            firebaseCrashlytics {
                mappingFileUploadEnabled true
            }
        }
    }
    lintOptions {
        disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
        textReport true
        textOutput "stdout"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/io.netty.versions.properties'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.7.0-alpha02'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'io.grpc:grpc-okhttp:1.52.1'
    implementation 'io.grpc:grpc-protobuf-lite:1.52.1'
    implementation 'io.grpc:grpc-stub:1.52.1'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.23.2'
    protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
    implementation 'com.google.android.gms:play-services-ads:22.1.0'
    implementation 'com.google.firebase:firebase-analytics:21.3.0'
    implementation 'com.google.firebase:firebase-crashlytics:18.3.7'
    implementation 'com.google.firebase:firebase-messaging:23.1.2'
    implementation ('com.google.firebase:firebase-inappmessaging-display:20.3.2') {
        exclude group: 'com.google.firebase', module: 'protolite-well-known-types'
    }
    implementation 'org.jcodec:jcodec:0.2.5'
    implementation 'org.jcodec:jcodec-android:0.2.5'
    implementation 'org.jcodec:jcodec-javase:0.2.5'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation 'com.google.android.ads.consent:consent-library:1.0.8'
    implementation 'com.google.android.play:core:1.10.3'
    implementation 'com.google.guava:guava:28.2-android'
    implementation project(path: ':nativetemplates')

    constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
            because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
        }
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
        }
    }
}

configurations.implementation {
    exclude group: 'com.google.guava', module: 'listenablefuture'
}


protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.23.2' }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.52.1' }
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins{
                java { option 'lite' }
            }
            task.plugins {
                grpc { option 'lite' }
            }
        }
    }
}

尝试删除以下行,但我得到了同样的错误:

 protobuf 'com.google.protobuf:protobuf-java:3.23.2'
    protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'

gradle创建的依赖列表如下:

https://pastebin.com/raw/PdYSg8bm

android protocol-buffers protoc protobuf-java
2个回答
0
投票

看来您通过

com.google.protobuf:protobuf-java
依赖于
com.google.protobuf:protobuf-javalite
io.grpc:grpc-protobuf-lite

提示在错误消息中,相关类是:

jetified-protobuf-javalite-3.21.7.jar:com/google/protobuf/Any$1.class
app\build\intermediates\javac\release\classes\com\google\protobuf\Any$1.class

请参阅 https://developer.android.com/build/dependency#view-dependency-tree 了解有关如何处理此问题的更多信息。您需要调查一下是什么在拉入多个不同的 protobuf 副本,然后找出如何消除依赖关系图。


0
投票
来自 Github 的

@ejona86 帮助我解决了这个问题: https://github.com/google/protobuf-gradle-plugin/issues/731

事实证明,问题出在旧的“googleapis-common-protos:0.0.3”工件上。我已将其更新到版本 2.23.0 但仍然有问题。

然后建议我手动注入descriptor.proto; 1.

 //protobuf 'com.google.api.grpc:googleapis-common-protos:0.0.3'
    protobuf ('com.google.api.grpc:proto-google-common-protos:2.23.0') {
        exclude group: 'com.google.protobuf'
    }
  1. 将文件添加到源中: 应用程序/src/main/proto/google/protobuf/descriptor.proto

这些更改之后,我的问题就消失了。 感谢@ejona86

© www.soinside.com 2019 - 2024. All rights reserved.