gradle / kotlin-dsl-samples

Samples builds using the Gradle Kotlin DSL
https://gradle.org/kotlin/
Other
3.71k stars 434 forks source link

KT-31077 - android.kotlinOptions block is lacking its type #1368

Closed mannodermaus closed 5 years ago

mannodermaus commented 5 years ago

YouTrack Issue

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

Expected Behavior

I can access the implicit KotlinJvmOptions directly:

android {
  kotlinOptions {
    jvmTarget = "1.8"
  }

Current Behavior

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

Your Environment

gildor commented 5 years ago

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

mannodermaus commented 5 years ago

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.

StefMa commented 5 years ago

Can you please refer to the ticket in the Jetbrains issue tracker?! Thank you :)

Edit

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

gildor commented 5 years ago

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.

mannodermaus commented 5 years ago

I raised the ticket as KT-31077 on YouTrack now. Added the link to the OP, too!

eskatos commented 5 years ago

Fixed in Kotlin 1.3.60 🎉

guuilp commented 5 years ago

@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.