Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Is it possible to preserve ALL constructors when using Linker?

AlexKeyAlexKey USMember ✭✭

It looks like most of the issues with the Linker related to the fact that it removes constructors which are necessary to create objects via reflection. There is of course a chance that an instance method needs to be preserved to be called via reflection as well but the majority is dynamic creation of objects. Dependency services, serialization and many other things rely on that. I think the overhead will not be huge if the Linker preserves all the constructors for the types which are identified to be referenced anywhere in the code. Is is possible to do it? Maybe there are options to the Linker which control this behavior?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    There's no such preserve as I knew.
    We could preserve a certain class using attribute:
    https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker#preserving-code
    like:

    [Preserve(AllMembers = true)]
    

    We could also preserve all the assembly using skipping:
    https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker#linkskip
    Linker will help us optimize code if they haven't been used. If you only want to preserve the constructor I'm afraid you need to set the attribute one by one.

  • AlexKeyAlexKey USMember ✭✭

    In this case is there a good documentation on the linker.xml file. I saw a few examples but I didn't find a complete documentation and a schema. For example, if I write

    <linker>
       <type fullname="*">
          <method name=".ctor" />
       </type>
    </linker>
    

    It does not complaint but it looks like it doesn't do anything. Is this a valid approach without specifying an assembly? Generally speaking this is what I want, to preserve all constructors in all types. I, also, tried to enclose "type" tag into "assembly" tag with "*" fullname but in this case it threw an exception "Unable to find * assembly.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you mean the custom linker configuration:
    https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/linker
    Follow it to build your own linker architecture and do not forget to change the build action to LinkDescription.

  • AlexKeyAlexKey USMember ✭✭

    I saw this documentation page. What I was surprised with is when I wrote a linker file the way I showed in the example the linker didn't complaint, it didn't say, for example that I cannot use "type" tag directly under the "linker" tag. That confuses and encourages continuation of the research. What if it is actually possible but I write it incorrectly.

  • LandLuLandLu Member, Xamarin Team Xamurai

    We need to indicate which assembly it belongs to.
    Maybe the same name could exist in different assemblies in your project.

Sign In or Register to comment.