Xamarin.iOS Linker removes class members needed to reflect

In the Xamarin project, I set the linker behavior to Link All Links. In this case, assemblies are optimized by removing unused elements. One problem, she does not know that they need us to think. I know that there is a Preserve attribute that we can set to tell the linker to save all the characters in the class, but what if the assembly is a DLL or just not an iOS project (it does not refer to monotouch assemblies, and cannot). Is there a way I can set for all classes from the library to β€œsave”, ignore it, or install only certain classes for optimization (reverse PreserveAttribute)?

+6
source share
3 answers

@SharpMobileCode gave a very good explanation, but there are two more ways to solve this problem.

  • Use the XML file and use --xml=your.file in the project options, under Advanced mtouch Arguments. This can be useful if you cannot change the source code of the assemblies β€” or β€” when you need / need to use the tool to create a list of saved items;

  • Use the new Preserve(Type) constructor. This allows you to add save instructions to another assembly (for example, your main .exe) that already has a link to Xamarin.iOS.dll (so you do not need to define your own type). For instance.

[assembly: Preserve (typeof (MyType), AllMembers = true)]

+8
source

What you can do is create a LinkerPleaseInclude.cs file. You can name it whatever you want, but that name makes sense. There you can create dummy objects for the types you need, especially for reflection. Thus, the link will see that you are "in need" of it and will not break it. Here is an example of a LinkerPleaseInclude.cs file . This is useful if the classes you need are defined in a third-party DLL.

Update:

And, I read your original post incorrectly, thinking that you are talking about third-party DLLs for which you have no control. Therefore, if you have a non-xamarin.iOS library, you can still use the Preserve attribute. However, you will need to define it like this in your library project .:

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

This is not the namespace that you define for the Preserve class, because the linker searches only for the Preserve name. Then you can just use the Preserve attribute as usual. Therefore, if you want to save the class and all 50 properties, then you do something like:

 [Preserve(AllMembers=true)] public MyClass { //code here } 
+5
source

For those who use Reflection to set the UIKit Views properties, here is my bread and butter preservation configuration.

Just create a new class in the Xamarin iOS application project and name it as "LinkerGuard.cs" or "PreserveConfiguration.cs"

Put this in it:

 using CoreAnimation; using CoreGraphics; using Foundation; using UIKit; [assembly: Preserve (typeof (UIResponder), AllMembers = true)] [assembly: Preserve (typeof (UIControl), AllMembers = true)] [assembly: Preserve (typeof (UIView), AllMembers = true)] [assembly: Preserve (typeof (UIButton), AllMembers = true)] [assembly: Preserve (typeof (UILabel), AllMembers = true)] [assembly: Preserve (typeof (UIImageView), AllMembers = true)] [assembly: Preserve (typeof (UITextField), AllMembers = true)] [assembly: Preserve (typeof (UISwitch), AllMembers = true)] [assembly: Preserve (typeof (UIActivityIndicatorView), AllMembers = true)] [assembly: Preserve (typeof (UIDatePicker), AllMembers = true)] [assembly: Preserve (typeof (UIScrollView), AllMembers = true)] [assembly: Preserve (typeof (UIWebView), AllMembers = true)] [assembly: Preserve (typeof (UIPageControl), AllMembers = true)] [assembly: Preserve (typeof (UIToolbar), AllMembers = true)] [assembly: Preserve (typeof (UIStepper), AllMembers = true)] [assembly: Preserve (typeof (UISegmentedControl), AllMembers = true)] [assembly: Preserve (typeof (UISearchBar), AllMembers = true)] [assembly: Preserve (typeof (UIScreen), AllMembers = true)] [assembly: Preserve (typeof (UIWindow), AllMembers = true)] [assembly: Preserve (typeof (UIBarItem), AllMembers = true)] [assembly: Preserve (typeof (UINavigationBar), AllMembers = true)] [assembly: Preserve (typeof (UITabBar), AllMembers = true)] [assembly: Preserve (typeof (UITabBarItem), AllMembers = true)] [assembly: Preserve (typeof (UIBarButtonItem), AllMembers = true)] [assembly: Preserve (typeof (UITextFieldCondition), AllMembers = true)] [assembly: Preserve (typeof (UIViewContentMode), AllMembers = true)] [assembly: Preserve (typeof (UITextAlignment), AllMembers = true)] [assembly: Preserve (typeof (UIControlContentHorizontalAlignment), AllMembers = true)] [assembly: Preserve (typeof (UIReturnKeyType), AllMembers = true)] [assembly: Preserve (typeof (UIDataDetectorType), AllMembers = true)] [assembly: Preserve (typeof (UIKeyboardType), AllMembers = true)] [assembly: Preserve (typeof (UITextFieldChange), AllMembers = true)] [assembly: Preserve (typeof (UITableView), AllMembers = true)] [assembly: Preserve (typeof (UICollectionView), AllMembers = true)] [assembly: Preserve (typeof (UITableViewCell), AllMembers = true)] [assembly: Preserve (typeof (UICollectionViewCell), AllMembers = true)] [assembly: Preserve (typeof (UITableViewDataSource), AllMembers = true)] [assembly: Preserve (typeof (UICollectionViewDataSource), AllMembers = true)] [assembly: Preserve (typeof (UIViewController), AllMembers = true)] [assembly: Preserve (typeof (UITabBarController), AllMembers = true)] [assembly: Preserve (typeof (UINavigationController), AllMembers = true)] [assembly: Preserve (typeof (UIDocument), AllMembers = true)] [assembly: Preserve (typeof (UITapGestureRecognizer), AllMembers = true)] [assembly: Preserve (typeof (UIGestureRecognizer), AllMembers = true)] [assembly: Preserve (typeof (CGColor), AllMembers = true)] [assembly: Preserve (typeof (UIColor), AllMembers = true)] [assembly: Preserve (typeof (UIFont), AllMembers = true)] [assembly: Preserve (typeof (UIImage), AllMembers = true)] [assembly: Preserve (typeof (NSObject), AllMembers = true)] [assembly: Preserve (typeof (NSAttributedString), AllMembers = true)] [assembly: Preserve (typeof (NSLayoutConstraint), AllMembers = true)] [assembly: Preserve (typeof (CAKeyFrameAnimation), AllMembers = true)] [assembly: Preserve (typeof (NSIndexPath), AllMembers = true)] 
+2
source

Source: https://habr.com/ru/post/989033/


All Articles