8. Further control the size of the package on the Xamarinandroid

Source: Internet
Author: User

link on Android

xamarin.android applications using the linker to reduce the size of your application. The linker uses static analysis of the application to determine which assemblies, types, and members are actually used. the linker behaves like a GC, constantly looking for referenced assemblies, types, and members until the entire referenced assembly, type, and members are found. Types and assemblies that are not referenced are discarded.

For example, Hello, Android Example:

Configuration 1.2.0 size 4.0.1 size
No link publishing: 14.0 MB 16.0 MB
Post by Link: 4.2 MB 2.9 MB

The size after processing through the linker is equivalent to the original 30% in version 1.2.0, which is equivalent to 1 in the 4.0.18% version. It works really well.

Control

The linker is based on Static Analysis . Therefore, code that relies on the runtime environment is not detected (such as reflection, dynamically generated objects, and so on, which cannot be detected by the linker):

//to play along at home, Example must is in a different assembly from myactivity. Public classExample {//Compiler provides default constructor ...}[activity (Label="Linker Example", mainlauncher=true)] Public classmyactivity {protected Override voidOnCreate (Bundle bundle) {Base.        OnCreate (bundle); //Would this work ?        varo = Activator.CreateInstance (typeof(examplelibrary.example)); }}
linker Behavior

The main mechanism for controlling the linker is " Project Options " in the dialog box of the linker Behavior ( in Visual Studio ) drop-down list . There are three options:

    1. do not link ( in Visual Studio No )
    2. Link SDK Assemblies ( SDK Assemblies only )
    3. Link All Assemblies ( SDK and User assemblies )

in the No links option disables the linker; This is the size of the "No link publishing" Application above. This approach is useful for troubleshooting run-time failures to see if the linker is doing the right thing. It is generally not recommended to use this setting in a production version.

the Link SDK Components option link Only come with xamarin.android components . all other assemblies (such as your code) are not linked.

the all components of the link all components on the selection link, which means that your code will be deleted if it is not statically referenced.

The example code above can successfully use the "Do not link and link SDK assemblies" link option . However, selecting the link all assemblies option will fail and generate the following error:

E/mono (17755): [0xafd4d440:] EXCEPTION Handling:System.MissingMethodException:Default constructor not found fortype examplelibrary.example.i/monodroid (17755): Unhandled EXCEPTION:System.MissingMethodException:Default constructor not found fortype examplelibrary.example.i/monodroid (17755): At System.Activator.CreateInstance (System.Type,BOOL) <0x00180>I/monodroid (17755): At System.Activator.CreateInstance (System.Type) <0x00017>I/monodroid (17755): At LinkerScratch2.Activity1.OnCreate (Android.OS.Bundle) <0x00027>I/monodroid (17755): At Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (INTPTR,INTPTR,INTPTR) <0x00057>I/monodroid (17755): at (wrapper Dynamic-method)Object.95bb4fbe-bef8-4e5b-8e99-ca83a5d7a124 (Intptr,intptr,intptr) <0x00033>E/mono (17755): [0xafd4d440:] EXCEPTION Handling:System.MissingMethodException:Default constructor not found fortype EXAMPLELIBRARY.EXAMPLE.E/mono (17755): E/mono (17755): Unhandled Exception:System.MissingMethodException:Default constructor not found fortype EXAMPLELIBRARY.EXAMPLE.E/mono (17755): At System.Activator.CreateInstance (System.Type Type, Boolean nonpublic) [0x00000]inch<filename Unknown>:0E/mono (17755): At System.Activator.CreateInstance (System.Type Type) [0x00000]inch<filename Unknown>:0E/mono (17755): At LinkerScratch2.Activity1.OnCreate (Android.OS.Bundle Bundle) [0x00000]inch<filename Unknown>:0E/mono (17755): At Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_ Savedinstancestate) [0x00000]inch<filename Unknown>:0E/mono (17755): at (wrapper Dynamic-method)Object: 95bb4fbe-bef8-4e5b-8e99-ca83a5d7a124 (INTPTR,INTPTR,INTPTR)
Keep Code

the linker sometimes removes the code you want to keep. For example:

    • You may have code that is called through dynamic reflection System.Reflection.MemberInfo.Invoke .

    • If you instantiate a type dynamically, you may need to preserve the default constructor for the type.

    • If you use XML serialization, you may want to preserve the properties of the type.

in these cases, you can use the Android.Runtime.Preserve The attribute is used to mark a member that is not being statically referenced, but your application still needs it. You can apply this property to each member of a type or to the type itself.

in the following example, this property is used to save Example Constructor for the class :

 Public class example{    [Android.Runtime.Preserve]    public  Example ()    {    }}

If you want to preserve the entire type, you can use the following property syntax:

true)]

For example, in the following code fragment, the entire Example classes are reserved for XML serialization:

true )]class  example{    //  Compiler provides default constructor ...}

Sometimes you want to keep some members, but only the types that are included are retained. In these cases, use the following attribute syntax:

true)]

If you do not want to rely on the Xamarin library (for example, you are building a cross-platform portable class Library (PCL)), you can still use the Android.Runtime.Preserve property. To do this, Android.Runtime declare a preserveattribute within the namespace class, is as follows:

namespace android.runtime{    publicsealedclass  Preserveattribute: System.Attribute    {        publicbool  allmembers;          Public BOOL Conditional;}    }

Error ID

If you cannot use the [Preserve] property, you typically need to provide a preprocessing directive so that the linker trusts that the type is being used and preserves the block. To use this technique we can do this:

[Activity (label="Linker Example", mainlauncher=true)]classmyactivity {#pragmaWarning Disable 0219, 0649Static BOOLFalseflag =false; Staticmyactivity () {if(falseflag) {varIgnore =NewExample (); }    }#pragmaWarning Restore 0219, 0649// ...}

Linkskip

you can specify that a set of user-supplied assemblies is not processed by the link (to preserve the entire assembly), while allowing Use Androidlinkskip MSBuild Properties through Link SDK Assembly behavior skips other user assemblies :

< PropertyGroup >    < Androidlinkskip > Assembly1; Assembly2</androidlinkskip></propertygroup> 

Link Description

the Build Action Can be used in a file that can contain a Custom connection Profile Definitions private or internal that needs to be retained members.

Custom Properties

When you link an assembly, the following custom attribute types are removed from all members:

    • System.ObsoleteAttribute
    • System.monodocumentationnoteattribute
    • System.monoextensionattribute
    • System.monointernalnoteattribute
    • System.monolimitationattribute
    • System.mononotsupportedattribute
    • System.monotodoattribute
    • System.Xml.MonoFIXAttribute

When you link an assembly, the following custom property types are removed from all members in the release version:

    • System.Diagnostics.DebuggableAttribute
    • System.Diagnostics.DebuggerBrowsableAttribute
    • System.Diagnostics.DebuggerDisplayAttribute
    • System.Diagnostics.DebuggerHiddenAttribute
    • System.Diagnostics.DebuggerNonUserCodeAttribute
    • System.Diagnostics.DebuggerStepperBoundaryAttribute
    • System.Diagnostics.DebuggerStepThroughAttribute
    • System.Diagnostics.DebuggerTypeProxyAttribute
    • System.Diagnostics.DebuggerVisualizerAttribute

8. Further control the size of the package on the Xamarinandroid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.