116

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'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

group 'com.ali'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

And I have a simple java interface:

public interface MyMath {
    static int myAbs(int input) {
        return Math.abs(input);
    }
}

When I import this interface and try to call myAbs method it fails with this error:

Error:(6, 12) Kotlin: Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

I have created an intellij kotlin app and it was working correctly. Is it a bug in new Kotlin gradle plugin?

alisabzevari
  • 8,008
  • 6
  • 43
  • 67
  • Do you have JDK 8? – Zoe Feb 04 '18 at 10:23
  • Yes, and this problem only happens when I build the project in intellij. gradle in terminal works fine! – alisabzevari Feb 04 '18 at 10:24
  • What is the IDE plugin version that you use? I could not reproduce the error with the 1.2.21 plugin. If that happens with the newer Gradle & IDE plugin versions as well, you could file an issue at [kotl.in/issue](https://kotl.in/issue) and describe the exact steps that lead to the error. – hotkey Feb 04 '18 at 11:11
  • https://stackoverflow.com/a/60246275/4694013 – Anoop M Maddasseri Feb 16 '20 at 07:20

12 Answers12

199

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()
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Jairo Lozano
  • 3,883
  • 3
  • 20
  • 29
92

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.

alisabzevari
  • 8,008
  • 6
  • 43
  • 67
25

There is a gradle property used by Kotlin plugin which can be used. When you add

kotlin.setJvmTargetFromAndroidCompileOptions = true

to your gradle.properties file. When you have this configuration on your build.config

android{
  compileOptions {
      sourceCompatibility = 1.8
      targetCompatibility = 1.8
  }
}

problem sould be solved.

Karol Kulbaka
  • 1,136
  • 11
  • 21
24

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 Preferences

  2. Check the project Facets enter image description here

Antal Attila
  • 588
  • 6
  • 18
15

//Just add the below code in your Build gradle(Module:app)

compileOptions {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
 }
4

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()
Ayush
  • 3,695
  • 1
  • 32
  • 42
3

This is also set in project settings, under Project Settings > Modules > Kotlin. This was my specific problem... literally the last thing I tried.

Tim Keating
  • 6,443
  • 4
  • 47
  • 53
3

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

Seroj Grigoryan
  • 159
  • 1
  • 2
1

Not quite sure why this works, but you could try changing the setting in the Idea itself. Since Gradle from the command line works, but not when building from IntelliJ this is probably the root.

Go to File -> Project Structure. Go to the Project tab and make sure Project SDK is 8 (or newer) and set the Project language level to Java 8

enter image description here

The config in there seems to override Gradle for some reason, so changing it should work

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • It is 8 in my intellij! – alisabzevari Feb 04 '18 at 10:47
  • Try invalidating the caches and restarting – Zoe Feb 04 '18 at 10:52
  • Didn't work. I have a project created with older intellij (current is 2017.3.4). The old one is working. I am comparing .idea folders of two project with each other. Does it make sense? – alisabzevari Feb 04 '18 at 11:02
  • 3
    I had the same error on a maven project. Just solved adding 1.8 to the properties section. Take a look here https://youtrack.jetbrains.com/issue/KT-17492 for more info – Paolo Aug 31 '18 at 14:51
  • The "Project language level" setting controls how Java source code is highlighted in the IDE. It has no impact on Kotlin compilation whatsoever. – yole Sep 28 '18 at 20:04
1

Try this code, it solved my problem. I had a problem inViewModel Factory. //Just add the below code in your Build gradle(Module:app)

Error "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"

    kotlinOptions {
    freeCompilerArgs = ['-Xjvm-default=compatibility']
    jvmTarget = '1.8'
}
0

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.

dastan
  • 892
  • 10
  • 17
0

solved it by editing build.gradle.kts

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = "1.8"
Jim
  • 1,123
  • 13
  • 29