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

Kotlin版本不兼容求助:firebase-analytics-ktx模块编译报错

解决Kotlin版本与Firebase库不兼容问题

问题根源

你使用的com.google.firebase:firebase-bom:32.7.0中包含的firebase-analytics-ktx:21.5.0和play-services-measurement-api:21.5.0是用Kotlin 1.9.0编译的,但项目当前Kotlin版本为1.8.21,二者元数据二进制版本不匹配。直接升级Kotlin到1.9.0后报错加剧,核心原因是Android Gradle Plugin (AGP) 7.4.2与Kotlin 1.9.x存在兼容性冲突,且Gradle版本未同步适配。

分步解决方案

1. 调整Kotlin、AGP、Gradle至兼容组合

选择经过官方验证的稳定兼容版本,无需大幅改动现有配置:

  • Kotlin版本:1.9.20
  • AGP版本:7.4.3(兼容Kotlin 1.9.x,且适配现有Gradle 7.5)
  • Gradle版本:保持7.5(与AGP 7.4.x兼容)

修改android/build.gradle配置:

buildscript {
    ext.kotlin_version = '1.9.20'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.3' // 升级AGP到兼容版本
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.4.0"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.8'
    }
}

2. 清理冗余依赖

app/build.gradle中同时引入了firebase-analytics和firebase-analytics-ktx,后者已包含前者的全部功能,保留ktx版本即可减少冲突:

dependencies {
    implementation platform("com.google.firebase:firebase-bom:32.7.0")
    implementation "com.google.firebase:firebase-analytics-ktx" // 仅保留ktx版本
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.google.firebase:firebase-messaging"
    implementation "com.google.firebase:firebase-crashlytics"
}

3. 同步并重建项目

  • 点击Android Studio工具栏的Sync Project with Gradle Files按钮同步配置
  • 执行Build > Clean Project,再执行Build > Rebuild Project

4. 强制统一Kotlin依赖版本(可选)

若仍存在第三方库Kotlin版本不一致的问题,可在app/build.gradle中添加强制统一规则:

configurations.all {
    resolutionStrategy {
        force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
        force "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    }
}

额外说明

  • Firebase BOM会自动管理所有Firebase库的版本,不要单独指定Firebase库的版本号,避免版本冲突
  • 若后续升级到Kotlin 1.10.x及以上,需同步将AGP升级到8.0+,并将Gradle版本升级到8.0+

内容的提问来源于stack exchange,提问作者Truong Ngoc Tuan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 17:13:15