Xamarin-android: Support full aot for Android

Created on 9 Dec 2017  路  7Comments  路  Source: xamarin/xamarin-android

Hope to implement "full aot" to remove libmonodroid_bundle_app.so.
It can reduce the size of the software when we do not need to dynamically generate the code.

Steps to Reproduce



    1. 2.
  1. 3.

Expected Behavior

Actual Behavior

Version Information

Log File

Most helpful comment

I wrote a custom msbuild task that removes unwanted .so files and cil-strip .dll files if their counterpart is in the lib folder as .so file before the "_Sign" step. All I want is a partial AOT. I don't need system.core to be striped because I don't want this file to be native as .so. When I run the final apk on the device, it gives "Could not load assembly AAAA during startup registration" error although the file is there in the assemblies folder as striped and there is a libaot- counterpart in the lib folder. Aot mode is normal. Is there a way to do what I want?

All 7 comments

Hope to implement "full aot" to remove libmonodroid_bundle_app.so.

That doesn't make any sense at all, as it intermixes two separate things.

(Aside: supporting "Full AOT", while possible, is explicitly not a goal, because it will very likely cause all existing apps to break, as it doesn't support any JIT use. We support "Hybrid AOT", which supports using the Full AOT backend+native libraries, while continuing to support a JIT, for System.Reflection.Emit/etc. use. Plus, Xamarin.Android itself still requires a JIT. Even if Xamarin.Android didn't require it, we have no way of knowing what existing apps require runtime codegen. Removing the JIT is Not A Thing we are considering.)

libmonodroid_bundle_app.so only contains assemblies, zip-compressed. It's a way to avoid the assemblies/ directory within the .apk.

"Full AOT"/Hybrid AOT generates a native library (.so) for each assembly. The assembly is still required to exist, somewhere, so that System.Reflection can work. (The assemblies can be IL-stripped with Hybrid AOT, but they must still exist!)

Finally, Hybrid AOT results in huge native libraries. You almost certainly will not get smaller apps from it. You will get faster apps from it. (The classic size/speed tradeoff.)

You are able to use both mkbundle ("Bundle assemblies into native code"/libmonodroid_bundle_app.so) and Hybrid AOT together and separately. They do not interact with each other in any meaningful way.

@jonpryor U've mentioned that assemblies can be IL-stripped. From security perspective it is a great thing. Right now it is matter of few hours for a smart guy from the street that knows what deflate is and has .APK, to get real IL.

Is there possibility that you guys make that stripping part of XA build pipeline ? Will be happy to become your beta tester.

Is there possibility that you guys make that stripping part of XA build pipeline?

@Belorus: That should already be possible:

  • Set $(AndroidAotMode)=Hybrid
  • Set $(AotAssemblies)=True

This should cause cil-strip.exe to be run, which will remove method body IL from the assemblies.

As with iOS, assemblies are still needed, but the IL method bodies are not.

I tried to build my project with AndroidAotMode=Hybrid and get this error when archiving:

Could not strip IL of assembly: obj\Release\android\assets\System.Core.dll

[cil-strip stdout] Error: System.MissingMethodException: Method not found: 'Void System.Array.Reverse(!!0[], Int32, Int32)'.
  [cil-strip stdout]    at Mono.Cecil.AssemblyNameReference.get_PublicKeyToken()
  [cil-strip stdout]    at Mono.Cecil.AssemblyNameReference.Parse(String fullName)
  [cil-strip stdout]    at Mono.Cecil.Signatures.SignatureReader.CreateEnumTypeReference(String enumName)
  [cil-strip stdout]    at Mono.Cecil.Signatures.SignatureReader.ReadTypeReference(Byte[] data, BinaryReader br, ElementType& elemType)
  [cil-strip stdout]    at Mono.Cecil.Signatures.SignatureReader.ReadNamedArg(Byte[] data, BinaryReader br, Boolean& read, Boolean resolve)
  [cil-strip stdout]    at Mono.Cecil.SecurityDeclarationReader.CreateSecurityAttribute(SecurityAction action, BinaryReader br, Byte[] permset, Int32 pos, Int32& start, Boolean resolve)
  [cil-strip stdout]    at Mono.Cecil.SecurityDeclarationReader.FromByteArray(SecurityAction action, Byte[] declaration, Boolean resolve)
  [cil-strip stdout]    at Mono.Cecil.AggressiveReflectionReader.ReadSecurityDeclarations()
  [cil-strip stdout]    at Mono.Cecil.AggressiveReflectionReader.VisitTypeDefinitionCollection(TypeDefinitionCollection types)
  [cil-strip stdout]    at Mono.Cecil.StructureReader.TerminateAssemblyDefinition(AssemblyDefinition asm)
  [cil-strip stdout]    at Mono.Cecil.AssemblyFactory.GetAssembly(ImageReader irv, Boolean manifestOnly)
  [cil-strip stdout]    at Mono.CilStripper.Program.Main(String[] args)

@bulente I found enable linker can solve the problem. But it will fail in runtime.
Unhandled Exception:

System.InvalidProgramException: Invalid IL code in Android.Support.V4.App.FragmentActivity:GetOnCreate_Landroid_os_Bundle_Handler (): IL_0000: ret       



01-13 06:54:14.165 E/mono    ( 4530): 
01-13 06:54:14.165 E/mono    ( 4530): Unhandled Exception:
01-13 06:54:14.165 E/mono    ( 4530): System.InvalidProgramException: Invalid IL code in Android.Support.V4.App.FragmentActivity:GetOnCreate_Landroid_os_Bundle_Handler (): IL_0000: ret       
01-13 06:54:14.165 E/mono    ( 4530): 
01-13 06:54:14.165 E/mono    ( 4530): 
01-13 06:54:14.165 E/mono    ( 4530):   at Android.Runtime.JNIEnv.RegisterJniNatives (System.IntPtr typeName_ptr, System.Int32 typeName_len, System.IntPtr jniClass, System.IntPtr methods_ptr, System.Int32 methods_len) [0x00217] in <daa44a0def9344c8889faf81a180b741>:0 
01-13 06:54:14.165 E/mono-rt ( 4530): [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in Android.Support.V4.App.FragmentActivity:GetOnCreate_Landroid_os_Bundle_Handler (): IL_0000: ret       
01-13 06:54:14.165 E/mono-rt ( 4530): 
01-13 06:54:14.165 E/mono-rt ( 4530): 
01-13 06:54:14.165 E/mono-rt ( 4530):   at Android.Runtime.JNIEnv.RegisterJniNatives (System.IntPtr typeName_ptr, System.Int32 typeName_len, System.IntPtr jniClass, System.IntPtr methods_ptr, System.Int32 methods_len) [0x00217] in <daa44a0def9344c8889faf81a180b741>:0 

@jonpryor

I found enable linker can solve the problem. But it will fail in runtime.

@jonpryor, thanks for the feedback but I'm using some 3rd party libraries that doesn't allow me to link system.core file which results that kind of runtime error.

I wrote a custom msbuild task that removes unwanted .so files and cil-strip .dll files if their counterpart is in the lib folder as .so file before the "_Sign" step. All I want is a partial AOT. I don't need system.core to be striped because I don't want this file to be native as .so. When I run the final apk on the device, it gives "Could not load assembly AAAA during startup registration" error although the file is there in the assemblies folder as striped and there is a libaot- counterpart in the lib folder. Aot mode is normal. Is there a way to do what I want?

Was this page helpful?
0 / 5 - 0 ratings