locked
Is it possible to preserve ALL constructors when using Linker? RRS feed

  • Question

  • User87950 posted

    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?

    Thursday, May 28, 2020 2:35 AM

All replies

  • User369979 posted

    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.

    Thursday, May 28, 2020 5:39 AM
  • User87950 posted

    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.

    Thursday, May 28, 2020 6:01 AM
  • User369979 posted

    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.

    Thursday, May 28, 2020 6:27 AM
  • User87950 posted

    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.

    Sunday, May 31, 2020 5:16 PM
  • User369979 posted

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

    Monday, June 1, 2020 9:38 AM