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

实现Firebase Auth时Kotlin版本不兼容问题解决求助

问题描述

在Jetpack Compose中基于Firebase Auth实现Google登录时,遇到Kotlin版本不兼容错误:

.gradle/caches/modules-2/files-2.1/androidx.annotation/annotation-jvm/1.6.0/a7257339a052df0f91433cf9651231bbb802b502/annotation-jvm-1.6.0.jar!/META-INF/annotation.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

已尝试将Gradle中Kotlin版本改为1.6.0,但问题未解决。以下是项目级和应用级Gradle配置:

项目级Gradle配置

buildscript {
    ext {
        compose_ui_version = '1.1.1'
        gradle_version = '7.3.0'
        kotlin_version = '1.6.0'
        google_services_version = '4.3.14'
        compose_bom_version = '2022.11.00'
        compose_version = '1.3.2'
        hilt_version = '2.44.2'
        hilt_navigation_compose_version = '1.0.0'
        firebase_bom_version = '31.0.3'
        play_services_version = '1.6.4'
        play_services_auth_version = '20.3.0'
        accompanist_version = '0.27.0'
        coil_compose_version = '2.2.2'
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.44.2"
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级Gradle配置

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"
}

android {
    namespace 'com.example.app'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.app"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

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

    buildTypes {
        release {
            minifyEnabled 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
        viewBinding true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.1.1'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    //AndroidX
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation("androidx.appcompat:appcompat-resources:1.6.1")
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.annotation:annotation:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"


    //compose
    implementation platform("androidx.compose:compose-bom:$compose_bom_version")
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose"
    implementation "androidx.compose.material:material"
    //Navigation Compose
    implementation "androidx.hilt:hilt-navigation-compose:$hilt_navigation_compose_version"

    //material
    implementation 'androidx.compose.material:material:1.4.0'

    //firebase
    implementation platform("com.google.firebase:firebase-bom:$firebase_bom_version")
    implementation "com.google.firebase:firebase-auth-ktx"
    implementation "com.google.firebase:firebase-firestore-ktx"

    //Play Services
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$play_services_version"
    implementation "com.google.android.gms:play-services-auth:$play_services_auth_version"

    //Accompanist
    implementation "com.google.accompanist:accompanist-navigation-animation:$accompanist_version"
    //Coil
    implementation "io.coil-kt:coil-compose:$coil_compose_version"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'


    implementation 'com.google.api-client:google-api-client:2.0.0'
    implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
    implementation 'com.google.apis:google-api-services-gmail:v1-rev20220404-2.0.0'
    implementation("androidx.compose.material3:material3:1.0.1")
    implementation("androidx.compose.material3:material3-window-size-class:1.0.1")


    //Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}
解决方案

1. 统一并升级Kotlin版本

项目级Gradle中ext.kotlin_version和插件版本不一致,且错误提示依赖是用Kotlin 1.8编译的,直接升级到兼容版本:

  • 修改项目级Gradle的ext.kotlin_version为1.8.22(或稳定的1.8.x版本)
  • 同步plugins里的org.jetbrains.kotlin.android版本为1.8.22
  • 更新Compose Compiler版本到匹配Kotlin 1.8的版本:在应用级Gradle的composeOptions中设置kotlinCompilerExtensionVersion = "1.4.3"

2. 排除或替换冲突的annotation依赖

如果不想升级Kotlin,可强制指定兼容版本:

  • 排除传递依赖的annotation-jvm:
implementation('androidx.annotation:annotation:1.6.0') {
    exclude group: 'androidx.annotation', module: 'annotation-jvm'
}
  • 或直接降级annotation版本到兼容Kotlin 1.6的1.5.0:
implementation 'androidx.annotation:annotation:1.5.0'

3. 清理缓存并重构

  • 执行Android Studio的File -> Invalidate Caches... -> Invalidate and Restart
  • 删除项目根目录下的.gradle和build文件夹
  • 运行命令./gradlew clean build

4. 排查依赖传递冲突

用Gradle命令查看依赖树,找到引入高版本annotation-jvm的来源:

./gradlew app:dependencies

针对性排除该传递依赖。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:55:02