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

Android Kotlin构建gRPC时出现重复类com.google.protobuf.AbstractMessageLite错误

Android Kotlin + gRPC HelloWorld构建错误排查

问题描述

刚入门Android Kotlin + gRPC技术,尝试构建简单HelloWorld应用时,遇到如下重复类构建错误:

Caused by: java.lang.RuntimeException: Duplicate class com.google.protobuf.AbstractMessageLite found in modules protobuf-java-3.25.3.jar -> protobuf-java-3.25.3 (com.google.protobuf:protobuf-java:3.25.3) and protobuf-javalite-3.25.5.jar -> protobuf-javalite-3.25.5 (com.google.protobuf:protobuf-javalite:3.25.5)

app模块build.gradle配置

import com.google.protobuf.gradle.*

plugins {
    id("com.android.application")
    id("com.google.protobuf")
    id("org.jetbrains.kotlin.android")
}

android {
    compileSdk = 35

    defaultConfig {
        minSdk = 24
        targetSdk = 35
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.13"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:4.29.0"
    }
    plugins {
        id("java") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.69.0"
        }
        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.69.0"
        }
        id("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.1:jdk8@jar"
        }
    }
    generateProtoTasks {
        all().forEach {
            it.plugins {
                id("java") {
                    option("lite")
                }
                id("grpc") {
                    option("lite")
                }
                id("grpckt") {
                    option("lite")
                }
            }
            it.builtins {
                id("kotlin") {
                    option("lite")
                }
            }
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
    implementation("androidx.activity:activity-compose:1.9.3")
    implementation(platform("androidx.compose:compose-bom:2024.12.01"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
    implementation("com.google.protobuf:protobuf-kotlin:3.25.3")
    implementation("io.grpc:grpc-stub:1.69.0")
    implementation("io.grpc:grpc-protobuf:1.62.2")
    implementation("io.grpc:grpc-okhttp:1.69.0")

    implementation("com.google.protobuf:protobuf-kotlin:3.25.3")
    implementation("io.grpc:grpc-kotlin-stub:1.4.1")
    implementation("io.grpc:grpc-protobuf-lite:1.69.0")

    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2024.12.01"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}

项目根目录build.gradle配置

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id("com.android.application") version "8.7.3" apply false
    id("com.google.protobuf") version "0.9.4" apply false
    id("org.jetbrains.kotlin.android") version "1.9.23" apply false
}

buildscript {
    dependencies {
        classpath("com.google.protobuf:protobuf-gradle-plugin:0.9.4")
    }
}

已参考相关资料但问题未解决。


解决方案

错误原因分析

  1. 同时引入了grpc-protobuf(依赖完整版protobuf-java)和grpc-protobuf-lite(依赖精简版protobuf-javalite),二者包含重复的Protobuf核心类
  2. Protobuf版本不一致(3.25.3和3.25.5)加剧冲突
  3. 依赖中重复声明了com.google.protobuf:protobuf-kotlin:3.25.3

具体修改步骤

  1. 清理冲突依赖
    移除完整版grpc-protobuf依赖,只保留lite版本;删除重复的protobuf-kotlin声明,改用lite版依赖:

    dependencies {
        // 保留Android Compose相关依赖
        implementation("androidx.core:core-ktx:1.15.0")
        implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
        implementation("androidx.activity:activity-compose:1.9.3")
        implementation(platform("androidx.compose:compose-bom:2024.12.01"))
        implementation("androidx.compose.ui:ui")
        implementation("androidx.compose.ui:ui-graphics")
        implementation("androidx.compose.ui:ui-tooling-preview")
        implementation("androidx.compose.material3:material3")
    
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
        // 替换为lite版本并统一版本号
        implementation("com.google.protobuf:protobuf-kotlin-lite:3.25.5")
        implementation("io.grpc:grpc-stub:1.69.0")
        implementation("io.grpc:grpc-okhttp:1.69.0")
        implementation("io.grpc:grpc-kotlin-stub:1.4.1")
        implementation("io.grpc:grpc-protobuf-lite:1.69.0")
    
        // 保留测试相关依赖
        testImplementation("junit:junit:4.13.2")
        androidTestImplementation("androidx.test.ext:junit:1.2.1")
        androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
        androidTestImplementation(platform("androidx.compose:compose-bom:2024.12.01"))
        androidTestImplementation("androidx.compose.ui:ui-test-junit4")
        debugImplementation("androidx.compose.ui:ui-tooling")
        debugImplementation("androidx.compose.ui:ui-test-manifest")
    }
    
  2. 统一Protobuf版本
    将protoc版本与依赖版本对齐,修改protobuf插件配置:

    protobuf {
        protoc {
            artifact = "com.google.protobuf:protoc:3.25.5"
        }
        plugins {
            id("grpc") {
                artifact = "io.grpc:protoc-gen-grpc-java:1.69.0"
            }
            id("grpckt") {
                artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.1:jdk8@jar"
            }
        }
        generateProtoTasks {
            all().forEach {
                it.plugins {
                    id("grpc") {
                        option("lite")
                    }
                    id("grpckt") {
                        option("lite")
                    }
                }
                it.builtins {
                    id("kotlin") {
                        option("lite")
                    }
                }
                // 移除java插件,避免生成重复Java代码
                it.plugins.remove(it.plugins.findByName("java"))
            }
        }
    }
    
  3. 同步并清理项目

    • 点击Android Studio的「Sync Project with Gradle Files」按钮
    • 执行命令./gradlew clean build清理缓存并重新构建

内容的提问来源于stack exchange,提问作者Prashant tiwari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 08:52:37