compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]”

后端 未结 3 1429
甜味超标
甜味超标 2020-12-08 02:07

I\'m trying to configure Kotlin to work with Java 1.8 in my Android project. I\'ve tried adding the compileKotlin block at the bottom of my build.gradle

相关标签:
3条回答
  • 2020-12-08 02:20

    The error you are getting means that there's no compileKotlin task in the project, and that's expected for Android projects.

    The Kotlin compilation task names in Android projects contain the build variant names (those are combined from build type, product flavor and other settings and look like debug or releaseUnitTest -- the tasks are compileDebugKotlin and compileReleaseUnitTestKotlin respectively). There's no compileKotlin task, which is usually created for the main source set in ordinary Java + Kotlin projects.

    Most probably, you want to configure all Kotlin compilation tasks in the project, and to do that, you can apply the block as follows:

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    
        kotlinOptions {
            jvmTarget = '1.8'
            apiVersion = '1.1'
            languageVersion = '1.1'
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:26

    Not a direct answer to the question, but Google led me to this answer when experiencing the same compileKotlin block in build.gradle file throws error “Could not find method compileKotlin() for arguments […]” error.

    In my case the problem was caused by importing a code module to my project that contained only Java code. The fix was to ensure the following is in the Java-specific module's build.gradle:

    apply plugin: 'kotlin-android'
    
    0 讨论(0)
  • 2020-12-08 02:31

    From the kotlin-android documentation:

    Android plugin also adds kotlinOptions extension to android section to set options for all kotlin tasks:

    android {
        kotlinOptions {
            jvmTarget = '1.8'
            noStdlib = true
        }
    }
    
    0 讨论(0)
提交回复
热议问题