镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Default Instance Java Interface Methods are ignored by Binding Tools #7125

Closed

Description

@jedusei

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VS 2022 17.2.4

Description

The binding tools seem to ignore default instance interface methods when generating the C# interface.

For example, for the following Java interface:

package com.example.calculator;
public interface Calculator {
    default int Add(int a, int b) {
        return a + b;
    }
    
    int Subtract(int a, int b);
}

This is the C# interface that is generated:

public interface ICalculator {
    int Subtract(int a, int b);
}

Steps to Reproduce

  1. Create a new file: Calculator.java
  2. Paste the Java interface code written in the description into the file.
  3. Open a terminal in the folder containing Calculator.java
  4. Run javac Calculator.java && jar -cf Calculator.jar Calculator.class. You should see a Calculator.jar file afterwards.
  5. Create a new Android Binding Project and add Calculator.jar to it.
  6. Build the project.
  7. Open the Object Browser (View > Object Browser), click the dropdown at the top of the window, and click My Solution.
  8. Find the binding project in the list and find the ICalculator interface in the Com.Example.Calculator namespace, right-click on it and click Go To Definition.
  9. The interface code will show up, and you'll see that even though there's a Subtract method, no Add method was generated.

Did you find any workaround?

I encountered the issue while creating a binding for the Twilio Conversations Android SDK. The only workaround I could come up with was to manually write the interface method & its invoker implementation.

Relevant log output

No response

Activity

  1. jpobst commented on Jun 24, 2022

    @jpobst
    Contributor

    We never turned this on by default for Classic binding projects. You will need to add the <_EnableInterfaceMembers> property mentioned here:
    https://docs.microsoft.com/en-us/xamarin/android/release-notes/10/10.1#c-80-default-interface-members-for-bindings-projects

    In a .NET 6+ binding this is on by default and can be disabled with:
    https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/OneDotNetBindingProjects.md#static-and-default-interface-members-dim

  2. added
    Area: BindingsIssues in Java Library Binding projects.
    and removed
    needs-triageIssues that need to be assigned.
    on Jun 24, 2022
  3. jedusei commented on Jun 24, 2022

    @jedusei
    Author

    Wow, it worked!
    I wish I had known this earlier. Please consider adding this information to the main Xamarin.Android binding documentation.
    Thank you very much!

  4. ghost locked as resolved and limited conversation to collaborators on Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: BindingsIssues in Java Library Binding projects.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions