【问题标题】:Cannot use Kotlin 1.7.10无法使用 Kotlin 1.7.10
【发布时间】:2022-08-21 18:40:24
【问题描述】:

我在 IntelliJ IDEA 中有一个 Kotlin 项目(不针对 Android),目前可以使用 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\"
}

    标签: kotlin gradle intellij-idea


    【解决方案1】:

    我不得不手动升级gradle

    ./gradlew wrapper --gradle-version 7.5.1
    ./gradlew --version
    ./gradlew build
    

    之后,gradlew 构建 Kotlin 1.7.10 代码,在 IntelliJ IDEA 中也构建成功。有趣的是,IntelliJ 抱怨不可用的功能(肯定是键入 T 和 Any),但它可以编译它并且它似乎可以工作。

    感觉 IntelliJ IDEA CE 还没有正式支持 Kotlin 1.7.10?

    【讨论】:

      【解决方案2】:

      这就是新生成的(由 IDEA CE / gradle-7.4.2)build.gradle 的样子

      plugins {
          id 'org.jetbrains.kotlin.jvm' version '1.7.10'
      }
      
      group = 'org.example'
      version = '1.0-SNAPSHOT'
      
      repositories {
          mavenCentral()
      }
      
      dependencies {
          testImplementation 'org.jetbrains.kotlin:kotlin-test'
      }
      
      test {
          useJUnitPlatform()
      }
      
      compileKotlin {
          kotlinOptions.jvmTarget = '1.8'
      }
      
      compileTestKotlin {
          kotlinOptions.jvmTarget = '1.8'
      }
      

      【讨论】:

      • 有趣的。所以IDEA CE支持 kotlin 1.7.10……这是最顶层的 build.gradle 吧?内部项目 build.gradle 是什么样子的?
      【解决方案3】:

      就我而言,只需替换为

      classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
      

      classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
      

      【讨论】:

        【解决方案4】:

        您可以尝试在构建和 gradle 中添加它。

        kotlinOptions {
            jvmTarget = "1.8"
        }
        

        【讨论】:

        • 不幸的是,这并没有解决问题。我还尝试将其包装在compileKotlin 中,或设置languageVersion = 1.7。我仍然得到同样的错误。当我尝试使用./gradlew build 从控制台构建它时,也会出现该错误,因此这不是仅 IDE 的问题。
        • 这不是 .gradle 文件中已添加的解决方案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-20
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 1970-01-01
        相关资源
        最近更新 更多