Hello everyone, I'm going through <getting started...
# getting-started
i
Hello everyone, I'm going through getting started with KMP and stuck on step Introduce a view model in 5th section. Dependency is not found:
Copy code
implementation(libs.androidx.lifecycle.viewmodel.compose)
How to fix it? Sorry for stupid question, but I've spent too much time on that already.
c
Hey, can you check that you have a line that looks like
Copy code
androidx-lifecycle-viewmodel-compose = { … }
in the file
gradle/libs.versions.toml
? It should be in the
[libraries]
section.
The "Gradle project sync failed" is probably the source of your issue; when the IDE cannot communicate with Gradle, weird things tend to happen. You should have more info on what went wrong in the "Build" toolbar on the bottom left of IntelliJ/Android Studio.
i
@CLOVIS thanks for quick reply. I've checked libs.versions file and I don't have that line:
Copy code
Executing tasks: [:composeApp:assembleDebug, :composeApp:assembleDebugUnitTest, :composeApp:assembleDebugAndroidTest] in project /Users/iuniriakupov/git/KotlinProject

Type-safe project accessors is an incubating feature.

> Configure project :composeApp
e: file:///Users/iuniriakupov/git/KotlinProject/composeApp/build.gradle.kts:38:42: Unresolved reference: lifecycle
w: Missing 'androidTarget()' Kotlin target in multiplatform project 'composeApp (:composeApp)'.
The Android Gradle plugin was applied without creating a corresponding 'android()' Kotlin Target:
plugins { id("com.android.application") kotlin("multiplatform") } kotlin { androidTarget() // <-- please register this Android target }
Copy code
FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file '/Users/iuniriakupov/git/KotlinProject/composeApp/build.gradle.kts' line: 38

* What went wrong:
Script compilation error:

  Line 38:             implementation(libs.androidx.lifecycle.viewmodel.compose)
                                                    ^ Unresolved reference: lifecycle

1 error

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at <https://help.gradle.org>.
==============================================================================

2: Task failed with an exception.
-----------
* Where:
Build file '/Users/iuniriakupov/git/KotlinProject/composeApp/build.gradle.kts' line: 38

* What went wrong:
Script compilation error:

  Line 38:             implementation(libs.androidx.lifecycle.viewmodel.compose)
                                                    ^ Unresolved reference: lifecycle

1 error

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at <https://help.gradle.org>.
==============================================================================

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to <https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings> in the Gradle documentation.

BUILD FAILED in 1s
Not sure what is useful there
c
That's probably your problem 😕 The latest version would be
Copy code
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version = "2.7.0" }
but I have no idea which version the sample is written for. You can find the full list here: https://androidx.tech/artifacts/lifecycle/lifecycle-viewmodel-compose/
* What went wrong:
Script compilation error:
Line 38: implementation(libs.androidx.lifecycle.viewmodel.compose)
^ Unresolved reference: lifecycle
1 error
This is the interesting part 🙂 But well, it's not teaching us much except that IntelliJ and Gradle agree on what's wrong, which probably means the only issue is the missing lifecycle dependency.
i
Thanks a lot
c
I don't know much about AndroidX lifecycle, so if that version doesn't work I'm afraid I won't be able to help much more. In that case, probably ask a more specific question in #compose-android. If that does fix it, please report it to https://kotl.in/issue, it's not great that the sample has missing dependencies 😅
i
Sure. Thanks again. Have a good evening.
🙏 1
j
Did you add this in commonMain or androidMain deps block? Because Androidx viewmodel not supported in KMP, only Android.
i
@Joel Denke ./KotlinProject/composeApp/build.gradle.kts
j
Yeah sorry missed that part. How does Toml file look now?
i
After manually adding the line with androidx-lifecycle-viewmodel-compose
Copy code
[versions]
agp = "8.2.2"
android-compileSdk = "34"
android-minSdk = "24"
android-targetSdk = "34"
androidx-activityCompose = "1.8.2"
androidx-appcompat = "1.6.1"
androidx-constraintlayout = "2.1.4"
androidx-core-ktx = "1.12.0"
androidx-espresso-core = "3.5.1"
androidx-material = "1.11.0"
androidx-test-junit = "1.1.5"
compose = "1.5.4"
compose-compiler = "1.5.8"
compose-plugin = "1.6.0-alpha01"
junit = "4.13.2"
kotlin = "1.9.21"
ktor = "2.3.7"
logback = "1.4.14"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-material = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version = "2.7.0" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material = { module = "androidx.compose.material:material", version.ref = "compose" }
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor" }
ktor-server-tests = { module = "io.ktor:ktor-server-tests-jvm", version.ref = "ktor" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
BTW, after that fix it's not complaining anymore about gradle file, but still cannot build:
j
Have you tried rebuild app? Sometimes need reset all in Gradle and rebuild. In your case looks like also having additional errors in App.kt. sometimes one error can trigger chained ones.
i
I've decided to try again this tutorial from very beginning and found mismatch of paths, probably this is root cause. Here it refer to
App.kt
file in
composeApp/src/androidMain/kotlin
But in fact it's not there. It's in the
composeApp/src/commonMain/kotlin
c
Did moving
App.kt
fix it? If not, can you show us the contents of
App.kt
?
j
Also in the ticket you say something else than saying here it sounds like. From what I can see youre trying to create a ViewModel with android target required, and try using it in commonMain, thats not possible, because androidx viewmodel not supported in KMP, only in Android targets.
To solve that need to create an actual/expect ViewModel in commonMain like expect class MyViewModel and then only inherit from androidx.viewModel.ViewModel in androidMain. Then you can use it in commonMain from App 🙂
I did read through the guide again now and oboy its confusing 😄 You get App.kt in commonMain from the wizard, but guide for Android says create App.kt in androidMain. @Iunir Iakupov If you would like to I can help you in DM, I think I need quite a lot of input not fit into a thread here 😛
i
Moving
App.kt
fixed it. Thank you all for helping me!
j
No worries, just glad I could help 🙂 Quite tricky tutorial.
r
I have had de very same problem
After monkey typing like a madman, I found the unresolved reference is related to the android gradle plugin (AGP) version
just check the contents of the
project_path/gradle/libs.versions.toml
, I got the project compiling/running after downgrading agp version from "8.2.0" to "8.1.4"
hope this helps