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

Gradle升级至8.0.2遇Kotlin版本不兼容问题求助

问题:Gradle升级至8.0.2后出现Kotlin版本不兼容错误

升级Gradle从v7.3.3到v8.0.2后,清理项目时触发以下错误:

/Users/johndoe/.gradle/caches/8.0.2/generated-gradle-jars/gradle-api-8.0.2.jar!/META-INF/configuration-cache.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缓存、重新执行gradle wrapper --gradle-version 8.0.2 --stack-trace,但问题依旧。项目关键信息:

  • Android Studio版本:Electric Eel(兼容Kotlin 1.8.0)
  • 已在项目级build.gradle中配置kotlin_version = '1.8.0'
  • React Native版本:0.68.2

项目级build.gradle代码:

import org.apache.tools.ant.taskdefs.condition.Os

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "31.0.0"
        minSdkVersion = 21
        compileSdkVersion = 31
        targetSdkVersion = 31
        googlePlayServicesAuthVersion = "19.2.0"
        kotlin_version = '1.8.0'

        if (System.properties['os.arch'] == "aarch64") {
            // For M1 Users we need to use the NDK 24 which added support for aarch64
            ndkVersion = "24.0.8215888"
        } else {
            // Otherwise we default to the side-by-side NDK version from AGP.
            ndkVersion = "21.4.7075529"
        }
    }
    repositories {
        google()
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
    }
    dependencies {
        classpath('com.android.tools.build:gradle:7.4.2')
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath('com.google.gms:google-services:4.3.10')
        classpath("de.undercouch:gradle-download-task:4.1.2")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
        google()
        // This is needed or else react-native-video will error out
        jcenter() {
            content {
                includeModule("com.yqritc", "android-scalablevideoview")
            }
        }
        maven { url 'https://www.jitpack.io' }
    }
}
解决方案

1. 升级Android Gradle Plugin(AGP)版本

Gradle 8.0.2要求AGP最低版本为7.4.0,当前使用的7.4.2满足基础要求,但建议升级到与Gradle 8.0.2完全适配的稳定版本,避免版本匹配漏洞:

// 在buildscript dependencies中修改
classpath('com.android.tools.build:gradle:7.4.3')

2. 强制所有模块使用统一Kotlin版本

在项目级build.gradle的allprojects块中添加版本强制规则,防止第三方依赖引入低版本Kotlin:

allprojects {
    repositories {
        // ... 原有仓库配置
    }

    // 添加版本强制配置
    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0"
            force "org.jetbrains.kotlin:kotlin-stdlib:1.8.0"
            force "org.jetbrains.kotlin:kotlin-reflect:1.8.0"
        }
    }
}

3. 彻底清理缓存与构建文件

执行以下命令彻底清理残留的旧版本缓存:

# 清理项目构建产物
./gradlew clean

# 删除Gradle 8.0.2版本缓存
rm -rf ~/.gradle/caches/8.0.2/

# 重新生成Gradle wrapper
./gradlew wrapper --gradle-version 8.0.2

4. 指定React Native Gradle插件版本

React Native 0.68.2的默认gradle-plugin依赖未指定版本,可能存在兼容性问题,需明确匹配RN版本:

// 修改buildscript中的react-native-gradle-plugin依赖
classpath("com.facebook.react:react-native-gradle-plugin:0.68.2")

5. 验证模块级Kotlin配置

检查App模块的build.gradle文件,确保已正确应用Kotlin插件:

// 模块级build.gradle顶部添加插件配置
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

// 或使用旧版写法
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:15:09