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?
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
For me the solution above this not work.
I needed to import :
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
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'
}
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"
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'
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.
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.
Related
I am trying to use Koin to inject my viewModel (which has some dependencies as well) like this:
I don't understand why it cannot find getViewModel when I have the following import:
I am using this Koin version: implementation "io.insert-koin:koin-android:$koin_version"
where $koin_version = '3.2.0-beta-1'
Any thoughts why my import is ignored here?
You're using a wrong import, you should use:
import org.koin.androidx.compose.getViewModel
To use it you need the following dependency:
implementation("io.insert-koin:koin-androidx-compose:$koinVersion")
Here's how I did it in koin 3.3.2
#Composable
fun HomeScreen(viewModel: PokemonViewModel = koinViewModel()) {
}
I also added koin-core to build.gradle(:app)
def koin_version = '3.3.2'
implementation "io.insert-koin:koin-core:$koin_version"
implementation "io.insert-koin:koin-android:$koin_version"
implementation 'io.insert-koin:koin-androidx-compose:3.4.1'
SOURCE
I want to implement ViewModel into my app, but I cant import viewModels() or activityViewModels(). Trying to follow tutorial but its still unresolved reference for me.
private val userViewModel: ProfileFlowFragment.UserViewModel by viewModels()
Imports:
implementation "androidx.fragment:fragment-ktx"
implementation "androidx.activity:activity-ktx"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
Did you specify the version in implementation "androidx.fragment:fragment-ktx" ?
Anyway, I have these dependencies in my Gradle file, and everything is fine.
implementation "androidx.core:core-ktx:$corektxVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_stdlibVersion"
Also add
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
The latest stable versions are below, it should work.
def activity_version = "1.2.0"
def fragment_version = "1.3.0"
implementation "androidx.activity:activity-ktx:$activity_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
I already have added dependency but still getting error
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
Unresolved reference: viewModels
You need to add fragment-ktx and activity-ktx
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
it also depends on how you want to use dependencies.
//to add the dependency on Fragment
implementation 'androidx.fragment:fragment-ktx:1.2.5'
//to add the dependency on the Activity
implementation 'androidx.activity:activity-ktx:1.1.0'
After, restart Android Studio with invalidate cache, and clean + rebuild build, I still can't import ViewModel class from Koin library. Even forcing manually the import org.koin.android.viewmodel.ext.android.viewModel import.
app / build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "org.koin:koin-android:1.0.2"
implementation 'org.koin:koin-android-viewmodel:1.0.2'
...
}
Anyone with the same problem?
Your activity must extend AppCompatActivity instead of Activity.
If you want to use koin with activities, fragments or services your class must implement implement KoinComponent.
I needed to use AppCompatActivity instead Activity
I had the same issue (also when the Activity extended AppCompatActivity), but in my case, the solution was in using stable version of core-ktx:
// Stable version - Works fine with koin and koin's viewModel
implementation "androidx.core:core-ktx:1.0.1"
// Alpha version - When in use viewModel is not found
implementation 'androidx.core:core-ktx:1.1.0-alpha03'
You don't need Koin library dependency to use by viewmodels method . You only need to ensure that you have these dependencies :
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0"
implementation "androidx.activity:activity-ktx:1.1.0"
Please follow this android studio guide for more info : https://androideveryday.com/2020/03/07/android-studio-guide-to-viewmodel-livedata-2020-edition/
I am trying to implement ViewModel in a 100% Kotlin app. Every piece of documentation I can find says I want to use this to get the ViewModel instance:
ViewModelProviders.of(this).get(CustomViewModel::class.java)
According to the docs, I should be able to import this with:
import android.arch.lifecycle.ViewModelProviders
This import is unresolved though. I am using the following in my build file:
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycler:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
Why can't I access ViewModelProviders?
Include the following as a dependency:
implementation "android.arch.lifecycle:extensions:1.1.1"
The equivalent AndroidX dependency is:
"androidx.lifecycle:lifecycle-extensions:VERSION"
in where VERSION can be replaced for Current Stable, Beta or Alpha values given in this official link
This dependency is for both ViewModel and LiveData and thus would not require you to give separate dependencies for the same either; i.e. the first two dependencies indicated by you can be replaced by the aforementioned lifecycle extensions dependency.
In my case I was missing :
implementation "androidx.fragment:fragment-ktx:1.1.0"
In addition to what Sup suggested, you'll have to correct lifecycler:compiler to lifecycle:compiler - the Gradle sync shouldn't even complete successfully with this typo.
Secondly, the standard android annotation processing ("annotationProcessor") doesn't really work with Kotlin. Instead, use Kotlin's kapt.
At the top of your build.gradle file, add the following:
apply plugin: 'kotlin-kapt'.
And in your dependencies section, replace occurences of annotationProcessor (like the above one) with kapt, e.g.
kapt "android.arch.lifecycle:compiler:1.1.1"
ViewModelProviders is deprecated in 1.1.0
Check this android docs
https://developer.android.com/topic/libraries/architecture/viewmodel
I am using 2.3.1 and add below dependency in build.gradle
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'
Declare ViewModel in your Kotlin code as per below:
private val cartViewModel: CartViewModel by viewModels()
Still, if you facing below error like
Unresolved reference: viewModels
Then need to add below below dependency in build.gradle
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'
OR
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
I solve this by doing like this.
implement this dependency
implementation "android.arch.lifecycle:extensions:1.1.1"
then call my ViewModel class
homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
I faced this kind of problem in AndroidStudio 3.0.1 and solved by adding
following dependencies in the proper build.gradle:
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
version code may be different as AndroidStudio tells you if it differs.
If the above "adding/updating dependecies" did not resolve your issue then try to restart your android studio. It´s just to root, I do not see any major issue with it. Hope it helps.
Resolved with this dependency for me
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
If someone want androidx, then, first - in build.gradle (app):
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
Second - in file with code:
import androidx.lifecycle.ViewModelProvider
And third - use it:
ViewModelProvider(this).get(CustomViewModel::class.java)
Or use it like this (if needed ViewModelFactory):
ViewModelProvider(this, CustomViewModelFactory).get(CustomViewModel::class.java)
Not really sure why there are soo many solutions to this problem however while none of these worked for me, I did find a solution in my case.
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
This can also be resolved by targeting minSdkVersion to 21.
If you have minSdkVersion 21 or higher, you won't need implementation "android.arch.lifecycle:extensions:1.1.1".
android {
kotlinOptions {
jvmTarget = '1.8'
}
}
project e app level