The last couple of weeks I have tried to create a ...
# multiplatform
m
The last couple of weeks I have tried to create a KMP project using compose, material3 and viewmodel. I tried and tried but everytime I got a compile error saying that the
viewModel {..}
was not available. If you search for these terms you get all kinds of pages and this page https://developer.android.com/jetpack/androidx/releases/lifecycle#2.8.0 looked promising. However the gradle dependencies did not work for me. (Damn enter published my message too soon!). Today I finally got it working but still I am confused. The solution for me was two wrong dependecies apparently. Changing:
Copy code
lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycleViewModel" }
lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycleViewModel" }
into
Copy code
lifecycle-runtime-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycleViewModel" }
lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycleViewModel" }
Both dependency definitions are valid but apparently different. Can someone explain? It would also be nice if this is documented somewhere for absolute beginners in the KMP world to get properly started (or point me to documentation which I could not find anywhere 😄 ) Hopefully this also helps others.
m
@Ivan Matkov Thx, I found that documentation after a lot of detours. I just wanted to point out to others that the difference in androidx.lifecylcle vs org.jetbrains.androidx-lifecycle might not always be that straightforward. Can you explain why there even is the androidx in that dependency? There might be a good reason, but it is very very confusing. is there a reason why it hasn´t been named something like org.jetbrains.kmp.lifecycle?
i
androidx - Google library org.jetbrains.androidx - JetBrains multiplatofrm adoption. redirects to Google's binary on platforms where available. androidx is a root package that is far too late to change
m
I guess I got confused by this article: https://touchlab.co/kmp-viewmodel because this says the jetpack viewmodel has KMP support.
i
It really does. But not all platforms
m
BTW I think it is never too late to rename packages. It you really want to have generic support eventually the name androidx will put off people from adopting KMP and that is a shame. It is really a promising technology.
And thx for explaining the difference between the packages, it is more clear now.