Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: viewModels() [duplicate]

I just started an Android App and I want to use ViewModel. I looked into documentation and I put all the dependencies from Lifecycle but when i try to instantiate a view model like this viewMyViewModel by viewModels() i get this error 'Unresolved reference: viewModels()'.

These are the dependencies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    def lifecycle_version = "2.2.0"
    def arch_version = "2.1.0"

    implementation "android.arch.lifecycle:extensions:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

    implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"
    testImplementation "androidx.arch.core:core-testing:$arch_version"

}
like image 417
Valentin Avatar asked Apr 29 '20 09:04

Valentin


People also ask

How do I find all unresolved elements in a model?

Select the Only unresolved/unloaded radio button. Then, click the Find button. You see a list of all unresolved and unloaded elements in the model. Delete the reference to the missing element from the model for each search result or restore the element in question.

How do you correct for unresolved and unloaded references in Rhapsody?

How do you correct for unresolved and unloaded references in an IBM Rational Rhapsody model? When an element is removed from a model, Rhapsody cannot delete any read-only references to that element. Upon removal of the original model element, these lingering references appear with a (U) symbol.

Why can't I resolve unresolved references from mainactivity?

Since the unresolved references, like VacinationCardViewModel, are in the same source set as MainActivity, it seems very weird that they can't be resolved from MainActivity. The only thing I can think of is that you've forgotten the import statements for those classes.

Does the compiler resolve viewmodels?

The compiler does not resolve viewModels. I don't know what the problem is. Did I miss something? implementation "androidx.activity:activity-ktx:$activity_version" implementation "androidx.fragment:fragment-ktx:$fragment_version"


Video Answer


1 Answers

Include this in your dependency,

implementation "android.arch.lifecycle:extensions:1.1.1"

then, add apply plugin: 'kotlin-kapt' in build.gradle

then add,

kapt "android.arch.lifecycle:compiler:1.1.1"

Replace other annotationProcessor with kapt

See if it works or not, if not, then go to

file -> invalidate cache and restart

like image 54
Shakil Ahmed Shaj Avatar answered Oct 24 '22 18:10

Shakil Ahmed Shaj