Skip to content

C# to Java wrong translation for constructor #3490

Description

@EmilAlipiev

I am trying to create Samsung Tizen Companion app in Xamarin.Android. Samsung has an SDK called Samsung Accessory SDK. This is only available in jar. So I created a Java Binding Library.
Samsung also has a sample code in java how to handle communication between Tizen and Android (in this case xamarin.android).
So I tried to convert below attached java class(ProviderService.txt) into C# see the url
ProviderService.txt

Problem here is

below code constructor in C#


[Service(Exported = true, Name = "WearCompanion.Droid.ProviderService")]
    public class ProviderService : SAAgent 
    {   

        public ProviderService() : base("ProviderService", SASOCKET_CLASS)
        {

        }

when I look at the obj folder of my xamarin.android project and find ProviderClass.java file generated

public class ProviderService
    extends com.samsung.android.sdk.accessory.SAAgent
    implements
        mono.android.IGCUserPeer
{

    public ProviderService (java.lang.String p0, java.lang.Class p1)
    {
        super (p0, p1);
        if (getClass () == ProviderService.class)
            mono.android.TypeManager.Activate ("WearCompanion.Droid.ProviderService, WearCompanion.Android", "System.String, mscorlib:Java.Lang.Class, Mono.Android", this, new java.lang.Object[] { p0, p1 });
    }

but it is supposed to be interpreted as below java code

public class ProviderService extends SAAgent {
    private static final String TAG = "HelloAccessory(P)";
    private static final Class<ServiceConnection> SASOCKET_CLASS = ServiceConnection.class;

    public ProviderService() {
        super(TAG, SASOCKET_CLASS);
    }

in C# obviously no difference between 2 code pieces but in Java i think that there is difference because. Sdk request is failing with error telling me that

java.lang.InstantiationException: java.lang.Class has no zero argument constructor

Repro:
https://github.com/EmilAlipiev/SamsungWearCompanion

Activity

  1. jpobst commented on Aug 20, 2019

    @jpobst
    Contributor

    How do you reproduce the java.lang.InstantiationException error? When I run your repro, I get a NullReferenceException here:

    https://github.com/EmilAlipiev/SamsungWearCompanion/blob/master/WearCompanion/WearCompanion/MainPage.xaml.cs#L48

    provider = DependencyService.Get<IProviderService>();
    provider.FindPeers(entry.Text);
    

    This is because the DependencyService cannot find anything that implements IProviderService. In order for it to find your ProviderService class the class needs to implement IProviderService.

    That is, this:

    public class ProviderService : SAAgent
    

    needs to be this:

    public class ProviderService : SAAgent, IProviderService
    
  2. added
    need-infoIssues that need more information from the author.
    on Aug 20, 2019
  3. EmilAlipiev commented on Aug 20, 2019

    @EmilAlipiev
    Author

    @jpobst thanks for looking into this issue first of all.
    it is a bit complicated use-case, i will try to explain here step by step. so basically,

    1. I try to communicate samsung galaxy watch and Xamarin.Android companion app.
      my repo project is the companion project installed on my phone. when you install this project, it is automatically opening a listener socket. Nothing needs to be done. So that button on Mainpage.xaml isnt needed at all. It is called Provider app.

    2. there is the consumer app also which is tizen.net application installed on the smart watch. Samsung has a great job for that and they have even simple hello world sample here as a zip. They explain also how it works between phone and watch. mine is number 1, Provider (Android) and Consumer (Gear).

    3. so i downloaded this HelloAccessory_.NET.zip (838KB) and opened with VS, VS Can be connected using Tizen Device manager. and once it is installed on the watch, there is "connect" button appears as shown in the image. I click on "connect" and request is sent to the phone over the bluetooth but it crashes with this exception message.

    For you to test it; You dont really need an actual Samsung Watch. there is emulator.

    • you need to install tizen sdk, tizen visual studio extension and enable a watch emulator which will appear after installing the extension and sdk.

    I created same ticket on Samsung developer portal as a support ticket. Samsung replied as they are reviewing but unfortunately so far no reply.
    I believe that error is on xamarin side but Samsung Sdk is also not optimized. if they could enabled a parameterless constructor in their java sdk, it would solve the problem but probably for Java it isnt required for C# seems to be yes.

  4. jpobst commented on Aug 20, 2019

    @jpobst
    Contributor

    With my noted change, the ProviderService can be created and called, so I don't think that class is the problem. Can you post a full stacktrace for your exception?

  5. jonpryor commented on Aug 20, 2019

    @jonpryor
    Contributor

    Our "Java Callable Wrapper" generation does not support reading IL to determine what base(...) arguments were provided to create a Java equivalent. It only supports forwarding parameters.

    What you can do, to provide a default constructor, is use ExportAttribute.SuperArgumentsString, e.g.

    partial class ProviderService : SAAgent 
    {
        [Export (SuperArgumentsString="\"ProviderService\", ServiceConnection.class")]
        public ProviderService() : base("ProviderService", SASOCKET_CLASS)

    This allows an appropriate Java-side default constructor to be generated, but at the cost of additional manual effort to ensure that your C# code and Java code are consistent with each other.

  6. EmilAlipiev commented on Aug 20, 2019

    @EmilAlipiev
    Author

    @jonpryor thank you. this was really helpful. I could make it working with the link you shared. Documentation explains it really well. Such a shame that my google search didnt end up on this page.
    I had to add Mono.Android.Export.dll reference, is there any disadvantage or performance downside using this dll. I would like to know because It will effect my main android app. so I have reevaluate if there is any cost.

  7. jonpryor commented on Aug 22, 2019

    @jonpryor
    Contributor

    I had to add Mono.Android.Export.dll reference, is there any disadvantage or performance downside using this dll.

    On methods, there is currently a performance disadvantage. On constructors, there is not.

  8. removed
    need-infoIssues that need more information from the author.
    on Aug 22, 2019
  9. ghost locked as resolved and limited conversation to collaborators on Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions