Hi. I’m using a third-party Kotlin library compil...
# announcements
n
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:
Copy code
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
Make sure that Kotlin is also targeting jdk8, not just Java. For example, in gradle:
Copy code
apply plugin: 'java'
apply plugin: "kotlin"

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

compileKotlin {
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
n
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
why is your code inlined into the library code? shouldnt it be the other way around?
n
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
you could copy that one source file to your own project.
n
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
Which library is this?
n
In this case it’s minutest.
But in the general case… is there a way round this bytecode version mismatch when using libraries?
c
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
Copy code
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
to my snippet above
n
Thanks! You’re a star
g
Better to use withType<KotlinCompile> to configure all available Kotlin tasks