How to really speed up your Xamarin Forms apps for Android.
Since ever, one of the most annoying problems of Xamarin Forms on Android was the start up time. A native app takes nothing while even the most basic Xamarin forms apps takes several seconds to show up on Android.
During the years Xamarin has tried hard to make things better, sometimes with very small improvements and other times with a bit better improvements but now with the new Visual Studio release (> 16.5) they did something amazing, something called Startup Tracing with custom Profiles.
I’m not going into many details here as the only thing that matters is how we can improve the performances of our Xamarin Forms apps for Android. So I’ll be very clear here. If you go into the Android Options of your Xamarin projects, you’ll see this:
If you enable the AOT Compilation (Ahead of time), your app performances will improve significantly and so the startup time, but your apk size will increase as well. At this point you can decide what is more important for you, performances or apk size.
Don’t answer yet, there is a solution
Since Visual Studio 2019 Version 16.2 you were able to enable the Startup Tracing. If you enable this option, the profiler will do its magic (memory, execution time and some stats) to create a profile for your app to increase the performances without affecting too much your apk size.
According to Xamarin (you can read the full post here), these are the results of enabling these options:
|Type||Startup Speed||APK Size|
|Startup Tracing||1s518ms||20.1 MB|
It means that AOT is amazing and you should enable it but if you care about the apk size you should definitely enable the Startup tracing as well, so you’ll get an incredible startup speed improvement without affecting too much your apk size.
Amazing but now it’s even better
Since Visual Studio 16.5 (or Visual Studio for Mac 8.5), Xamarin did even better and trust me, the result is more than amazing. I was really impressed by it so do a favour to yourself and continue to read!
We can now generate a Custom Profile of our app! If we do that, the next time our app will run in no time (Cool cool cool cool cool…cit.).
Unfortunately for now there is no UI to generate a Custom Profile (hopefully Microsoft will do it soon), but I’ll show you how to generate your Custom Profile in no time. I’ll try to keep it as easy as I can and as usual, if you have questions or issues, leave a comment here and I’ll reply as soon as possible.
Generate a Custom Profile
Follow this short guide and you’ll get your Custom Profile in no time and you’ll really improve the performances of your Xamarin Forms apps for Android.
- Open a command prompt in the directory that contains your Android *.csproj file.
- Connect one (and only one) Android device to your computer.
- In your Android manifest enable the INTERNET permission.
- Write and run this command: msbuild /t:BuildAndStartAotProfiling
- Run your app until is completely loaded (don’t press or do anything on your app!!!).
- Write and run this command: msbuild /t:FinishAotProfiling (* if you have problems read just below these points)
- This will create the file custom.aprof to your Android project. Now change the Build Action of this file to AndroidAotProfile.
- Last step is to add two new properties to your Release configuration:
<PropertyGroup> <AndroidEnableProfiledAot>true</AndroidEnableProfiledAot> <AndroidUseDefaultAotProfile>false</AndroidUseDefaultAotProfile> </PropertyGroup>
Enjoy your new amazing performances!
ONLY IN CASE OF ERROR: When running the command msbuild /t:FinishAotProfiling some people (I had it as well on one of my machines) get the error:
aotprofile-tool: Unable to read profile through local port: 9999
If you do, open the Command prompt with admin privileges and (always in the folder containing you Android .csproj file) write:
set PATH=%PATH%;"C:\Program Files (x86)\Android\android-sdk\platform-tools" msbuild -t:FinishAotProfiling
This will solve the problem and you will be able to have your custom profile.
Again according to Xamarin (you can read it here) this is the result of their tests:
|Release||Release/AOT||Release/Startup Tracing (Default)||Release/Startup Tracing(Custom)|
I’ve personally tried the Startup Tracing with Custom Profiles and trust me, the results are impressive, the first time i couldn’t believe my eyes. When we enable these options we can finally obtain startup performances very similar to a native app (similar, not the same…yet!) so may suggestion here is to enable AOT and Startup Tracing and to create a Custom Profile for every project you are going to create! My suggestion is add this page as preferite as you will need it for every project you are going to create. As usual, add a comment and I’ll try to reply asap!