After applying the kotlin-android plugin, a global configuration closure named kotlinOptions becomes available inside the android block. This closure takes one parameter, however it's not strongly typed and resolved to Action<Any>:
public fun com.android.build.gradle.internal.dsl.BaseAppModuleExtension.kotlinOptions(configure: org.gradle.api.Action<kotlin.Any>): kotlin.Unit { /* compiled code */ }
I can access the implicit KotlinJvmOptions directly:
android {
kotlinOptions {
jvmTarget = "1.8"
}
The missing typing prevents us from setting the configuration without a proper cast in place:
kotlinOptions {
// We have to add the explicit cast before accessing the options itself.
// If we don't, it does not work: "unresolved reference: jvmTarget"
val options = this as KotlinJvmOptions
options.jvmTarget = "1.8"
}
}
Alternatively, we need to configure each task individually:
tasks.withType<KotlinCompile> {
kotlinOptions {
options.jvmTarget = "1.8"
}
}
I believe this is not a problem of Kotlin DSL, but this convention. Is it part of kotlin-android plugin feature? If so, better to report it on Kotlin issue tracker
Thanks for the reply. I wasn't sure where this convention is being synthesized, but I do believe that it's added through the kotlin-android plugin. I'll report it on the Kotlin issue tracker and attach a link here.
Can you please refer to the ticket in the Jetbrains issue tracker?! Thank you :)
I think the issue is somewhere here 馃槀
https://github.com/JetBrains/kotlin/blob/bd1f95da04c4363cb7f5da4c6a9eef8e6cdca882/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt#L802-L861
I think that the problem with usage of extension function addExtenion. This function add extension without type:
https://github.com/JetBrains/kotlin/blob/b6102c52b9dac6a258a2add4a4a28571a502692c/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/gradleUtils.kt#L52-L53
Instead, it should be something like this:
internal inline fun <reified T : Any> Any.addExtension(name: String, extension: T) =
(this as ExtensionAware).extensions.add(T::class.java, name, extension)
So in this case extension will be properly typed.
I raised the ticket as KT-31077 on YouTrack now. Added the link to the OP, too!
Fixed in Kotlin 1.3.60 馃帀
@eskatos it was fixed in Gradle 5.6, actually: https://youtrack.jetbrains.com/issue/KT-31435.
I tested here with kotlin 1.3.60 and still got the error. When I updated to Gradle 5.6.x, it worked.
Most helpful comment
@eskatos it was fixed in Gradle 5.6, actually: https://youtrack.jetbrains.com/issue/KT-31435.
I tested here with kotlin 1.3.60 and still got the error. When I updated to Gradle 5.6.x, it worked.