35

Kotlin project success build by build.gradle:

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

Nice.

But I need to change to 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"
}

and now I get error:

Line 32: compileKotlin {
           ^ Unresolved reference: compileKotlin
Alexei
  • 14,350
  • 37
  • 121
  • 240

4 Answers4

45

There's an issue in the Kotlin Gradle DSL that causes this.

https://github.com/gradle/kotlin-dsl-samples/issues/1368

You will need to use the following workaround until it gets resolved.

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
15

Following official Kotlin documentation (using Gradle part), I suggest to use such constructions in the 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"
}
AlexanderRS
  • 359
  • 2
  • 9
  • 2
    Shouldn't be there a difference between `compileTestKotlin` and `compileKotlin`. Or maybe I'm missing sth? – Siamak Aug 18 '21 at 10:15
9

Use withType keyword:

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 
        }
    }
}


Dmitry Kaltovich
  • 2,046
  • 19
  • 21
0

unrelated: Unresolved reference: compile

I was able to provide the dependency to implementation() instead of 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)
}
//...

this so post says api and implementation keywords are successors to the compile keyword Gradle Implementation vs API configuration

edit:google turned up this question for Unresolved reference: compile

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147