I am working on Android ViewModel architecture component but I am getting the above mentioned error when trying to initialize a ViewModel in an AppCompatActivity.
import android.arch.lifecycle.ViewModelProviders;
ViewModelProviders.of(this).get(CounterViewModel.class);
There are a few questions and articles related to this, and they pointed towards adding the lifecycle:extensions and lifecycle:viewmodel dependencies in the app gradle file, but I am still getting the error.
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
The package android.arch.lifecycle does not contain the class ViewModelProviders and it only has ViewModelProvider class.
What else needs to be added to access the ViewModelProviders class?
Edit :
Dependencies in app/build.gradle:
dependencies {
implementation project(':lifecycle')
implementation project(':base')
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}
android.arch.lifecycle:extensions:1.1.1 definitely has android.arch.lifecycle.ViewModelProviders. You can see it in Android Studio, if you open the "External Libraries" portion of the project tree and examine the library contents:
Some possible reasons for not finding the import include:
You have implementation "android.arch.lifecycle:extensions:1.1.1" in the wrong place (it should be in the dependencies closure of the module's build.gradle file, such as app/build.gradle)
You did not sync Android Studio with the Gradle build files (you are normally prompted to do this, but you can do it manually from File > Sync Project with Gradle Files from the Android Studio main menu)
You do not need both lifecycle:extensions and lifecycle:viewmodel in your build.gradle file, remove
implementation "android.arch.lifecycle:viewmodel:1.1.1"
and it should be fine now. Also, you may want to migrate to AndroidX and use the 2.0.0 versions of the library.
ViewModelProviders is now deprecated. Use ViewModelProvider instead.
If you are configuring in libary, you can modify the implementation to api
Related
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 have a module called "Common" as library, this module has few dependencies like: com.badlogicgames.gdx, com.squareup.wire etc. And it works fine, I use them inside of this module.
And I have another module called "Tracking", where in the gradle I have:
dependencies {
compile project(':Common')
}
And if I try there to import any public class of module "Common", it works fine, but if I try to import any class of library com.badlogicgames.gdxor com.squareup.wire, it says me "Cannot resolve symbol ..." and hightlight it red. And no code autocompleting for such classes.
However the project compiles and starts on the device without errors.
Has somebody any idea? I tried "clean and rebuild" for project, "invalidate cashes and restart" for Android Studio. Nothing helps.
in the module common you need to declare those transitive dependencies as api to expose them to other modules:
e.g. common/build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
api 'com.squareup.wire'
}
https://jeroenmols.com/blog/2017/06/14/androidstudio3/
Solution
Change compile to api
dependencies {
api project(':Common')
}
Reason
Because compile is deprecated, so it is been treated as implementation.
FYI compile and api (new keyword for compile) are same in which all internal modules are visible.
But new gradle project having compile keyword are treated as implementation. and in implementation internal modules are not visible to main project.
Suggestion
You should declare dependency in your gradle because it is not good to make leak of internal modules.
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
I am using Android Room Persistence library (v.1.0.0-alpha1) in my app.
Although it is working fine, when I open model class (Kotlin Data class) in Android studio, it shows Unresolved reference for all annotations used for Room database like #Entity, #ColumnInfo etc. I tried changing version of arch library to 1.0.0-alpha5 but result was same.
In Lint inspection it is showing Remove deprecated symbol import for all imported annotations.AS was not showing this error previously.
How can I resolve this issue
Edit
Following are imports I have added in my build.gradle
compile "android.arch.persistence.room:runtime:1.0.0-alpha5"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha5"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha5"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha5"
Here you have an example.
https://github.com/jsperk/PocRoom
Remember, you need add:
Gradle (Project)--> maven
Gradle (Module App) dependencies -->
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
testImplementation "android.arch.persistence.room:testing:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
In my project, I have this question cause I'm using
android.arch.lifecycle:livedata:1.1.1
than no matter using room with version 1.1.1 or 1.0.0, it still can't find the android.arch.persistence.room.Entity.
I've searched for a long time, till I found that, when I delete the LiveData implementation, the problem solved. Then I notice that, the version of these two libraries conflict. At last, I use the same version of 1.1.0 for livedata and room (since livedata have no version of 1.0.0), and solved it.
def arch_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$arch_version"
annotationProcessor "android.arch.persistence.room:compiler:$arch_version"
implementation "android.arch.persistence.room:rxjava2:$arch_version"
implementation "android.arch.persistence.room:common:$arch_version"
implementation "android.arch.lifecycle:livedata:$arch_version"
implementation "android.arch.lifecycle:extensions:$arch_version"
Adding these dependencies to your Gradle(Module App) file will solve it:-
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
testImplementation "android.arch.persistence.room:testing:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
Why am I getting this error? My repository and Google play services are up to date and I've used all the requires steps for using Firebase like copying firebase code in both build.gradle(Project and app). The intellisence doesn't even show FirebaseAuth but displays other members of Firebase.
Solved the error by adding this to the build.gradle file(for app) -
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
The 2nd dependency is to solve the version conflict error.
Add this dependency to Gradle.Build(Module:App)
compile "com.google.firebase:firebase-auth:9.0.2"
then sync with gradle :)
Add these two dependencies into your build.gradle
as 'compile' is replaced by 'implementation'
dependencies {
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation "com.google.android.gms:play-services-gcm:16.0.0"
}
***For the latest android studio version 3.0 and new ones ***
Same problem. Clean your project with "Build"->"Clean Project".
add the following to the build.gradle (app)
implementation 'com.google.firebase:firebase-auth:9.2.1'
For me this worked with all the firebase extensions:
eg.:
Android studio adds the following line to your gradle file:
implementation 'com.google.firebase:firebase-auth:16.0.1:15.0.0'
You have to change it to:
implementation 'com.google.firebase:firebase-auth:16.0.1'
and add:
kapt 'com.google.firebase:firebase-auth:16.0.1:15.0.0'
I have android studio 3.3.1 installed. Compile is replaced with implementation. So you have to write
implementation 'com.google.firebase:firebase-auth:16.1.0'
You should replace version according to the warning you get.
on dependencies in app gradle
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
this is useful android 3.0 and onwards versions auth:16.0.1 and
core:16.0.1Firebase will work
Solved the error by adding this to the build.gradle file(for app) -
compile 'com.google.firebase:firebase-auth:9.2.1'
compile "com.google.android.gms:play-services-gcm:9.2.1"
You may need to make sure that your activity is importing the Firebase Auth module. In your .java file (eg. Login.java):
import com.google.firebase.auth.FirebaseAuth;
Please add the below line in your build.gradle file:
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation "com.google.android.gms:play-services-gcm:10.2.1"
In my case, I was using firebase-auth and firebase-messaging with different versions. So After getting an error I kept the same version for both e.g.
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
After sync, my problem was solved. try to keep the version the same for firebase libraries
implementation 'com.google.firebase:firebase-auth:16.0.1'
make sure your firebase and all the dependency are the same versions.