Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42730

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?

Upvotes: 0

Views: 1267

Answers (5)

Divyansh Ingle
Divyansh Ingle

Reputation: 370

I know the question was asked previously when it was not deprecated but for other developer's info. The Class has been deprecated and now you can directly user ViewModelProvider with constructor only.

For Further Info Android Developer

Upvotes: 0

Rafael
Rafael

Reputation: 6369

In my case I also need to remove from gradle.properties these lines:

android.useAndroidX=true
android.enableJetifier=true

Upvotes: 0

Dhananjay Waghela
Dhananjay Waghela

Reputation: 178

Please try this, Change in your viewmodel and extension version '1.1.0'

def lifecycle_version = "1.1.0" // change in this line

implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"

Note : I have also using this kind of error message, but my problem was solved when I downgrade version 1.1.1 with version 1.1.0.

You need to check downgrade your version number.

Upvotes: 1

Stanislav Bondar
Stanislav Bondar

Reputation: 6255

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

Upvotes: 1

Sarthak Gandhi
Sarthak Gandhi

Reputation: 2170

Please try this :

implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0,0'
implementation 'androidx.lifecycle:lifecycle-livedata:2.0.0'

This is what I am using right now as I converted to androidX.

Hope this helps.

Upvotes: 0

Related Questions