问题
I am developing an Android project with Kotlin and Dagger 2. I have a NetworkModule
it is supposed to provide a singleton instance of Retrofit. in which I define all those provider functions.
All code snippet below are inside NetworkModule
:
@Module
object NetworkModule {
...
}
I want to provide HttpRequestInterceptor , this is what I tried:
@Provides
@JvmStatic
internal fun provideHttpRequestInterceptor(): Interceptor {
// compiler error: 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
return Interceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
val request = requestBuilder.build()
chain.proceed(request)
}
}
But above code always give me compiler error 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
.
In my build.gradle I have specified
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
I have two questions now:
What is built with jvm 1.6 and 1.8 respectively? How this could happen (please use example to explain) ?
As you can see in my build.gradle I already declared build target is JVM 1.8. Why I still get this error?
回答1:
Add kotlinOptions
and set jvmTarget
to 1.8 in your build.gradle
file to resolve the problem
kotlinOptions {
jvmTarget = "1.8"
}
来源:https://stackoverflow.com/questions/59488983/why-i-still-get-cannot-inline-bytecode-built-with-jvm-target-1-8-into-bytecode