升级Kotlin至1.7.10后构建失败,请求技术支持
Kotlin升级至1.7.10后Gradle构建依赖解析失败问题
问题描述
非Android Kotlin项目使用Kotlin 1.6.10时编译正常,切换到1.7.10后出现以下构建错误:
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'jwotd'. > 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 - Variant 'gradle70SourcesElements' 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 sources 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 'javadocElements' 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 release status but the consumer didn't ask for it - Variant 'runtimeElements' 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 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 - Variant 'sourcesElements' 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 sources 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 release status but the consumer didn't ask for it The following variants were also considered but didn't match the requested attributes: - Variant 'apiElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0 declares a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component - Variant 'gradle70ApiElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0 declares a component compatible with Java 8, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component
当前配置
根目录build.gradle:
buildscript { ext.kotlin_version = '1.7.10' repositories { jcenter() mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }
子项目build.gradle示例:
apply plugin: 'org.jetbrains.kotlin.jvm' sourceSets { main { java.srcDirs "src/java" } } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } dependencies { api project(':cjk') implementation project(':kokuban') implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.beust:klaxon:5.0.1' implementation "com.oracle.database.xml:xmlparserv2:21.1.0.0" }
解决方法
1. 升级Gradle版本
Kotlin 1.7.x系列插件要求Gradle版本至少为7.0,旧版Gradle无法正确处理插件的变体属性。修改gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
2. 移除已废弃的JCenter仓库
JCenter已停止维护,所有依赖改用MavenCentral即可,修改根目录build.gradle:
buildscript { ext.kotlin_version = '1.7.10' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }
3. 优化子项目配置(可选)
改用现代的Plugins DSL应用Kotlin插件,并通过BOM统一管理Kotlin版本:
plugins { id 'org.jetbrains.kotlin.jvm' version '1.7.10' } sourceSets { main { java.srcDirs "src/java" } } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } dependencies { api project(':cjk') implementation project(':kokuban') implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7" implementation 'com.beust:klaxon:5.0.1' implementation "com.oracle.database.xml:xmlparserv2:21.1.0.0" }
4. 清理依赖缓存
执行以下命令清理旧缓存并重新构建:
./gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者digory doo
相关产品推荐
相关产品推荐

