Search code examples
androidkotlinandroid-studiooverloadingambiguity

Kotlin, Android Studio. "Overload resolution ambiguity. All these functions match" on Text(...)


I've recently started to learn app development in Android Studio and Kotlin so I am a total beginner but I learn fast. However, I stumbled on this error: "Overload resolution ambiguity. All these functions match". I have only one file MainActivity.kt in which I defined 2 Composable functions ItemOrder and UpperPanel. The problem is when I try to call Text(...) function in both of these 2. I don`t know why the error appears and searched online but none of the related answers really solved my problem. I repeat I am a beginner so I might do a rookie mistake somewhere. I attach a snippet of the error below. enter image description here

I also have to mention that I import only this Text instance : "import androidx.compose.material3.Text" so to my bigger surprise I don't understand how the conflict appears even though I have only one import in a single file. Thanks to everyone in advance.

P.S.: I can also attach the code if necessary but I thought I could save space.

P.S.S.: I've just noticed that the previews are ok if I change the value of one parameter of a Text(...) constructor, even though the actual Text word is underlined with red giving the error I've just shown. So I think I can continue to code, but I am still wondering what the problem is or, if the case, how to disable the red underlines.

Later update: I managed to get rid of the red underlines of the Text constructor by specifying the onTextLayout, even if empty, in the parameter list. However, now the problem is at runtime when I try to run the app. As Beto Caldas suggested, it is indeed a problem related to duplicated classes found and even though I can't see it directly by inspecting the list of imports (I have only one file) after further digging I opened the Navigate -> Class window and found out it is about the coexistence of the packages he mentioned. But, for the moment, I don't know how to eliminate the second.

Problem solved. I looked into the gradle scripts and removed the second import in Beto Caldas' answer.


Solution

  • Check if you have those both imports

    { group = "androidx.compose.material3", name = "material3" }
    

    and

    { group = "androidx.compose.material3", name = "material3-android", version.ref = "material3Android" }
    

    Remove the second.

    Are you reading compose documentation and created a new project to test it? If so, you will have a problem with documentation cuz the documentation was done with Material 2 as target and your new project is targeting Material 3. This migration document can help https://developer.android.com/jetpack/compose/designsystems/material2-material3