ViewModels + Hilt : cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6

Viewed 419

I am using view models and Hilt in my android app (Kotlin). But I have a issue when initializing my View model :

private val myViewModel: MyViewModel by viewModels()

The app compile fine and I can run it, but Android Studio indicates an error, and when I move my cursor above it I have the following message :

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'

In my build gradle I have specified Java 1.8 for Compile option (even for Kotlin).

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8
   targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
   jvmTarget = JavaVersion.VERSION_1_8
}

Here are the annotations for my viewmodel and my fragment

@HiltViewModel
class MyViewModel: ViewModel()

And

@AndroidEntryPoint
class MyFragment: Fragment()

some of my dependencies :

'androidx.core:core-ktx:1.5.0'
'androidx.appcompat:appcompat:1.3.0'
'com.google.dagger:hilt-android:2.35'
'com.google.dagger:hilt-android-compiler:2.35'
"androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

Has anyone faced a similar issue ? Thank you for your help !

2 Answers

I have the same issue and nothing helped me except reducing Kotlin version. I have put 1.5.10 instead of 1.5.31 and error disappeared. My corrections in build.gradle file as follow:

buildscript { ext.kotlin_version = '1.5.10'

...

dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Seems here is something wrong with new Kotlin version.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8' // <---- 
}

include '1.8' rather than JavaVersion.VERSION_1_8, sometimes compiler have difficulty in detecting

Related