2

I have used Xamarin Native UI for Android application and Created different class library for API call and data Deserialize api data using Newtonsoft.Json.

That class library Target Framework is .NET Standar 2.0.

As I have added that reference to the console application so its working fine but same reference i have add in Android project its throw error.

**Error Details** Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

As per error message i have used attribute JsonConstructor for default constructor of class.

Example:

public class TestClass
{
    [JsonConstructor]
    public TestClass()
    {

    }
}
4
  • What is the Mono Linker setting for the build configuration that you are getting the error with. Commented Oct 4, 2018 at 5:50
  • @SushiHangover Sdk and User Assemblies. Commented Oct 4, 2018 at 5:56
  • Use the PreserveAttribute in Arvinfraja's answer Commented Oct 4, 2018 at 5:56
  • @SushiHangover , As PreserveAttribute required Mono.Android.dll Or 'Xamarin.iOS.dll' reference in that but my class library is Common for both thats why it is not possible. Commented Oct 4, 2018 at 5:59

2 Answers 2

4

As PreserveAttribute required Mono.Android.dll Or 'Xamarin.iOS.dll' reference in that but my class library is Common for both thats why it is not possible

Add a PreserveAttribute class to your class library and use that attribute as the Mono Linker only uses the "name" of the attribute and not the namespace/classname...

public sealed class PreserveAttribute : Attribute
{
    public bool AllMembers;
    public bool Conditional;
    public PreserveAttribute (bool allMembers, bool conditional)
    {
        AllMembers = allMembers;
        Conditional = conditional;
    }
    public PreserveAttribute ()
    {
    }
}

And then use that attribute on your JSON model/class:

[Preserve(AllMembers = true)]
public class TestClass
{
  ~~~
Sign up to request clarification or add additional context in comments.

Comments

1

Try using PreserveAttribute at the top of your class

[PreserveAttribute(AllMembers = true)]
public class TestClass
{   
    public TestClass() {}

}

add one more class PreserveAttribute in class library

public sealed class PreserveAttribute : System.Attribute
{
    public bool AllMembers;
    public bool Conditional;
}

Edit Linking- Sdk & user assemblies

Skip linking assemblies- Newtonsoft.Json;

add Newtonsoft.Json in your Skip linking assemblies option

4 Comments

This is work with Xamarin forms but with Native ui its not work and I have taken separate class library for Android and iOS for Api call and Business logic.
Even I am using PCL with xamarin.android, the same attribute working for me. I got this error before few days,
In Same android project you can do this but if I have different class library for common logic, and in that common library i can't do it.
@ManishSharma - Add one sealed class with name PreserveAttribute in your class library. I forgot to mention it before. If still wont work try to follow my edit section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.