Reputation: 1884
How do I import androidx.lifecycle.viewmodel.compose.viewModel
on Kotlin Multiplatform (iOS, web, desktop, Android)?
Attempts:
[versions]
androidx-activityCompose = "1.10.0-alpha02"
androidx-compose-runtime = "1.8.0-alpha03"
androidx-lifecycle-viewmodel = "2.9.0-alpha04"
lifecycleViewmodel = "2.9.0-dev1873"
[libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle-viewmodel" }
androidx-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "androidx-compose-runtime" }
jetbrains-lifecycle-viewmodel = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycleViewmodel" }
lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "androidx-lifecycle-viewmodel" }
Then in kotlin.sourceSets.commonMain.dependencies
of build.gradle
:
runtimeOnly(libs.androidx.runtime)
runtimeOnly(libs.androidx.lifecycle.viewmodel.compose)
runtimeOnly(libs.lifecycle.viewmodel)
implementation(libs.jetbrains.lifecycle.viewmodel)
Unfortunately that doesn't work:
Upvotes: 1
Views: 183
Reputation: 299
All my projects use this import statement now, instead of the one you're using
import androidx.lifecycle.ViewModel
But I'm not even seeing the lifecycle dependency in my toml
files anymore. Is it possible you are missing the compose compiler and JetBrains Compose plugins that automatically add it?
Toml file:
[plugins]
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
androidApplication = { id = "com.android.application", version.ref = "agp" }
Build.Gradle file:
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.androidApplication)
}
Sorry for any typos. Write this on my phone to avoid opening the laptop tonight.
Upvotes: 0
Reputation: 1884
Reusing lifecycleViewmodel = "2.9.0-dev1873"
, I was able to add:
lifecycle-viewmodel-compose = { module =
"org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose",
version.ref = "lifecycleViewmodel" }
(with implementation(libs.lifecycle.viewmodel.compose)
in my kotlin.sourceSets.commonMain.dependencies
)
Upvotes: 0
Reputation: 1
It looks like your dependency has an typo. "kotlin.sourceSets.ommonMain.dependencies" of build.gradle
Upvotes: -1