Java – Kotlin: Overloading resolves ambiguity

Kotlin: Overloading resolves ambiguity… here is a solution to the problem.

Kotlin: Overloading resolves ambiguity

Screenshot of the error Try to execute the following code:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

Test().list.contains(1)
    }
}

public class Test {

ArrayList<Integer> list;

public ArrayList<Integer> getList() {
        return list;
    }
}

And the compilation fails at Test().list.contains(1) with the message:

Task :app:compileDebugKotlin FAILED
e: /Users/sreejithcr/Documents/MyApplication/app/src/main/java/com/wxample/myapplication/MainActivity.kt: (13, 31): Overload resolution ambiguity:
public open fun contains(@Nullable element: Int!): Boolean defined in java.util.ArrayList
public open fun contains(@Nullable element: Int!): Boolean defined in java.util.ArrayList

As far as I know, the compiler found 2 contains() with exactly the same signature, but wasn’t sure which one to call.

Gradient configuration:

ext.kotlin_version = ‘1.3.41’

Classpath ‘com.android.tools.build:gradle:3.4.2’

Solution

This is an issue with Android Studios API 29 R2

https://issuetracker.google.com/issues/139041608#comment3

Go to Tools -> SDK Manager -> uninstall Android 9.+, then install it again when Google rolls back R2 so you’ll be back to R1

Related Problems and Solutions