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"
}
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 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.
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.
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With