升级Kotlin 1.6.0到1.7.0后无法解析kotlin-gradle-plugin的解决办法
Kotlin 1.7.0升级后Gradle插件依赖解析错误修复方案
将Kotlin版本从1.6.0升级至1.7.0后,项目配置时出现以下错误:
A problem occurred configuring root project 'android'. > Could not resolve all artifacts for configuration ':classpath'. > Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0. Required by: project : > The consumer was configured to find a runtime of a component compatible with Java 8, packaged as a jar, and its dependencies declared externally. However we cannot choose between the following variants of org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0: - gradle70JavadocElements - gradle70RuntimeElements - gradle70SourcesElements - javadocElements - runtimeElements - sourcesElements All of them match the consumer attributes: - Variant 'gradle70JavadocElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0 declares a runtime of a component, and its dependencies declared externally: - Unmatched attributes: - Provides documentation but the consumer didn't ask for it - Provides javadocs but the consumer didn't ask for it - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Provides attribute 'org.gradle.plugin.api-version' with value '7.0' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Variant 'gradle70RuntimeElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0 declares a runtime of a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Unmatched attributes: - Provides a library but the consumer didn't ask for it - Provides attribute 'org.gradle.jvm.environment' with value 'standard-jvm' but the consumer didn't ask for it - Provides attribute 'org.gradle.plugin.api-version' with value '7.0' but the consumer didn't ask for it - Provides release status but the consumer didn't ask for it - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but the consumer didn't ask for it
修复步骤:
升级Gradle版本至7.0及以上
Kotlin 1.7.0的Gradle插件依赖Gradle 7.x版本的特性,旧版本Gradle无法识别插件的gradle70*变体,导致版本选择冲突。修改项目根目录下的gradle/wrapper/gradle-wrapper.properties文件,更新Gradle分发地址:distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip统一Java兼容性配置
在根项目的build.gradle(或build.gradle.kts)中明确指定Java 8的兼容性,避免属性不匹配:
若使用Groovy DSL:plugins { id 'com.android.application' version '7.0.0' apply false id 'org.jetbrains.kotlin.android' version '1.7.0' apply false } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 }若使用Kotlin DSL:
plugins { id("com.android.application") version "7.0.0" apply false id("org.jetbrains.kotlin.android") version "1.7.0" apply false } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 }清理缓存并重新同步项目
执行Gradle命令清理缓存并刷新依赖:./gradlew clean build --refresh-dependencies在Android Studio中,可通过
File -> Sync Project with Gradle Files完成项目同步。
内容的提问来源于stack exchange,提问作者Murat Aktay
相关产品推荐
相关产品推荐

