Reputation: 2448
Must be a very simple solution, but...
I'm using StateFlow
in my android project to replace LiveData
that were used as Observables in a Service. I used LiveData as observables for Room Database
but the limitations regarding thread (must register observer and notify on main thread) made me switch to Flow
class. Everything fitted nicely and logically, and to keep info about state, I then decided to use StateFlow
.
I added
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
and
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions.freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"] }
in my gradle file and
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
on top of the classes that had StateFlow member types. but I get a crash when I run the project stating:
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/flow/StateFlowKt; .... Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.flow.StateFlowKt" on path: DexPathList[[zip file "/data/app/com.xxxxx.consumer-X46raHzqXUeRRH40JT1LUg==/base.apk"],nativeLibraryDirectories=[/data/app/com.xxxxx.consumer-X46raHzqXUeRRH40JT1LUg==/lib/arm64, /data/app/com.xxxxx.consumer-X46raHzqXUeRRH40JT1LUg==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64]] ....
What steps are necessary to add StateFlow in an Android Project using Kotlin?
Thanks!
Upvotes: 4
Views: 3453
Reputation: 1007584
You have multiple modules, and your library module using StateFlow
apparently is not distributed via an artifact repository. As a result, the app module consuming that module is not getting transitive dependency data, so it has no way of knowing that your library module wants a particular version of coroutines.
IMHO, ideally, you would use an artifact repository (e.g., mavenLocal()
) and have the library module use an api
dependency for the coroutines library.
If you would prefer not to mess with that, any module consuming that library should also have the coroutines dependency, with a matching (or at least compatible) version.
Upvotes: 4