Faster Application Startup using Custom Profiles with Startup Tracing on Android

Jon Douglas

In our efforts to bring you extremely fast startup times on Android, we’re bringing you the ability to record your own profiles to use with startup tracing on Android.

What is a custom profile?

A custom profile is a profile that is generated based on running an embedded profiler against your application to find out which methods are JIT compiled during the startup. The custom profile can then be used in your release application to get your users into your app even faster.

Requirements

To use custom profiles with startup tracing on Android, you’ll need Visual Studio 16.5+ or Visual Studio for Mac 8.5+.

Getting Started

Today, you can get started using custom profiles with startup tracing via the command line and MSBuild. In a future version of Visual Studio, you will be able to record custom profiles inside the IDE!

  1. Open a command prompt or terminal against your Android project’s directory that contains the .csproj.
  2. Ensure only one Android device is attached.
  3. Under the app’s Android Manifest > Required permissions in the Visual Studio project property pages, ensure the INTERNET permission is enabled.
  4. msbuild /t:BuildAndStartAotProfiling
  5. Let your application run until it’s loaded.
  6. msbuild /t:FinishAotProfiling
  7. This will add a custom.aprof file to your project which you’ll want to change the Build Action to AndroidAotProfile. You can do this by adding the following <ItemGroup> to your .csproj:<ItemGroup> <AndroidAotProfile Include="$(MSBuildThisFileDirectory)custom.aprof" /> </ItemGroup>Image customprofile
  8. Finally, you’ll need to include two MSBuild properties to your Release configuration to enable the feature and use the custom profile.

<PropertyGroup> <AndroidEnableProfiledAot>true</AndroidEnableProfiledAot> <AndroidUseDefaultAotProfile>false</AndroidUseDefaultAotProfile> </PropertyGroup>

Note: On Windows, you can use Developer Command Prompt for Visual Studio(Tools > Command Line > Developer Command Prompt). This is especially helpful for side by side installations of Visual Studio.

Differences in Startup Tracing Modes

You can see the differences in startup tracing modes against a larger Xamarin.Forms application. The measurements below were collected against the SmartHotel360 repository.

Release Release/AOT Release/Startup Tracing (Default) Release/Startup Tracing(Custom)
Activity Displayed 4863ms 2086ms 3655ms 1900ms
APK Size 48MB 95MB 57MB 60MB

Custom profiles has the most optimal startup performance with a minimal overhead to APK size!

For more details on profiling your applications using our embedded profiler, see our documentation.

Inspecting AOT Profiles

The AOT profiles generated by the profiler can be analyzed and/or filtered by the aprofutil tool.

To get started with this tool, run the aprofutil tool with the -s (summary) argument against your generated .aprof profile.

For example:

aprofutil -s SmartHotel.Clients.Android/custom.aprof

Summary:
	Modules: 50
	Types: 1,750
	Methods: 7,940

For more information regarding using the aprofutil tool, see the documentation.

Note: Because this tool uses adb, you will want to ensure adb is on your environment path. It can be found in your Android SDK’s android-sdk\platform-tools  directory.

Summary

Now you can record and use custom profiles when using startup tracing on Android. This provides you the most optimal application startup time with a slight increase to APK size.

In the future, you will be able to record profiles within Visual Studio and Visual Studio for Mac.

We can’t wait to see how this feature improves your application’s startup times.

Also, don’t forget to check out Ryan Davis’s excellent blog article on Using Custom AOT Profiles with Xamarin.Android, where he goes through using this feature within his apps!

Feedback

We’re continuously improving this experience and want to know your thoughts on it! For anything you believe is an issue with Startup Tracing with Android apps, please file an issue on Developer Community.

Additionally, let us know if you have any suggestions for how we can improve the Startup Tracing experience.

4 comments

Discussion is closed. Login to edit/delete existing comments.

  • Cosmin Stirbu 0

    Hello,

    Thank you for sharing this.
    What kind of “start-up code changes” should be considered a trigger for re-generating the .aprof file?

    Thanks,
    Cosmin

  • Michael Behr 0

    “Ensure only one Android device is attached.”
    Does this include the emulator devices?

    If I follow your advise, I receive the error
    “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android\aprofutil” -s -v -f -p 9999 –
    o “custom.aprof”
    Calling ‘adb forward tcp:9999 tcp:9999’…
    EXEC : error : aotprofile-tool: Unable to read profile through local port: 9999. [C:\Users\***\source\repos\PokerMone
    y\PokerMoney\PokerMoney.Android\PokerMoney.Android.csproj]
    What am I doing wrong?

    • Rubén Gómez 0

      Hey Michael!

      Maybe it’s a bit late, but just in case: I got the same error and found out there’s a workaround for it here, i can confirm it works like a charm.

  • Cory Kroll 0

    Does this feature work with Visual Studio Community Edition? The full AOT feature requires Enterprise.

Feedback usabilla icon