Why tasks.withType(JavaCompile) can't work?


in Android studio,use tasks.withType(JavaCompile) tell me wrong,why?

in project build.gradle.kts file:

plugins {
    id("com.android.application") version "8.1.2" apply false
}

in model build.gradle.kts file:

plugins {
    id("com.android.application")
}

android {
    allprojects{
        gradle.projectsEvaluated{

        }
    }

    tasks.withType(JavaCompile){
        options.compilerArgs += ['-Xlint:deprecation', '-Xlint:unchecked']
    }

Because you try to use Groovy DSL code in a Kotlin DSL file.
Those are different languages.
In Kotlin DSL it would be tasks.withType<JavaCompile> { ... } or tasks.withType(JavaCompile::class) { ... } or tasks.withType(JavaCompile::class.java) { ... }, all three would work.

But you should use neither of them, as you break task configuration avoidance for all JavaCompile tasks that way.

Instead you should use tasks.withType<JavaCompile>().configureEach { ... } or tasks.withType(JavaCompile::class).configureEach { ... } or tasks.withType(JavaCompile::class.java).configureEach { ... }.

Of course the following line will also not work, as again this is Groovy DSL code that you cannot use like that in a Kotlin DSL file.

1 Like

Thank you very much, the problem is solved。

1 Like