Felix Olszewski
Felix Olszewski

Reputation: 798

Jetpack compose lifecycle viewmodel: -> unresolved reference: compose

I copied over the code from this website to play/pause an audio with a single button which changes icons: https://semicolonspace.com/android-compose-music-button/

First I got this error: Jetpack Compose - Unresolved reference: observeAsState but the first anser of the question resolved it.

Now however, on the following line:

androidx.lifecycle.viewmodel.compose.viewModel()

I still get this error: MainActivity.kt: Unresolved reference: compose code-image

There is nothing I can import anymore and I have copied over the code as is on the website.

I refreshed my build.gradle too, such that it takes effect, but didnt help

EDIT:

Here is a github repo in case you would like to inspect this small project yourself, it's just the MainActitiy file, only a few lines of code like on the link above: https://github.com/folsze/unresolved-reference-compose

Upvotes: 7

Views: 7423

Answers (5)

Empty Brain
Empty Brain

Reputation: 1

In my case , this worked for me

go to Grade Scripts > build.gradle.kts (Module :app)

then add these dependencies

// ViewModel
implementation(libs.androidx.lifecycle.viewmodel.ktx)
// ViewModel utilities for Compose
implementation(libs.androidx.lifecycle.viewmodel.compose)
// LiveData
implementation(libs.androidx.lifecycle.livedata.ktx)
// Lifecycles only (without ViewModel or LiveData)
implementation(libs.androidx.lifecycle.runtime.ktx)
// Lifecycle utilities for Compose
implementation(libs.androidx.lifecycle.runtime.compose)

// Saved state module for ViewModel
implementation(libs.androidx.lifecycle.viewmodel.savedstate)

after adding the dependencies it will ask for sync then click on sync now

Upvotes: 0

Sahil Gupta
Sahil Gupta

Reputation: 1

if You are having a similar error in implementing dependencies then you may be need to add the following code in setting.gradle.kts in dependencyResolutionManagement

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            library("retrofit2-retrofit", "com.squareup.retrofit2", "retrofit").version("2.9.0")
            library("androidx-lifecycle-viewmodel-compose", "androidx.lifecycle", "lifecycle-viewmodel-compose").version("2.7.0")
            library("retrofit2-converter-gson", "com.squareup.retrofit2", "converter-gson").version("2.9.0")
            library("coil-kt-coil-compose", "io.coil-kt", "coil-compose").version("2.5.0")
        }
    }
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

here's a picture for the same [1]: https://i.sstatic.net/4aqTNZVL.png

Upvotes: 0

Shatil Ahmed
Shatil Ahmed

Reputation: 1

Import the above dependency to solve your problem "implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")"

Upvotes: -1

rjhcnf
rjhcnf

Reputation: 1057

Make sure that your file extensions for all files correct (e.g. build.gradle does not have .kt etc)

Upvotes: 0

Francis Mwangi
Francis Mwangi

Reputation: 61

What really worked for me is that first in the build.gradle file app level I have added the following dependancy

implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"

I have also made sure the androidx.lifecycle:lifecycle-runtime-ktx is version 2.4.1 which wasn't the case

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'

Here is a repo incase of further inspection https://github.com/waithakaFM/Restaurants

Upvotes: 6

Related Questions