10

I have a Kotlin project in Android Studio. I am calling a static method in Java interface from the Kotlin code. The build fails with the error,

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

Error

I have the following in my build.gradle,

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

I have also changed the Target JVM version to 1.8 in Kotlin compiler settings. Still, the build throws the error. Also tried Invalidating cache and restarting.

Kotlin Compiler Settings

Android Studio version: 3.0.1

Raj
  • 143
  • 1
  • 5

4 Answers4

11

Proper way to set Kotlin compile options in android gradle. Make these changes to your app level Build.gradle

Change

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

to

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

Then write this task

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

just below(not necessarily)

repositories {
    mavenCentral()
}

For details, refer here

Debanjan
  • 2,817
  • 2
  • 24
  • 43
5

The compileOptions section in build.gradle affects the Java compiler, not the Kotlin compiler. To set the target JVM version for the Kotlin compiler, use the following block:

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

See the documentation for more information.

yole
  • 92,896
  • 20
  • 260
  • 197
  • I added the following code to my root build.grade file, Still getting the same error. `plugins { id "org.jetbrains.kotlin.jvm" version "1.2.21" } compileKotlin { kotlinOptions.jvmTarget = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }` – Raj Feb 24 '18 at 01:50
0

I had the same problem because I had a folder for the integration tests different than the unit test folder. So the build.gradle needs more configuration:

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
} 
//added this
compileIntegrationTestKotlin {
  kotlinOptions.jvmTarget = "1.8"
}
Antonio Martin
  • 361
  • 3
  • 12
0

I am able to resolve this by adding below block as my build.gradle is not in dsl. To set the target JVM version for the Kotlin compiler, use the following block:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57