Why kotlin gradle plugin cannot build with 1.8 target?

前端 未结 10 2069
悲哀的现实
悲哀的现实 2020-12-08 12:50

I have the simplest gradle project configured using intellij for kotlin 1.2.10. Here is my build.gradle file:

buildscript {
    ext.kotlin_version = \'1.2.10         


        
相关标签:
10条回答
  • 2020-12-08 13:01

    Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler a setting called Target JVM version should have been set to 1.8.

    0 讨论(0)
  • 2020-12-08 13:02

    This worked for me in Gradle with Kotlin DSL:

    import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    val compileKotlin: KotlinCompile by tasks
    compileKotlin.kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
    
    0 讨论(0)
  • 2020-12-08 13:05

    In my case, I tried all @alisabzevari options including given in comment, but It didn't work,

    my mistake I added java file also in src/<sourcset_name>kotlin/ folder, later I converted java file to kotlin file. and Voila! It works.

    May be it will help somebody.

    0 讨论(0)
  • 2020-12-08 13:09

    I think this could be helpful for those using Android Studio 3.2 on Mac.

    To change the Kotlin Compiler Target JVM version you should go to Android Studio -> Preferences -> Kotlin Compiler and then change the Target JVM version choosing from the dropdown.

    Anyway, I'm still getting the following error

    Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

    SOLVED

    Adding the following to my build.gradle solved the problem:

    android {
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    

    About this and other Gradle configuration options: https://kotlinlang.org/docs/reference/using-gradle.html


    With Kotlin Gradle DSL:

    import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
    
    (kotlinOptions as KotlinJvmOptions).apply {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
    
    0 讨论(0)
  • 2020-12-08 13:11

    It turned out that it was my kotlin compiler configuration in intellij settings. In Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler a setting called Target JVM version should have been set to 1.8.

    0 讨论(0)
  • 2020-12-08 13:15

    Please check if you have the following three stuff set in idea:

    1. kotlin options in build.gradle
        kotlinOptions {
            jvmTarget = '1.8'
        }
    
    1. Check the relevant Idea preferences

    2. Check the project Facets

    0 讨论(0)
提交回复
热议问题