javaandroidambiguousappwritekotlin-java-interop

Ambiguous method call when using Appwrite's Client.setEndpoint


I created project on appwrite and to connect it with my project I added dependency to my gradle file : implementation 'io.appwrite:sdk-for-android:8.0.0' and in Java

import android.content.Context;
import io.appwrite.Client;

public class AppWriteProvider {
    public static Client getClient(Context context) {
        Client client = new Client((Context) context.getApplicationContext());

        client.setEndpoint("https://example");

        client.setProject("");

        return client;
    }
}

But it is giving error on setEndPoint method, the error is

Ambiguous method call: both 'Client.setEndpoint(String)' and 'Client.setEndpoint(String)' match.


Solution

  • This seems to be a bug in the Appwrite library (also in the newest version at the time of writing, 8.1.0).

    It was written in Kotlin where the Compiler automatically generates getter and setter functions to enable Java code to access properties. The problem here is with the endpoint property: The auto-generated setEndpoint(String) function conflicts with an already existing setEndpoint(String) function that the developers explicitly added to the class, so you have two versions here, hence the ambiguity.

    Looking at the Kotlin code it seems that this endpoint property shouldn't be public in the first place (at least not the setter). Making it private would prevent the Compiler generating the conflicting function and everything should work.

    But that is nothing you can do (except writing a bug report to the developers). If you do not want to switch with your code to Kotlin as well (there this works without issues because you don't need the Java-interop code), then one possible workaround would be to pass the endpoint to the constructor instead of setting it after the object was instantiated:

    Client client = new Client(context.getApplicationContext(), "https://example");
    

    Then you don't need to call the ambiguous setEndpoint function anymore.