0

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

Chef Lax
  • 89
  • 2
  • 10
  • You need special dependency in `build.gradle`: `implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1'`. – ConstOrVar Dec 19 '22 at 20:24
  • Note please, that instead of manually hard code versions in `build.gradle`, the best way is to use [bom file](https://developer.android.com/jetpack/compose/setup#using-the-bom) – ConstOrVar Dec 19 '22 at 20:25
  • You didn't add view model related dependencies. See this page: https://semicolonspace.com/gradle-files-jetpack-compose/ These are the gradle files used in the project (music button) – SemicolonSpace Dec 20 '22 at 06:13

1 Answers1

1

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