https://kotlinlang.org logo
Title
n

natpryce

10/15/2018, 3:41 PM
Hi. I’m using a third-party Kotlin library compiled for JVM 1.6. My application is targeting JVM 1.8. When I try to compile my code that calls an inline higher-order function in the library, I get the following compilation error:
Error:(17, 5) Kotlin: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option
What’s the solution to this?
c

Casey Brooks

10/15/2018, 3:45 PM
Make sure that Kotlin is also targeting jdk8, not just Java. For example, in gradle:
apply plugin: 'java'
apply plugin: "kotlin"

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

compileKotlin {
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
n

natpryce

10/15/2018, 3:46 PM
I think that’s why I’m getting the error … My code is being compiled to 1.8 because of that
kotlinOptions.jvmTarget
declaration in Gradle, but the bytecode it’s being inlined into is 1.6 bytecode in the library my code depends on.
c

christophsturm

10/15/2018, 3:58 PM
why is your code inlined into the library code? shouldnt it be the other way around?
n

natpryce

10/15/2018, 6:21 PM
The library defines an inline higher order function
I pass it a lambda
The compiler would normally inline the library function and body of the lambda parameter into the body of the enclosing function
But the enclosing function (and therefore the lambda) is compiled for JDK1.8 and the library function is compiled for JDK1.6
c

christophsturm

10/15/2018, 6:30 PM
you could copy that one source file to your own project.
n

natpryce

10/15/2018, 6:31 PM
The library is mostly higher-order functions.
Thanks to defining a DSL
so that would mean copying almost all functions from the library into my own code
Plus many of the library functions call private features of the library, and so cannot be copied into my own compilation units
c

Casey Brooks

10/15/2018, 6:34 PM
Which library is this?
n

natpryce

10/15/2018, 6:35 PM
In this case it’s minutest.
But in the general case… is there a way round this bytecode version mismatch when using libraries?
c

Casey Brooks

10/15/2018, 6:48 PM
I think I’ve figured it out. Setting the Kotlin version in
compileKotlin {}
doesn’t set the same value for the
test
sourceset. Try adding
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
to my snippet above
n

natpryce

10/15/2018, 10:14 PM
Thanks! You’re a star
g

gildor

10/15/2018, 11:19 PM
Better to use withType<KotlinCompile> to configure all available Kotlin tasks