You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Android Studio中com.google.protobuf.Any$1重复定义问题

问题

将Android应用升级以支持API 33,同时升级了protobuf和grpc库。直接从Android Studio构建并运行到手机时应用正常,但尝试构建发布用的App Bundle时,出现类重复定义错误:

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

当前app级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'两行依赖,但错误仍存在。

解决方法

错误核心是项目同时引入了protobuf-javalite库和编译生成的普通protobuf类,导致类重复定义,按以下步骤修复:

  1. 全局排除非lite版本的protobuf依赖
    在build.gradle中添加全局配置,避免任何依赖引入非lite的protobuf-java:

    configurations.all {
        exclude group: 'com.google.protobuf', module: 'protobuf-java'
        // 保留原有的guava排除
        exclude group: 'com.google.guava', module: 'listenablefuture'
    }
    

    替换原来单独的configurations.implementation配置。

  2. 强制统一protobuf-javalite版本
    grpc-protobuf-lite 1.52.1依赖的是protobuf-javalite 3.21.7,在dependencies的constraints中添加版本约束,避免版本不一致:

    dependencies {
        // 其他依赖保持不变...
        constraints {
            // 新增版本约束
            implementation("com.google.protobuf:protobuf-javalite:3.21.7") {
                because "grpc-protobuf-lite 1.52.1依赖此版本,避免版本冲突"
            }
            // 原有的kotlin约束保持不变...
        }
    }
    
  3. 使用lite版本的公共proto库(可选)
    如果需要使用googleapis-common-protos,替换为lite版本:

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

    不需要则直接移除该行。

  4. 清理缓存并重新构建
    执行Gradle命令清理缓存后再构建App Bundle:

    ./gradlew clean
    ./gradlew bundleRelease
    

内容的提问来源于stack exchange,提问作者yasin tavukcuoglu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:38:08