30

I get the following error when trying to compile a unit test written in kotlin.

Task :app:compileDebugUnitTestKotlin FAILED ...Cannot inline bytecode built with JVM target 1.7 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option

I've tried setting the source compatibility for my android configuration in my app build.gradle:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

as well as configuring all kotlin compile tasks in the root build.gradle:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
Stephen__T
  • 2,004
  • 3
  • 25
  • 48

4 Answers4

40

This is a temporary Android Studio bug. I needed to add these lines to the app's gradle file:

android {
    ...
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    ...
}
aschepler
  • 70,891
  • 9
  • 107
  • 161
Daniele
  • 1,030
  • 9
  • 20
11

1)On a Mac

Android Studio -> Preferences -> Kotlin Compiler -> Change Target JVM version

2) PC

Android Studio -> File -> Settings -> Kotlin Compiler -> Change Target JVM version

rafa
  • 1,319
  • 8
  • 20
  • That is set to `1.8`, and shouldn't affect the `gradle` build. – Stephen__T Mar 05 '19 at 16:45
  • Why wouldn't it affect the Gradle build? What is its purpose then? Also... I tried this setting and it had no effect on this error. I can't find where 1.6 is being specified. – Oscar Jul 06 '20 at 00:17
  • This is a good suggestion but it's not woking on Android Studio 4.0.1. The alert continuos showing as red code. When I specify in build.gradle(app) finally stop showing those red code suggestions. – Hanako Jun 27 '21 at 01:03
  • 1
    I am not a person. – Dávid Horváth Sep 07 '22 at 12:23
3

The default target JVM bytecode is 1.6. But this can be set by changing build.gradle(:app):

android{
...
    kotlinOptions {
        jvmTarget = 1.8
    }
}

It seems, the Android Studio Kotlin compiler target JVM option does not work.

CodingFrog
  • 1,225
  • 2
  • 9
  • 17
1

you need to add this in build.gradle file.

android {
  kotlinOptions {
    jvmTarget = "1.8"
    freeCompilerArgs += [
        '-Xjvm-default=enable'
    ]
  }

}

Ahmad Bhatti
  • 189
  • 1
  • 8