Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the jvm target for android studio using gradle and kotlin?

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'     } } 
like image 644
Stephen__T Avatar asked Mar 03 '19 18:03

Stephen__T


People also ask

How does Kotlin compile to JVM?

Yes, when targeting the JVM, Kotlin is compiled to JVM *. class files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code.

What is Kotlin JVM?

Kotlin is a general purpose, free, open source, statically typed “pragmatic” programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features. It is focused on interoperability, safety, clarity, and tooling support.

What is target JVM version?

-jvm-target versionThe default value is 1.8 .


1 Answers

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"     }     ... } 
like image 147
Daniele Avatar answered Oct 10 '22 02:10

Daniele