Search code examples
androidkotlinviewmodelandroid-lifecycle

android.arch.lifecycle.ViewModelProviders is not recognized in Kotlin code


For Java project, in order to recognize android.arch.lifecycle.ViewModelProviders, I simply define the following in my build gradle

Java project

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    def lifecycle_version = "1.1.1"
    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    // alternately - if using Java8, use the following instead of compiler
    implementation "android.arch.lifecycle:common-java8:$lifecycle_version"

However, in Kotlin project, I can hardly make it recognizes android.arch.lifecycle.ViewModelProviders. I tried

Kotlin project

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    def lifecycle_version = "1.1.1"
    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    kapt "android.arch.lifecycle:compiler:$lifecycle_version"

It is still not able to recognize android.arch.lifecycle.ViewModelProviders

enter image description here

The complete build.gradle is as follow - https://github.com/yccheok/AndroidDraw/blob/748baf44571c5b83d0c6e5b6a7137eef9cb17cdc/app/build.gradle

May I know, is there anything else I had missed out?


Solution

  • Using kapt you should also apply plugin: 'kotlin-kapt'.