Bug 60291 - There is no option to strip 3rd party [assembly: Preserve] marked assemblies
Summary: There is no option to strip 3rd party [assembly: Preserve] marked assemblies
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 7.4 (15.3)
Hardware: PC Windows
: --- enhancement
Target Milestone: ---
Assignee: Radek Doulik
Depends on:
Reported: 2017-10-20 16:57 UTC by Eric Liu
Modified: 2017-10-24 15:54 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

example of preserved 3rd party (38.39 KB, application/x-zip-compressed)
2017-10-23 19:44 UTC, Eric Liu

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 60291 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Eric Liu 2017-10-20 16:57:18 UTC
There is no option to force the linker to strip assembly if the 3rd party assembly is marked as [assembly: Preserve] in a Xamarin.Android assembly.

Tried to look through the linker + followed the options



Tried some hacky way too with no success:

  <assembly fullname="someAssemblyMarkedAsPreserve">
    <type fullname="*" preserve="nothing" required="false"/>

There are documentations about skip stripping specific things, but not otherwise. (not everything is documented, like how to keep nested generic classes)

Is there any way to achieve this?
Comment 1 Radek Doulik 2017-10-23 18:57:44 UTC
Could you please add a project, which demonstrates the issue?

If that's 3rd party assembly, do you use LinkAll or LinkSDKOnly mode?
Comment 2 Eric Liu 2017-10-23 19:44:07 UTC
Created attachment 25412 [details]
example of preserved 3rd party

Link all, ofc.
The android tooling skips assemblies (with preserve) even if it's in LinkDescription not to skip.

In this example I try to shrink the Xamarin.Forms.Core.dll which will never happen because 
605Kb 95% unused code

But cases like a nuget package with 3mb sized assembly marked as [assembly: Preserve] can hurt the app.

Still not sure if it's by design or a bug.
Comment 3 Radek Doulik 2017-10-24 10:29:03 UTC
OK, now I see what you mean.

I think there's no way to do that in Xamarin.Android currently. You would have to remove the attribute by some other tool beforehand. (it would be probably easy to create such a tool with https://github.com/jbevain/cecil)

I guess the assembly which harms you is different than the Xamarin.Forms.Core as it is smaller than 3mb? You might try to contact the developers of the 3rd party assembly to find out why they used the Preserve attribute on the whole assembly.
Comment 4 Eric Liu 2017-10-24 15:54:46 UTC
Thank you

So it's the Xamarin-Tooling's design and not a linker bug. I move the category because it's nothing to do with the linker then.

So the only option currently is manually modify the assembly or fork open source and build it myself.

Btw, if the linker removes some IL code I need, then I just define a tag in the xml descriptor. The biggest problem is if the assembly using lots of unwanted references...