升级现有项目Kotlin版本遇getDestinationDir方法找不到错误求助
KotlinCompile.getDestinationDir()方法 当前环境
- Android Gradle插件版本:
com.android.tools.build:gradle:7.0.1 - 原Kotlin Gradle插件版本:
org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30
问题描述
将Kotlin插件升级到org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20后,出现以下错误:
Unable to find method ''java.io.File
org.jetbrains.kotlin.gradle.tasks.KotlinCompile.getDestinationDir()''
'java.io.File
org.jetbrains.kotlin.gradle.tasks.KotlinCompile.getDestinationDir()'Gradle's dependency cache may be corrupt (this sometimes occurs after
a network connection timeout.)Re-download dependencies and sync project (requires network) The state
of a Gradle build process (daemon) may be corrupt. Stopping all Gradle
daemons may solve this problem.Stop Gradle build processes (requires restart) Your project may be
using a third-party plugin which is not compatible with the other
plugins in the project or the version of Gradle requested by the
project.In the case of corrupt Gradle processes, you can also try closing the
IDE and then killing all Java processes.
已尝试的操作:
- 重新同步Gradle
- 重新下载依赖
- 清理缓存
- 更新所有依赖
解决方案
1. 核心原因:版本兼容性不匹配
Android Gradle插件(AGP)与Kotlin插件存在严格的版本对应关系:
- AGP 7.0.x系列最高兼容Kotlin 1.6.x版本,无法直接支持Kotlin 1.7.x。
- Kotlin 1.7.x需要AGP 7.2及以上版本配合使用。
方案A:升级AGP到兼容版本
- 打开项目根目录的
build.gradle文件,修改AGP和Kotlin插件版本:dependencies { classpath 'com.android.tools.build:gradle:7.2.2' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20' } - 修改
gradle/wrapper/gradle-wrapper.properties中的Gradle版本为7.3.3(AGP 7.2.2对应的配套版本):distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip - 执行终端命令停止所有Gradle守护进程:
- Mac/Linux:
./gradlew --stop - Windows:
gradlew.bat --stop
- Mac/Linux:
- 重新同步项目。
方案B:降级Kotlin插件到兼容版本
如果暂时不想升级AGP,可将Kotlin插件降级到与AGP 7.0.1兼容的最高稳定版本1.6.21:
- 修改项目根目录
build.gradle中的Kotlin插件版本:dependencies { classpath 'com.android.tools.build:gradle:7.0.1' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21' } - 确保所有模块的
build.gradle中使用的Kotlin版本与插件版本一致:kotlin { jvmToolchain(11) version = "1.6.21" } - 执行终端命令停止Gradle守护进程,重新同步项目。
额外检查
确认项目中没有使用与新版Kotlin插件冲突的第三方Gradle插件,若有需同步升级第三方插件版本。
内容的提问来源于stack exchange,提问作者Esperanz0

