未解析的引用:在build.gradle.kts中编译Kotlin

b4lqfgs4  于 2023-03-30  发布在  Kotlin
关注(0)|答案(4)|浏览(283)

Kotlin项目成功构建by build.gradle:

compileKotlin {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
compileTestKotlin {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}

不错。
但我需要更改为build.gradle.kts

plugins {
    kotlin("jvm") version "1.2.10"
    id("application")
}

group = "com.myproject"
version = "1.0-SNAPSHOT"

application {
    mainClassName = "MainKt"
}

java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
    jcenter()
}

val kotlinVer = "1.2.10"

dependencies {
    compile(kotlin(module = "stdlib-jre8", version = kotlinVer))
    implementation("com.google.code.gson:gson:2.7")
    implementation("com.squareup.okhttp3:logging-interceptor:3.8.0")
    implementation("com.squareup.retrofit2:converter-gson:2.1.0")
    implementation("com.squareup.retrofit2:retrofit:2.5.0")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

现在我得到错误:

Line 32: compileKotlin {
           ^ Unresolved reference: compileKotlin
zrfyljdw

zrfyljdw1#

KotlinGradle DSL中存在导致此问题的问题。
https://github.com/gradle/kotlin-dsl-samples/issues/1368
您需要使用以下解决方法,直到问题得到解决。

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
owfi6suc

owfi6suc2#

根据官方Kotlin文档(using Gradle part),我建议在build.gradle.kts中使用这样的构造:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    java
    kotlin("jvm") version ("1.3.21")
}
// repositories, dependencies, etc...
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks

compileKotlin.kotlinOptions {
    jvmTarget = "1.8"
}
compileTestKotlin.kotlinOptions {
    jvmTarget = "1.8"
}
sqyvllje

sqyvllje3#

使用withType关键字:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    val kotlin = "1.3.61"
    kotlin("jvm") version kotlin apply false
}

subprojects {
    repositories { mavenCentral(); mavenLocal() }
    apply(plugin = "org.jetbrains.kotlin.jvm")

    tasks {
        val java: String by project
        withType<KotlinCompile>{ 
            kotlinOptions { jvmTarget = java }; sourceCompatibility = java; targetCompatibility = java 
        }
    }
}
h9a6wy2h

h9a6wy2h4#

无关:Unresolved reference: compile

我能够将依赖项提供给implementation()而不是compile()

// build.grdle.kts
//...
dependencies {
//- compile("com.cloudbees:groovy-cps:1.22)
//+ implementation("com.cloudbees:groovy-cps:1.22)
    implementation("com.cloudbees:groovy-cps:1.22)
}
//...

这个帖子说apiimplementation关键字是compile关键字Gradle Implementation vs API configuration的后继者
编辑:Google为Unresolved reference: compile打开了这个问题

相关问题