I was using lifecycle version 2.3.1 but wanted to upgrade to 2.4.0
While doing so, I realized that now the imports for kotlinx.coroutines.launch, kotlinx.coroutines.Dispatchers and kotlinx.coroutines.withContext lead to unresolved references.
Below are my current dependencies:
dependencies {
def lifecycle_version = "2.4.0"
def arch_version = "2.1.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// ViewModel utilities for Compose
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - helpers for implementing LifecycleOwner in a Service
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
// optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$arch_version"
}
And an example of a ViewModel that's not working:
import android.util.Log
import android.util.Patterns
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.psy.myapplication.R
import com.psy.myapplication.auth.model.User
import com.psy.myapplication.auth.register.data.remote.RemoteRegisterDataSource
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.psy.myapplication.core.Result
val TAG: String = "REGISTER"
class RegisterViewModel(private val dataSource: RemoteRegisterDataSource) : ViewModel() {
private val mutableRegisterFormState = MutableLiveData<RegisterFormState>()
val registerFormState: LiveData<RegisterFormState> = mutableRegisterFormState;
private val mutableRegisterResult = MutableLiveData<RegisterResult>()
val registerResult: LiveData<RegisterResult> = mutableRegisterResult
fun register(email: String, password: String) {
viewModelScope.launch {
Log.v(TAG, "Launched register")
val result = withContext(IO) {
dataSource.register(User(email, password))
}
if (result is Result.Success){
mutableRegisterResult.value = RegisterResult(success = result.data)
}
else if (result is Result.Error) {
mutableRegisterResult.value = RegisterResult(error = result.exception.message)
}
else {
mutableRegisterResult.value = RegisterResult(defaultError = R.string.register_error)
}
}
}
}
In the kotlin code, I get errors while trying to call viewModelScope.launch and withContext(IO), getting unresolved references for launch, withContext and Dispatchers.IO
EDIT: Ok, so apparently the errors are not actual errors because the app still compiles and runs fine, it just shows me that there are errors. Invalidating caches, restarting, clean & rebuild, all of these didn't work.