{"id":46652,"date":"2020-03-19T10:35:42","date_gmt":"2020-03-19T17:35:42","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=46652"},"modified":"2020-03-24T12:05:29","modified_gmt":"2020-03-24T19:05:29","slug":"faster-android-startup-times-with-startup-tracing","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/faster-android-startup-times-with-startup-tracing\/","title":{"rendered":"Faster Application Startup using Custom Profiles with Startup Tracing on Android"},"content":{"rendered":"<p>In our efforts to bring you extremely fast startup times on Android, we&#8217;re bringing you the ability to record your own profiles to use with startup tracing on Android.<\/p>\n<h2>What is a custom profile?<\/h2>\n<p>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.<\/p>\n<h2>Requirements<\/h2>\n<p>To use custom profiles with startup tracing on Android, you&#8217;ll need Visual Studio 16.5+ or Visual Studio for Mac 8.5+.<\/p>\n<div class=\"row justify-content-center\"><a class=\"cta_button_link\" style=\"text-transform: titlecase; display: inline-block; background-color: #0078d4; color: white; padding: 10px; margin: 10px 0; text-decoration: none;\" href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\" target=\"_blank\" rel=\"noopener noreferrer\">Download Visual Studio Preview<\/a><\/div>\n<div class=\"row justify-content-center\"><a class=\"cta_button_link\" style=\"text-transform: titlecase; display: inline-block; background-color: #0078d4; color: white; padding: 10px; margin: 10px 0; text-decoration: none;\" href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/mac\/install-preview?view=vsmac-2019\" target=\"_blank\" rel=\"noopener noreferrer\">Download Visual Studio for Mac Preview<\/a><\/div>\n<h2>Getting Started<\/h2>\n<p>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!<\/p>\n<ol>\n<li>Open a command prompt or terminal against your Android project&#8217;s directory that contains the <code>.csproj<\/code>.<\/li>\n<li>Ensure only one Android device is attached.<\/li>\n<li>Under the app&#8217;s\u00a0<strong>Android Manifest &gt; Required permissions<\/strong>\u00a0in the Visual Studio project property pages, ensure the\u00a0<strong>INTERNET<\/strong>\u00a0permission is enabled.<\/li>\n<li><code>msbuild \/t:BuildAndStartAotProfiling<\/code><\/li>\n<li>Let your application run until it&#8217;s loaded.<\/li>\n<li><code>msbuild \/t:FinishAotProfiling<\/code><\/li>\n<li>This will add a custom.aprof file to your project which you&#8217;ll want to change the <code>Build Action<\/code> to <code>AndroidAotProfile<\/code>. You can do this by adding the following <code>&lt;ItemGroup&gt;<\/code> to your <code>.csproj<\/code>:<code>&lt;ItemGroup&gt; &lt;AndroidAotProfile Include=\"$(MSBuildThisFileDirectory)custom.aprof\" \/&gt; &lt;\/ItemGroup&gt;<\/code><img decoding=\"async\" class=\"aligncenter size-full wp-image-46653\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/03\/customprofile.png\" alt=\"Image customprofile\" width=\"335\" height=\"233\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/03\/customprofile.png 335w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/03\/customprofile-300x209.png 300w\" sizes=\"(max-width: 335px) 100vw, 335px\" \/><\/li>\n<li>Finally, you&#8217;ll need to include two MSBuild properties to your <code>Release<\/code> configuration to enable the feature and use the custom profile.<\/li>\n<\/ol>\n<p><code>&lt;PropertyGroup&gt; &lt;AndroidEnableProfiledAot&gt;true&lt;\/AndroidEnableProfiledAot&gt; &lt;AndroidUseDefaultAotProfile&gt;false&lt;\/AndroidUseDefaultAotProfile&gt; &lt;\/PropertyGroup&gt;<\/code><\/p>\n<p><strong>Note:<\/strong> On Windows, you can use Developer Command Prompt for Visual Studio(<strong>Tools &gt; Command Line &gt; Developer Command Prompt<\/strong>). This is especially helpful for side by side installations of Visual Studio.<\/p>\n<h2>Differences in Startup Tracing Modes<\/h2>\n<p>You can see the differences in startup tracing modes against a larger Xamarin.Forms application. The measurements below were collected against the <a href=\"https:\/\/github.com\/microsoft\/SmartHotel360\">SmartHotel360<\/a> repository.<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 20%;\"><\/td>\n<td style=\"width: 20%;\">Release<\/td>\n<td style=\"width: 20%;\">Release\/AOT<\/td>\n<td style=\"width: 20%;\">Release\/Startup Tracing (Default)<\/td>\n<td style=\"width: 20%;\">Release\/Startup Tracing(Custom)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%;\">Activity Displayed<\/td>\n<td style=\"width: 20%;\">4863ms<\/td>\n<td style=\"width: 20%;\">2086ms<\/td>\n<td style=\"width: 20%;\">3655ms<\/td>\n<td style=\"width: 20%;\">1900ms<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%;\">APK Size<\/td>\n<td style=\"width: 20%;\">48MB<\/td>\n<td style=\"width: 20%;\">95MB<\/td>\n<td style=\"width: 20%;\">57MB<\/td>\n<td style=\"width: 20%;\">60MB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Custom profiles has the most optimal startup performance with a minimal overhead to APK size!<\/p>\n<p>For more details on profiling your applications using our embedded profiler, <a href=\"https:\/\/github.com\/xamarin\/xamarin-android\/blob\/master\/Documentation\/guides\/profiling.md#profiling-the-aot-compiler\">see our documentation.<\/a><\/p>\n<h2>Inspecting AOT Profiles<\/h2>\n<p>The AOT profiles generated by the profiler can be analyzed and\/or filtered by the <code>aprofutil<\/code> tool.<\/p>\n<p>To get started with this tool, run the <code>aprofutil<\/code> tool with the <code>-s<\/code> (summary) argument against your generated <code>.aprof<\/code> profile.<\/p>\n<p>For example:<\/p>\n<p><code>aprofutil -s SmartHotel.Clients.Android\/custom.aprof<\/code><\/p>\n<pre><code>Summary:\r\n\tModules: 50\r\n\tTypes: 1,750\r\n\tMethods: 7,940\r\n<\/code><\/pre>\n<p>For more information regarding using the <code>aprofutil<\/code> tool, see the <a href=\"https:\/\/github.com\/mono\/mono\/tree\/master\/mcs\/tools\/aprofutil\">documentation.<\/a><\/p>\n<p><strong>Note:<\/strong> Because this tool uses <code>adb<\/code>, you will want to ensure <code>adb<\/code> is on your environment path. It can be found in your Android SDK&#8217;s\u00a0<span class=\"lang:default decode:true  crayon-inline \">android-sdk\\platform-tools<\/span>\u00a0 directory.<\/p>\n<h2>Summary<\/h2>\n<p>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.<\/p>\n<p>In the future, you will be able to record profiles within Visual Studio and Visual Studio for Mac.<\/p>\n<p>We can&#8217;t wait to see how this feature improves your application&#8217;s startup times.<\/p>\n<p>Also, don&#8217;t forget to check out Ryan Davis&#8217;s excellent blog article on <a href=\"https:\/\/ryandavis.io\/using-custom-aot-profiles-with-xamarin-android\/\">Using Custom AOT Profiles with Xamarin.Android<\/a>, where he goes through using this feature within his apps!<\/p>\n<h2>Feedback<\/h2>\n<p>We\u2019re 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\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio\">file an issue on Developer Community.<\/a><\/p>\n<p>Additionally,\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/suggest-a-feature\">let us know if you have any suggestions<\/a>\u00a0for how we can improve the Startup Tracing experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developers today can already take advantage of Startup Tracing to improve the startup time of their Android apps. Today, I will show you a new feature that let&#8217;s you build a custom profile to be used for your app with Startup Tracing that finely tunes and optimizes your apps startup.<\/p>\n","protected":false},"author":551,"featured_media":46653,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2,1,367],"tags":[5,8234,8235,6291,6290],"class_list":["post-46652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","category-xamarin","category-xamarin-forms","tag-android","tag-boot-time","tag-custom-profiles","tag-startup-time","tag-startup-tracing"],"acf":[],"blog_post_summary":"<p>Developers today can already take advantage of Startup Tracing to improve the startup time of their Android apps. Today, I will show you a new feature that let&#8217;s you build a custom profile to be used for your app with Startup Tracing that finely tunes and optimizes your apps startup.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/551"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=46652"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/46653"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=46652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=46652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=46652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}