Hi, not sure if I should ask here, but for some re...
# amper
g
Hi, not sure if I should ask here, but for some reason I can not use collectAsStateWithLifecycle in JVM app. It works in Android target.
Copy code
16:51:14: Executing 'run -m jvm-app'…

/Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/amper run -m jvm-app
Logs are in file:///Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/build/logs/amper_2025-07-07_16-51-16_86163_run

00:02.801 INFO  :ui:compileJvm            Compiling module 'ui' for platform 'jvm'...
00:06.306 ERROR :ui:compileJvm            e: file:///Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/ui/src/screens/stats/PlayerStatsScreen.kt:21:27 Unresolved reference 'compose'.
00:06.306 ERROR :ui:compileJvm            e: file:///Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/ui/src/screens/stats/PlayerStatsScreen.kt:37:38 Unresolved reference 'collectAsStateWithLifecycle'.
00:06.304 WARN  :ui:compileJvm            w: Language version 2.2 is experimental, there are no backwards compatibility guarantees for new language and library features

ERROR: Task ':ui:compileJvm' failed: Kotlin compilation failed:

e: file:///Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/ui/src/screens/stats/PlayerStatsScreen.kt:21:27 Unresolved reference 'compose'.
e: file:///Users/Giorgi_Shalvashvili/IdeaProjects/Oversearch/ui/src/screens/stats/PlayerStatsScreen.kt:37:38 Unresolved reference 'collectAsStateWithLifecycle'.
16:51:21: Execution finished 'run -m jvm-app'.
Any ideas how to fix it?
fails on this
import androidx.lifecycle.compose.collectAsStateWithLifecycle
line
r
collectAsStateWithLifecycle
is Android only -- it doesn't exist on other platforms.
g
oh, did not know that
but then why would it exist in common main?
z
That function is multiplatform, you can use it in common code. The dependency for it is
Copy code
org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose
r
Oh nice, didn't know that. Here are the docs: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-lifecycle.html. Does this automatically delegate to Google's implementation on Android? The docs are mum on the subject.
z
Yes, in general, all these androidx-related KMP artifacts redirect to Google's published artifacts for Android. I don't think we're publishing anything for Android around Compose at all.
👍 1
That being said, the multiplatform implementation that we publish for other platforms is often just a straight copy of the Android one, if it doesn't touch anything platform-specific, which I don't think something like
collectAsStateWithLifecycle
would, it just relies on other, already multiplatform APIs (coroutines, Compose, core things from Lifecycle)
r
I think
collectAsStateWithLifecycle
does have some platform-specific logic, because "lifecycle" varies on different platforms. The lifecycle docs I linked above give the non-Android platform specifics.
I'm assuming that is just the
LifecycleOwner
implementation though, and everything else is common.
z
The
Lifecycle
implementation itself yes, would be different, I meant that the body of
collectAsStateWithLifecycle
is likely identical from there
👍 1
g
can confirm that adding
org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose
dependency fixed crash on JVM
thanks 📌
👍 2
l
Why isn't that dependency present by default when you do Compose UI?
z
It's one of the many dependencies around Compose, it's not in the regular
ui
package. That being said, we're currently fixing this in the KMP wizard, so that it does include this one by default.