Reputation: 8417
I'm trying to implement viewmodel with kotlin. First I added the needed dependecies:
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0"
Then I created a simple viewmodel:
class MyViewModel: ViewModel() {
val greeting = "Hello World"
}
But when I tried to access the view model from the activity with kotlin property delegate:
val model by viewModels<MyViewModel>()
The compiler does not resolve viewModels
. I don't know what the problem is. Did I miss something?
Upvotes: 80
Views: 48651
Reputation: 1
For me, the only solution was to go to-> File->Repair IDE, then in the notification, click on More->Rescan Project Indexes, then Reopen Project.
That's not the definitive solution but only in that way the IDE recognizes viewModels() again, until a new gradle sync.
I think this issue is not related to the dependencies, is more about a bug in the IDE.
Upvotes: 0
Reputation: 377
For me, I worked a lot to make this work by trying different different imports. But none of them worked. After that I understood, that it was not the imports, it was the place I was trying to use by viewModels
that was causing the problem.
You can use the same syntax in "onCreate" function, pass the view model to another composable function, and use it there without any problem, like mentioned in this solution
https://stackoverflow.com/a/73091010/8860360
Another workaround is like this, thus you don't need to pass the viewmodel to function:
ie, instead of this val model by viewModels<MyViewModel>()
you should use this val model: MyViewModel = viewModel()
inside a composable function
Upvotes: 0
Reputation: 437
To people who already has declared the fragment-ktx
or the activity-ktx
in their build file but still the IDE won't find the viewModels()
or activityViewModels()
for them, neither in the Fragments nor the Activities: Restart your Android Studio. If it didn't work, restart your computer. It worked for me.
UPDATE
Didn't work for long. Although I realized the import statement doesn't show any errors and it's just the statement that show the error, but even with that, the code compiles fine.
Upvotes: 0
Reputation: 11
See if you have navigation-fragment dependency added.
implementation("androidx.navigation:navigation-fragment-ktx:2.7.0")
implementation("androidx.navigation:navigation-ui-ktx:2.7.0")
Having this one and activity/fragment-ktx additionaly made viewModels delegate unavailable for me so I got rid of both of them and only left the navigation artifact.
Upvotes: 1
Reputation: 71
It depends, on how you are using ViewModel. If you don't use fragments, this is enough to resolve "Unresolved reference: viewModels" for activity.
// ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.activity:activity-ktx:1.6.1'
// is to add the dependency on Fragment
implementation 'androidx.fragment:fragment-ktx:1.2.5'
// is to add the dependency on Activity
implementation 'androidx.activity:activity-ktx:1.1.0'
Upvotes: 5
Reputation: 886
For me, I was having the following dependencies:
implementation 'androidx.activity:activity-compose:1.5.0'
but still I faced this same error.
The reason was that by viewModels()
must be called inside the onCreate()
:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val customViewModel: MyViewModel by viewModels()
setContent {
AppTheme {
Surface() {
MyApp(customViewModel = customViewModel)
}
}
}
}
}
and not in any other composable function. But I was trying to calling it from MyApp()
composable function. Hence the error.
Upvotes: 4
Reputation: 101
In my case, I was just using the wrong type of Activity. I had an android.app.Activity
instead of an androidx.appcompat.app.AppCompatActivity
.viewModels()
is only available in the latter.
Upvotes: 1
Reputation: 2687
Resolved this error, using below dependency in module-level build.gradle
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
OR
implementation 'androidx.lifecycle:lifecycle-extensions-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.4.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'
I have also added below dependency to implement ViewModel in Kotlin
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
My module-level build.gradle start as per below
plugins {
id 'com.android.application'
id 'kotlin-android'
}
Upvotes: 6
Reputation: 407
For me the solution above this not work.
Add this dependency: :
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
Add This dependency :
implementation "androidx.lifecycle:lifecycleviewmodel:2.2.0"
Upvotes: 3
Reputation: 9732
Add these dependencies:
implementation "androidx.activity:activity-ktx:$activity_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
You can find the latest versions of libraries here:
https://developer.android.com/jetpack/androidx/releases/activity
https://developer.android.com/jetpack/androidx/releases/fragment
Upvotes: 135
Reputation: 13302
For me the solution above this not work.
I needed to import :
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
Upvotes: 18