Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable AOT in Xamarin for Android (Visual Studio)

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)

like image 673
Riuo Avatar asked May 22 '16 17:05

Riuo


People also ask

What is AOT xamarin?

AOT stands for Ahead of Time compilation, and compiles your code, to the native platform, dependent upon the architecture. The reason you would use AOT, is because it has a drastically reduces startup time, and app performance. You can see some results from Improving Xamarin. Forms Startup Performance.

Can we use xamarin in Android Studio?

I assume that when you are talking about Android Studio you mean Native Android Development and by Visual Studio you mean Xamarin. If you want to create crossplatform app then Native Android Development is NOT a way to go. Xamarin might work for you, BUT - you'd rather recommend you to go with Flutter.

How do I make an APK file in Visual Studio?

To create the APK file, right-click the Xamarin. Android project in the Solution Explorer and select Archive... This will open the Archive manager and begin archiving the project, preparing to create the APK file. When it finishes archiving the project, click in Distribute... to proceed.


2 Answers

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.

like image 174
SushiHangover Avatar answered Oct 20 '22 10:10

SushiHangover


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 :)

like image 11
VeYroN Avatar answered Oct 20 '22 09:10

VeYroN