Unresolved reference: viewModelScope - Android KTX - android

I'm trying to use the new viewModelScope() function provided by the new android ktx library.
In the gradle file, I've added:
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
implementation 'androidx.core:core-ktx:1.0.2'
but when I tied to access the viewModelScope(), I got Unresolved reference: viewModelScope error:
class MainViewModel(application: Application): AndroidViewModel(application) {
fun fetchData(){
viewModelScope.launch{
}
}
}
I don't understand what the problem is. Any idea? Thanks.

Check the release notes https://developer.android.com/jetpack/androidx/releases/lifecycle#declaring_dependencies
viewModelScope is available from v2.1.x
So you need to bump your version in your gradle file.

I have use in my project 2.1.0-beta01:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01"
This is less version then released on official site.
However, I use it because new version 2.2.0-alpha01 still has the Unresolved reference: viewModelScope - Android KTX error.

Related

Room database.withTransaction { } block not Resolved

I am following this tutorial about RemoteMediator and everything is fine until I run into a weird error when using database.withTransaction{} to allow database operations.
Unfortunately, the IDE doesn't seem (or is refusing) to recognize this very genuine and
legit block. I have checked that I defined ROOM Database abstract class correctly and declared these ROOM and Paging 3 libs in build.gradle(app) file.
// Room
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
// Room-paging artifact
implementation 'androidx.room:room-paging:2.4.3'
// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha16'
So I decided to ignore the error and went ahead to call a dao function inside the withTransaction{} block but I get Suspension functions can be called only within a coroutine body.
This is a bit confusing since RemoteMediator's load() override function is already a suspend function.
Anyone with insights on this issue please help as I don't seem to be getting anywhere around this.
Thanks to #ianhanniballake day-saving comment above, if you run into this situation just change the room-runtime flavor from:
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
to room-ktx flavor:
implementation "androidx.room:room-ktx:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
As per Developers Docs ktx-libs provide Kotlin Extensions and Coroutines support which is exactly what database.withTransaction{} block is - it is an extension function on RoomDatabase as shown on this line from the source code.
public suspend fun <R> RoomDatabase.withTransaction(block: suspend () -> R): R {
Android KTX is a set of Kotlin extensions that are included with
Android Jetpack and other Android libraries. KTX extensions provide
concise, idiomatic Kotlin to Jetpack, Android platform, and other
APIs. To do so, these extensions leverage several Kotlin language
features, including the following:
Extension functions
Extension properties
Lambdas
Named parameters
Parameter default values
Coroutines

Koin - Ensure that all "by inject" are valid

I work on an Android project using multiple Project Flavors, and we use Koin to inject the appropriate dependencies based on the current flavor.
We already use the checkModules Gradle task (described here : https://start.insert-koin.io/#/getting-started/testing?id=checking-your-modules) in order to ensure that our dependency tree is valid.
However, it seems that there is a use case missing.
Let's say I want to inject an InterfaceA in my Activity. I would write the following code :
class MyActivity : Activity() {
private val interfaceA_Impl: InterfaceA by inject()
...
}
Koin requires the implementation of InterfaceA to be provided in a module, as such :
val myModule = module {
single<InterfaceA> { MyInterfaceImpl() }
}
In my project, each implementation is "flavor-specific".
My question is :
Is there a way to ensure that ALL by inject targets are valid ? In other words, to ensure that all interfaces that I am trying to inject have valid implementations ? Currently, if an implementation is forgotten, the app crashes during runtime, and I would like to know about it sooner (maybe during unit tests, at the same time checkModules is ran ?)
Thanks a lot !

Upgrade to androidx.appcompat:appcompat:1.2.0 issues

I just updated our Android project to
implementation 'androidx.appcompat:appcompat:1.2.0'
from
implementation 'androidx.appcompat:appcompat:1.1.0'
And suddenly, the app is very slow.
Information about the project:
MVVM architecture
Using dev.b3nedikt.restring:restring:4.0.3
In the Android documentation it says that:
Deprecated AppCompatDelegate.attachBaseContext(). If you are calling or overriding this method, use AppCompatDelegate.attachBaseContext2() instead.
In all activities, I call:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase ?: this)))
}
However, removing it does not seems to solve the problem.
Does anyone have similar issues?

Can't use liveData or viewModelScope.launch

I'm trying to use these two builders for coroutines in my app, but in my ViewModel I can't import them or they do not pop up.
These are my dependencies:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
and in my ViewModel
class MainViewModel(): ViewModel() {
init{
viewModelScope ----> does not work , marked in red
val data = liveData {} ----> does not work, marked in red
}
}
I rebuilt, cleaned, and restarted with invalidating cache, but I can't use them
Add the ViewModel ktx lib:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx$lifecycle_version"
Available after AndroidX lifecycle v2.1.0
for liveData:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$livedata_version"
for viewModelScope:
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
The above additions to build.gradle with cache invalidation and rebuild still were not sufficient to resolve the issue for me (Android Studio Bumblebee | 2021.1.1 Patch 2). Only when I wrote this. I could choose viewModelScope from the pulldown, with automatic import insertion.

Dagger 2 using classes generated by another library

I have a homemade library that generates DataMapper classes.
They are generated with #Singleton and #Inject annotations to be able to inject them where i need them.
But where it doesn't work is when Dagger tries to create the dependency tree, this error shows :
:data:kaptGenerateStubsDebugKotlin
e: /Users/me/myproject/data/build/tmp/kapt3/stubs/debug/com/myproject/data/di/DataComponent.java:11: error: [Dagger/MissingBinding] error.NonExistentClass cannot be provided without an #Inject constructor or an #Provides-annotated method.
public abstract com.myproject.domain.repository.ContentRepository contentRepository();
^
error.NonExistentClass is injected at
com.myproject.data.repository.ContentDataRepository.<init>(…, myGeneratedDataMapper, …)
com.myproject.data.repository.ContentDataRepository is injected at
com.myproject.data.di.module.DataModule.contentRepository(contentDataRepository)
com.myproject.domain.repository.ContentRepository is provided at
com.myproject.data.di.DataComponent.contentRepository()
:data:kaptDebugKotlin
:data:kaptDebugKotlin FAILED
Involved classes are :
DataModule (module for dagger)
#Module
class DataModule {
#Provides
#Singleton
fun contentRepository(contentDataRepository: ContentDataRepository): ContentRepository = contentDataRepository
}
DataComponent (component for dagger):
#Singleton
#Component(modules = [DataModule::class])
interface DataComponent {
fun contentRepository(): ContentRepository
}
ContentDataRepository
#Singleton
class ContentDataRepository #Inject constructor(
private val myGeneratedDataMapper: MyGeneratedDataMapper
) : ContentRepository {
...
}
MyGeneratedDataMapper
#Singleton
class MyGeneratedDataMapper #Inject constructor() {
...
}
The thing is, if i disable kapt of dagger dependency in gradle.build, then build, then enable it, then build, it works.
If i do a clean + build, it doesn't work, same error.
I want to make it work in one row.
I don't know if you are using AS3.2 or AS3.3 with androidX artifacts or not but Maybe this is the case with you too.
so when i migrated to androidX artifacts in AS3.2 i got hit with bunch of NonExistentClass errors ends the build with
kaptGenerateStubsDebugKotlin
:data:kaptDebugKotlin
:data:kaptDebugKotlin
I finally found out that it has something to do with Dagger itself and degraded the version from 2.17 to 2.16 now the latest version of Dagger2 is 2.18 which i can't use due to this bug / feature [they forgot about].
Update:
i found the solution and it just came today so here is the issue tracker link:
https://issuetracker.google.com/issues/115738511
so the bug was not in the Dagger but it was with Jetifier and i totally ignored the fact that it was set enabled during migration
here's the solution i copied from the link:
Sorry jetifier beta01 was not binary compatible with alpha10.
We have published beta02 that should fix this issue.
Please try:
buildscript { dependencies {
classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02' } }
You're probably not going to like my answer but the order is kinda random.
Look at this thread for some more explaining and maybe some more guidance but, if you want to verify you are running first look at Gradle plugins and how to use them

Categories

Resources