0

When experimenting with the Kotlin language and its "Compose" library, I am encountering problems with some examples (which I looked up on the site of Android, so I suppose the example should be more or less OK).

More precisely, some imports (specified in the examples) yield error messages such as:

Unresolved reference: Card

for the import:

import androidx.compose.material.Card

needed for the following snippet:

@Composable
fun MyCard() {
    Card {
        Text("Card Content")
    }
}

I'm not sure if the error has to do with:

  • The IDE (Android Studio).
  • Some plugin missing.
  • A problem with the libraries.

I would appreciate any advice / hint on how to proceed from here.

For completeness: the other imports relating to Compose are:

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.selection.toggleable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.fillMaxSize

Additional infos:

Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 18, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.4
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin (212-1.6.21-release-334-AS5457.46)

Gradle dependencies (generated by Android Studio):

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation 'androidx.compose.material3:material3:1.0.0-alpha01'
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Serge Hulne
  • 594
  • 5
  • 17
  • 2
    If you asked about that, your question would be on topic for this site, right here. Asking for help finding other resources, however, is off-topic for Stack Overflow. – Tenfour04 Jun 08 '22 at 17:32
  • 2
    Can you add the relevant dependencies to your question? (ie. `androidx.compose.material...`) – Henry Twist Jun 08 '22 at 17:43
  • I saw that you added your imports etc, but can you include your Gradle dependencies? If you haven't added anything to them then that's likely your issue. There's all the information you need in the [Getting Started](https://developer.android.com/jetpack/compose/setup) section of the Compose docs. – Henry Twist Jun 08 '22 at 18:35
  • Add this : implementation "androidx.compose.material:material:$compose_version" – Code Poet Jun 08 '22 at 18:57
  • Yep, as @CodePoet said you don't have the Material dependency to use `androidx.compose.material.Card`, however I would probably recommend sticking with one library, the M3 library should have everything you need (including the M3 `Card`) if you're just playing experimenting. – Henry Twist Jun 08 '22 at 20:36

2 Answers2

1

Update your Compose Material 3 dependency in your build.gradle file to version 1.1.0-alpha02 to use Card

implementation 'androidx.compose.material3:material3:1.1.0-alpha02'

Then all you need to do in your code is to import

import androidx.compose.material3.*

to use all Material 3 components available in your code

0

Have you added @ExperimentalMaterial3Api as per the documentation here? Maybe that is the issue?

Edit: If you are using Material 3, then you need to remove this import:

import androidx.compose.material.Card

Also, for info, the Material 3 dependencies are now at 1.0.0-alpha13:

dependencies {
    implementation "androidx.compose.material3:material3:1.0.0-alpha13"
    implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha13"
}

If you are using Material 2, then you need to add this to your dependencies:

implementation "androidx.compose.material:material:$compose_version"
Code Poet
  • 6,222
  • 2
  • 29
  • 50