ViewModel: Unresolved reference - android

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"

Related

'by viewModels()' Kotlin property delegate unresolved reference

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.

how to fix unresolved reference lifecycleScope?

Trying to build a simple example that uses kotlin coroutines in an activity:
lifecycleScope.launch {
val result = httpGet("http://hmkcode-api.appspot.com/rest/api/hello/Android")
textView.setText(result)
}
Can't get rid of error "unresolved reference lifecycleScope"
Relevant part of gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.2"
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycle:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
implementation "android.arch.lifecycle:extensions:$androidArchVersion"
}
kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.3'
languageVersion = '1.3'
}
}
As per the Lifecycle KTX documentation, you must include the lifecycle-runtime-ktx artifact if you want Coroutine specific extensions.
Note that lifecycle-runtime-ktx was only introduced in Lifecycle 2.2.0, so you'll need to Migrate to AndroidX, then upgrade to Lifecycle 2.2.0 if you want to use that functionality.
Late response here but I also faced this issue and finally managed to resolve it by changing the inherited Android Activity to an AppCompatActivity.

Butterknife On Kotlin Not Working For Binding Colours And Drawables AndroidX

I still use Butterknife on my Kotlin project but for only binding colours and drawables as there's no need for that on binding views. However, after updating my project to AndroidX I can't get the library to work anymore.
That's what I have
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
kapt 'androidx.databinding:databinding-compiler:3.5.0-alpha02'
kapt 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'
ext.kotlin_version = '1.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
AS 3.3.1
And call them this way
#JvmField
#BindColor(R.color.just_pink)
var pink: Int = 0
#JvmField
#BindDrawable(R.drawable.rectangle_pink_btn_all_radius)
var rectanglePinkAllRadius: Drawable? = null
Making sure I have Butterknife.bind(this, view) on my onCreate method.
Thanks for your help.
In Kotlin you specify the dependencies in a similar to Java way using Kotlin Annotation processing tool (kapt) instead of annotationProcessor.
So replace
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
with this
kapt 'com.jakewharton:butterknife-compiler:10.0.0'

android.arch.lifecycle.ViewModelProviders is not recognized in Kotlin code

For Java project, in order to recognize android.arch.lifecycle.ViewModelProviders, I simply define the following in my build gradle
Java project
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
However, in Kotlin project, I can hardly make it recognizes android.arch.lifecycle.ViewModelProviders. I tried
Kotlin project
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
kapt "android.arch.lifecycle:compiler:$lifecycle_version"
It is still not able to recognize android.arch.lifecycle.ViewModelProviders
The complete build.gradle is as follow - https://github.com/yccheok/AndroidDraw/blob/748baf44571c5b83d0c6e5b6a7137eef9cb17cdc/app/build.gradle
May I know, is there anything else I had missed out?
Using kapt you should also apply plugin: 'kotlin-kapt'.
Please try this, Change in your viewmodel and extension version '1.1.0'
def lifecycle_version = "1.1.0" // change in this line
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"
Note : I have also using this kind of error message, but my problem was solved when I downgrade version 1.1.1 with version 1.1.0.
You need to check downgrade your version number.
Please try this :
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.0,0'
implementation 'androidx.lifecycle:lifecycle-livedata:2.0.0'
This is what I am using right now as I converted to androidX.
Hope this helps.
In my case I also need to remove from gradle.properties these lines:
android.useAndroidX=true
android.enableJetifier=true
I know the question was asked previously when it was not deprecated but for other developer's info. The Class has been deprecated and now you can directly user ViewModelProvider with constructor only.
For Further Info Android Developer

RoomDatabase_Impl does not exist

I when try to implement a Room Database, I get the following error:
java.lang.RuntimeException: cannot find implementation for com.udacity.gradle.builditbigger.Database.HilarityUserDatabase. HilarityUserDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92)
I tried adding the relevant kotlin dependencies to my gradle file (shown below) but when I do, all of my Databinding classes that would normally be generated with any issues are now generating errors in my gradle console. Is there way for me to use the DataBinding library and the Room Pesistence Library?
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
dependencies{
kapt "android.arch.persistence.room:compiler:1.0.0"
}
It did happen to me before, make sure that you have all 3 dependencies in build.gradle
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
kapt 'android.arch.persistence.room:compiler:1.0.0'
Also, a "Project Clean" after gradle synch will help as well.
Make sure kotlin-kapt is included in app-level gradle file.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
and make sure you use kapt instead of annotationProcessor. That solved my problem.
And also check Room Model, DAO, and Database files for #Entity, #Dao and #Database annotations.
For usage of Room, LiveData and ViewModel you need these libraries:
•implementation "android.arch.persistence.room:runtime:1.0.0"
•implementation "android.arch.lifecycle:extensions:1.1.0"
•kapt "android.arch.persistence.room:compiler:1.0.0"
•kapt "android.arch.lifecycle:compiler:1.1.0"
LiveData and ViewModel allows you to use the DataBinding technique.
For more info check the official page: https://developer.android.com/topic/libraries/architecture/adding-components.html
I was facing the same issue, later found that I am not using the #Database annotation for AppDatabase
Use this
#Database(entities = {RowEntity.class, WifiDetailEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase
{ ...
For the newer version of the room compiler don't need to add both dependencies, Just do the following -
kapt 'android.arch.persistence.room:compiler:2.2.3'
As explained above, for ease
1- Add the following in the build.gradle(Module:app) at the very top of the file
apply plugin: 'kotlin-kapt'
2- Then add
//Room for database
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
implementation 'android.arch.persistence.room:runtime:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
3- Sync the file and clean the project. Done
Had a similar problem while doing Codelabs. I was able to solve it by adding the updated Room dependencies on my build.gradle(module).
You can copy and paste the implementation declarations from the Room docs page in the Android Developer site. (Note: Check in your list of dependencies for
implementation 'androidx.room:room-runtime:2.2.5'
or similar, make sure to remove it and only leave the one you copied and pasted, which would look like this
implementation "androidx.room:room-runtime:$room_version"
)
Room | Android Developers #Declaring Dependencies
Add these Dependencies to your build.gradle file
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
kapt 'android.arch.persistence.room:compiler:1.1.1'
MhzDev answer is fine these are the updated version of the dependencies

Categories

Resources