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

Android项目Kotlin版本不匹配:依赖编译用1.8.0,预期1.6.0

解决Kotlin版本不兼容问题

创建显示用户地图位置的页面后,出现以下Kotlin版本不兼容错误:

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/fragment-1.7.1-api.jar!/META-INF/fragment_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/jetified-activity-1.8.1-api.jar!/META-INF/activity_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/jetified-lifecycle-livedata-core-ktx-2.7.0-api.jar!/META-INF/lifecycle-livedata-core-ktx_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/lifecycle-livedata-2.7.0-api.jar!/META-INF/lifecycle-livedata_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/lifecycle-viewmodel-2.7.0-api.jar!/META-INF/lifecycle-viewmodel_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/core-ktx-1.13.1-api.jar!/META-INF/core-ktx_release.kotlin_module: 模块使用不兼容的Kotlin版本编译。其元数据的二进制版本为1.8.0,预期版本为1.6.0。

项目配置文件

Android/build.gradle

buildscript {
    ext.kotlin_version = '1.9.0'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

Android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.projectexample"
    compileSdk = 34

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    defaultConfig {
        applicationId = "com.example.projectexample"
        minSdk = 21
        targetSdk = 34
        versionCode = 1
        versionName = "1.0.0"
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            signingConfig signingConfigs.debug
        }

        debug {
            minifyEnabled false
            shrinkResources false
        }
    }
}

flutter {
    source = "../../"
}

dependencies {
    implementation "androidx.core:core-ktx:1.13.0"
    implementation "androidx.fragment:fragment-ktx:1.7.1"
    implementation "androidx.lifecycle:lifecycle-livedata-core-ktx:2.7.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1"
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip

Settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.9.0" apply false
}

include ":app"

解决方案

1. 统一Android Gradle Plugin版本

Settings.gradle中com.android.application版本为7.3.0,但Android/build.gradle中是8.0.2,版本不一致会引发依赖解析异常。修改Settings.gradle中的版本,与build.gradle保持一致:

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.0.2" apply false // 改为8.0.2
    id "org.jetbrains.kotlin.android" version "1.9.0" apply false
}

2. 清理Gradle缓存

缓存中的旧依赖可能导致版本冲突,执行以下操作:

  • 在项目根目录运行命令:./gradlew clean
  • 删除项目下的.gradle/caches文件夹
  • 重新同步项目并构建

3. 验证Flutter环境兼容性

作为Flutter项目,运行flutter doctor检查环境,确保Flutter SDK版本与Android Gradle Plugin、Kotlin版本匹配,必要时升级Flutter到稳定版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 11:07:06