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
Related
I'm trying to implement navigation in my app which is built with Jetpack Compose, but when I try to navigate from a screen to another I get:
java.lang.IllegalArgumentException: CreationExtras must have a value by SAVED_STATE_REGISTRY_OWNER_KEY
I'm using:
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
And here is the code:
if(viewModel.isAuthenticated) {
navController.navigate(Screen.Profile.route)
}
#Dragan.T 's answer is correct.
Adding
implementation "androidx.navigation:navigation-compose:2.5.1"
Solved my problem. As for why we need to add, I am not so sure.
If you use Compose with Fragments, then you may not have the Fragments dependency where viewModels() is defined.
Adding:
implementation "androidx.fragment:fragment-ktx:1.5.2"
to my build.grade script fixed it for me (previously this was a transitive dependency).
Hilt release notes for 2.43 flags a dependency incompatibility:
As part ViewModel bug fixes, dependencies were updated as below. androidx.navigation users will need to update to 2.5.0 to interoperate. These libraries require building with SDK 31. To build with SDK 31, AGP users will need to use AGP 7.0+. This will also require using JDK11.
androidx.activity and androidx.fragment to 1.5.0
androidx.lifecycle to 2.5.0
androidx.savedstate to 1.2.0
Insert implementation "androidx.navigation:navigation-compose:2.5.1" into your gradle file. If you already have it, be sure it's updated with the latest (2.5.1) version.
In my case I had to upgrade two dependencies: Hilt and androidx.navigation. The curious issue is that after upgrading it, the error is gone, but if I downgrade again to previous versions, the error doesn't appear again. These are the dependencies I changed:
navigation_version = "2.5.3" // was 2.5.1
dagger_hilt_version = "2.44.2" // was 2.43.2
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
implementation "androidx.navigation:navigation-dynamic-features-fragment:$navigation_version"
androidTestImplementation "androidx.navigation:navigation-testing:$navigation_version"
implementation "com.google.dagger:hilt-android:$dagger_hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
testImplementation "com.google.dagger:hilt-android-testing:$dagger_hilt_version"
kaptTest "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
androidTestImplementation "com.google.dagger:hilt-android-testing:$dagger_hilt_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
This issue is resolved by updating to version 1.1.0-alpha01 of the hilt navigation compose library.
You can to continue use this library:
androidx.hilt:hilt-navigation-compose:1.0.0
Only you are sure that ViewModel is initialized in a Composable that is root in your activity.
For example:
setContent {
AndroidLearningTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
HeroesScreen()
}
}
}
Your Composable
#Composable
fun HeroesScreen(
viewModel: HeroesViewModel = hiltViewModel()
)
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'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.
I try to add viewModelScope to a basic viewModel but android studio doesn't recognize it.
I tried to change my gradle build file with some solution I found but nothing works.
Here an extract of my build.gradle app
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
When I type viewModelScope in my viewModel it say Unresolved reference: viewModelScope.
for now its in alpha, so please update your gradle to use the following dependencies:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
In my case i forgot to extends ViewModel in that class, the class you use for viewModelScope must be like yourModelClass : ViewModel() in kotlin and for java yourModelClass extends ViewModel
Hope its help
I've had the same issue and I've just imported:
"androidx.navigation:navigation-fragment-ktx:2.2.0-rc03"
"androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-rc03"
Even though I thought fragment-ktx was not really related. Took me a while to figure that out. Hope it helps!
Also check that you are in the correct file. I had the same problem for a moment and I came to this page, but later on, I realized I accidentally tried to run viewModelScope.launch on my Fragment.
viewModelScope.launch is only available in your ViewModels and
lifecycleScope.launch in your lifecycle aware components.
For latest version of the artifact refer
Maven Repository Android Lifecycle ViewModel Kotlin Extensions
In app level build.gradle file add the following :-
def lifecycle_version = "2.2.0-rc03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
Don't forget to add apply plugin: 'kotlin-kapt' at the top of app/build.gradle file
viewModelScope was introduced with release 2.1.0, see here.
Check whether lifecycle-viewmodel-ktx-2.2.0-alpha01.aar is installed. For me there is no error message with the settings you wrote. However, there is an error message when using an earlier version:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0"
But this works:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
Maybe you are not extending the activityViewModel with ViewModel class
class SampleActivityViewModel: ViewModel() {
fun getData(){
viewModelScope.launch{
// Make an API call
}
}
}
Remove below config from build.gradle(:app)
configurations {
all {
exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
}
}
It looks like you've got two different versions of the androidX lifecycle libraries in use.
Change your app/build.gradle to be:
...
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0-alpha01"
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha01"
...
ViewModelScope only added in version 2.2.0, not available on higher versions too. I tried with 2.6.0 but got same error.
In Build.gradle (App Level)
Change your code from:
def lifecycle_version = "2.0.0" or if you are using any lower version than this to:
def lifecycle_version = "2.2.0"
viewModelScope was launched in 2.2.0 version of lifecycle module so you won't find it before that.
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