I am trying to implement java library into xamarin. For that I created a java binding library and referenced dll into my xamarin.android project. Basically JBL generated code looks like as below. as you can see that it has no constructor with 0 parameters.
public abstract class SAAgent : Service
{
[Register(".ctor", "(Ljava/lang/String;)V", "")]
protected SAAgent(string p0);
protected SAAgent(IntPtr javaReference, JniHandleOwnership transfer);
[Register(".ctor", "(Ljava/lang/String;Ljava/lang/Class;)V", "")]
protected SAAgent(string p0, Class p1);
I implemented a class inheriting this generated SAAgent class.
[Service(Exported = true, Name = "WearCompanion.Droid.ProviderService")]
public class ProviderService : SAAgent
{
public ProviderService() : base("ProviderService", SASOCKET_CLASS)
{
}
when I execute the code, I receive an exception telling that
Java.Lang.RuntimeException Message=Unable to instantiate service WearCompanion.Droid.ProviderService:
java.lang.InstantiationException: java.lang.Class has no zero argument constructor
so it is expecting me to have 0 argument constructor. There is a sample on Samsung Tizen website and their java sample looks like as below
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);
}
But when I look at the obj folder of my xamarin.android project and find ProviderClass.java file generated. it looks like as below,
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 });
}
I dont understand how can I make that xamarin produces a java class from c# with 0 constructor? Is that a java binding problem?
PS, I have decompiled jar to see SAAgent java implementation and it is the same as C# one. so java binding library did a correct job. I am not sure if I should manually add a parameterless constructor myself?
Finally i found the solution. Xamarin actually has in their documentation regarding missing constructor. as explained, it should be used a Export attribute. So if i change my C# class constructor as below. It generates exact java class as expected with zero parameter constructor.
[Export(SuperArgumentsString = "\"ProviderService\", ProviderService_ProviderServiceSocket.class")]
public ProviderService() : base("ProviderService", SASOCKET_CLASS)
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.