7

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 the above code always give me this 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:

  1. What is built with jvm 1.6 and 1.8 respectively? How this could happen (please use example to explain) ?

  2. As you can see in my build.gradle I already declared build target is JVM 1.8. Why I still get this error?

xarlymg89
  • 2,205
  • 2
  • 24
  • 35
user842225
  • 4,171
  • 6
  • 42
  • 74

1 Answers1

19

Add kotlinOptions and set jvmTarget to 1.8 in your build.gradle file to resolve the problem

kotlinOptions {
    jvmTarget = "1.8"
}
Md. Asaduzzaman
  • 13,121
  • 2
  • 19
  • 34