Kotlin 版本不匹配:Android 项目中使用 Kotlin 1.8.0 编译的依赖项,预计为 1.6.0

问题描述 投票:0回答:1

创建屏幕以在地图上显示用户位置后,出现以下错误:

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/fragment-1.7.1-api.jar!/META-INF/fragment_release.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.

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/jetified-activity-1.8.1-api.jar!/META-INF/activity_release.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.

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: 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.

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/lifecycle-livedata-2.7.0-api.jar!/META-INF/lifecycle-livedata_release.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.

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/lifecycle-viewmodel-2.7.0-api.jar!/META-INF/lifecycle-viewmodel_release.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.

e: /path/to/project/.gradle/caches/transforms-3/XXXXXX/transformed/core-ktx-1.13.1-api.jar!/META-INF/core-ktx_release.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.

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

设置.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"
android flutter kotlin dart sdk
1个回答
0
投票

首先,我猜这是一个警告而不是错误?

因此错误表明您的 kotlin 版本与预期版本不同。根据你的gradle,你的gradle版本是8.1.1,你的kotlin版本是1.9.0。基于 Gradle 兼容性矩阵,Gradle 8.3 版本支持 Kotlin 1.9.0。

您应该检查兼容性矩阵以同步 Gradle、Kotlin 和 Android Gradle 插件 (AGP) 之间的版本。

所以你在这里可以做的是(2个选项):

1.升级你的 Gradle

  • 查找
    android/gradle-wrapper.properties
    文件
  • distributionUrl
    更改为
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
  • 然后保存。

要升级你的 gradle,你需要检查与你安装的 gradle 兼容的 Java 版本

2.降级 Kotlin 版本

如果您已经执行了选项 1,则无需执行选项 2。

  • 寻找
    android/settings.gradle
  • plugins
    部分中的 kotlin 版本更改为
id "org.jetbrains.kotlin.android" version "1.8.10" apply false

您之前的 kotlin 版本是 1.9.0,然后我将其更改为 1.8.10 以与您的 Gradle 版本(8.1.1)兼容

© www.soinside.com 2019 - 2024. All rights reserved.