Enable AOT in Xamarin for Android (Visual Studio)

限于喜欢 提交于 2020-06-09 08:37:49

问题


I know that there's support for AOT in Xamarin for Android. After the software became free, all of its features became free as well. I read around the documentation and I enabled AOT by modifying my project.csproj file, as follows:

<AotAssemblies>True</AotAssemblies>

After making sure that my project path doesn't contain spaces (breaks process), I ran a build and I got an APK with both managed .NET DLLs and native compiled libs. Sadly, the app seems to be using the .NET DLLs and completely ignoring the native libs. Is there any way I can remedy this?

EDIT: Reading around some other Mono AOT-related questions, it appears that this might be how it is supposed to work. I wanted to AOT-compile my app in hopes to reduce the ~2 seconds start-up time, which did not change at all after I switched from JIT to AOT. Can somebody please explain this this to me?

BONUS: Is there any way I can enable advanced optimization flags? (e.g. -o)


回答1:


AOT'ing your assemblies/code is not going to change the startup of the app's initialization (native app bootstrap + Xamarin/Mono initialization BUT not including any of your code execution time).

Now, if you are doing X amount of work that is CPU bound within your code, say within the OnCreate (which you really should not be doing), you would (should) see a decrease in total time. I say should due to the fact that AOT'ing does not guarantee that you will see a faster execution time of a particular portion of code, it does eliminates the jitter, but there are lots of other factors involved. I've been using Mono (AOT w/ & w/o LLVM) for many years and you really need to instrument and test on your code.

Although the JIT mode is very fast, and the default optimizations in Mono have been tuned to provide a good balance between optimizations and JIT speed, AOT compilation provides a few extra benefits:

  • Reduced startup time.

Note: This is particularly useful for large programs that might need to execute a lot of code before they are operational...

  • Potential better performance.

Note: ....This means that certain programs might run slower as the generated code is more general than the specific code that the JIT can produce.

Ref: http://www.mono-project.com/docs/advanced/aot/


Enable LLVM and AOT for testing your release builds:

In terms of optimization of AOT code, enable LLVM along with AOT in your release builds for performance/instrumentation testing. Note: Testing is key, having a complete app test suite and internal instrumentation for collecting runtime performance is key to getting those 5 stars reviews on the app stores ;-)


EnableLLVM

A boolean property that determines whether or not LLVM will be used when Ahead-of-Time compiling assemblines into native code. Support for this property was added in Xamarin.Android 5.1.

This property is False by default.

This property is ignored unless the $(AotAssemblies) MSBuild property is True.


AotAssemblies

A boolean property that determines whether or not assemblies will be Ahead-of-Time compiled into native code and included in the .apk. Support for this property was added in Xamarin.Android 5.1.

This property is False by default.




回答2:


Coincidence or not, when I added <AotAssemblies>True</AotAssemblies> to <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> section of the android .csproj my startup times were reduced from 10 seconds to 4 seconds! Then I removed the AotAssemblies and tried again and I have 10 seconds again, so the AotAssemblies does something :)



来源:https://stackoverflow.com/questions/37377106/enable-aot-in-xamarin-for-android-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!