{"id":49312,"date":"2023-02-15T10:00:00","date_gmt":"2023-02-15T18:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=49312"},"modified":"2023-02-15T09:38:28","modified_gmt":"2023-02-15T17:38:28","slug":"upgrade-xamarin-ios-android-to-dotnet6-dotnet7","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/upgrade-xamarin-ios-android-to-dotnet6-dotnet7\/","title":{"rendered":"Tips &amp; Tricks on Upgrading Xamarin.iOS &amp; Xamarin.Android to .NET for iOS &amp; Android"},"content":{"rendered":"<p>There has never been a better time to update &amp; migrate your Xamarin.iOS and Xamarin.Android apps to the latest version of .NET. The update process for most apps should be quick and when you are finished you will be able to take advantage of the latest features of .NET 7 including <a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/whats-new\/csharp-11\">C# 11<\/a> and the new project system. In addition, iOS &amp; Android apps built against .NET 6 and .NET 7 have large <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-7-performance-improvements-in-dotnet-maui\/\">performance improvements<\/a> and developer productivity features as they take advantage of build system enhancements. The team has just released <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/migration\/\">upgrade documentation<\/a> for apps, so go head over there for a full walkthrough. In this blog, I will walk you through some tips &amp; tricks to get started with your update.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2023\/02\/xamarin-to-dotnet.png\" alt=\"Xamarin to .NET with an arrow in between\" \/><\/p>\n<p>If you are looking to update and migrate your Xamarin.Forms based applications then skip this blog and follow the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/get-started\/migrate\">self-guided documentation<\/a> that outlines out to manually update or take advantage of the .NET Upgrade Assistant.<\/p>\n<h2>Work in a Branch<\/h2>\n<p>Before you start your update process it is a good idea to work in a branch if you are using git! This will ensure you can easily roll back and iterate over time if you have a complex project.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2023\/02\/net7-update.png\" alt=\"Create a branch dialog in Visual Studio\" \/><\/p>\n<h2>Analyze NuGet Packages<\/h2>\n<p>The first step in the upgrade process is to check your NuGet packages in your projects. You will need to ensure that they have been updated and re-compiled against .NET 6 or .NET 7 for iOS and Android.<\/p>\n<p>Consider my Xamarin.Android app that I am looking to update, it has references to AndroidX, Material Design, Xamarin.Essentials, and a few of my own libraries:<\/p>\n<pre><code class=\"language-xml\">&lt;ItemGroup&gt;\n  &lt;PackageReference Include=\"Xamarin.AndroidX.AppCompat\" Version=\"1.6.0.1\" \/&gt;\n  &lt;PackageReference Include=\"Xamarin.Google.Android.Material\" Version=\"1.7.0.2\" \/&gt;\n  &lt;PackageReference Include=\"Xamarin.Essentials\" Version=\"1.7.4\" \/&gt;\n  &lt;PackageReference Include=\"Plugin.InAppBilling\" Version=\"6.7.0\" \/&gt;\n  &lt;PackageReference Include=\"MonkeyCache.FileStore\" Version=\"1.5.2\" \/&gt;\n&lt;\/ItemGroup&gt;<\/code><\/pre>\n<p>You can browse <a href=\"https:\/\/www.nuget.org\">NuGet.org<\/a> to find your packages and see what frameworks they support. In the case of <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.AndroidX.AppCompat#supportedframeworks-body-tab\">Xamarin.AndroidX.AppCompat<\/a> we can see that it supports both <code>monoandroid12.0<\/code> (Xamarin.Android) and <code>net6.0-android31.0<\/code> (compiled against .NET 6).<\/p>\n<blockquote>\n<p>Note: For Xamarin.Android projects it is recommended to migrate from Android Support Libraries as they are not supported in .NET 6+ Android projects. You can do this by migrating to AndroidX by following the <a href=\"https:\/\/learn.microsoft.com\/xamarin\/android\/platform\/androidx\">migration guide documentation<\/a>. <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2023\/02\/androidx.png\" alt=\"AndriodX.AppCompat support table\" \/><\/p>\n<\/blockquote>\n<p>In this case, this library is fully compatible with the latest versions of .NET for Android. If the version of a NuGet package isn&#8217;t compatible with the latest framework you may need to update to a new version or find a replacement. Monkey Cache for example only supports only supported Xamarin apps in <a href=\"https:\/\/www.nuget.org\/packages\/MonkeyCache\/1.5.2#supportedframeworks-body-tab\">version 1.5.2<\/a>, but <a href=\"https:\/\/www.nuget.org\/packages\/MonkeyCache\/2.0.1#supportedframeworks-body-tab\">version 2.0.1<\/a> was recompiled against .NET 6 and will be compatible.<\/p>\n<h2>Update Project Files<\/h2>\n<p>Android and iOS are now integrated directly into .NET. This means that they have have new <a href=\"https:\/\/learn.microsoft.com\/dotnet\/standard\/frameworks#net-5-os-specific-tfms\">Target Framework Monikers<\/a> of <code>net7.0-android<\/code> and <code>net7.0-ios<\/code>. They also use the new .NET SDK project style. This means the configuration inside of your projects <code>.csproj<\/code> has been greatly simplified. The next step in the process is to <strong>unload<\/strong> your Android and iOS Projects. The easiest thing to do is delete all of the content inside of it and add the new base SDK style project settings:<\/p>\n<p><strong>Android<\/strong>:<\/p>\n<pre><code class=\"language-xml\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n  &lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net7.0-android&lt;\/TargetFramework&gt;\n    &lt;OutputType&gt;Exe&lt;\/OutputType&gt;\n  &lt;\/PropertyGroup&gt;\n&lt;\/Project&gt;<\/code><\/pre>\n<p><strong>iOS<\/strong>:<\/p>\n<pre><code class=\"language-xml\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n  &lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net7.0-ios&lt;\/TargetFramework&gt;\n    &lt;OutputType&gt;Exe&lt;\/OutputType&gt;\n  &lt;\/PropertyGroup&gt;\n&lt;\/Project&gt;<\/code><\/pre>\n<p>Notice here that there is no longer the need to manually reference individual files, resources, or compilation settings for <code>Debug<\/code> or <code>Release<\/code>. The project system is automatically configured to use defaults. You can always update these settings with the new project settings dialog.<\/p>\n<p>Before reloading the project it is a good idea to manually delete the <code>obj<\/code> and <code>bin<\/code> folders for the project.<\/p>\n<h2>Android: Delete Resource.designer.cs<\/h2>\n<p>In Xamarin.Android projects a <code>Resource.designer.cs<\/code> file was generated when resources changed and added into the <code>Resources<\/code> folder. This file is no longer needed and can be deleted as they are auto generated as a code generation step.<\/p>\n<h2>Delete AssemblyInfo.cs<\/h2>\n<p>Similar to the <code>Resource.designer.cs<\/code> the <code>AssemblyInfo.cs<\/code> file is automatically generated based on project settings. These files can be deleted from the <strong>Properties<\/strong> folder for both iOS and Android.<\/p>\n<blockquote>\n<p>Note: You may have had permission settings in your Android project&#8217;s <code>AssemblyInfo.cs<\/code> file. You can leave these and remove everything else, or move them to another file.<\/p>\n<\/blockquote>\n<h2>Xamarin.Essentials to .NET MAUI Essentials<\/h2>\n<p>Xamarin.Essentials was a fundamental library for nearly every Xamarin application. If you were using this NuGet package, you will want to remove it as Xamarin.Essentials is now part of .NET MAUI. The team has worked hard to ensure that while it comes pre-configured with every .NET MAUI application, it is still available to all iOS and Android apps built with .NET.<\/p>\n<pre><code class=\"language-xml\">&lt;PropertyGroup&gt;\n  &lt;UseMauiEssentials&gt;true&lt;\/UseMauiEssentials&gt;\n&lt;\/PropertyGroup&gt;<\/code><\/pre>\n<p>Once you update to .NET MAUI Essentials, you will need to update any <code>using Xamarin.Essentials;<\/code> using statements to the new .NET MAUI Essentials namespaces, which you can find in the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/platform-integration\">documentation<\/a>.<\/p>\n<h2>Add &amp; Update NuGet Packages<\/h2>\n<p>Going back to the NuGet packages that we previously analyzed, now we can add them back into our project. For our Android app we will move over and update the packages to the latest versions that are compatible, and remove the reference to Xamarin.Essentials:<\/p>\n<pre><code class=\"language-xml\">&lt;ItemGroup&gt;\n    &lt;PackageReference Include=\"Xamarin.AndroidX.AppCompat\" Version=\"1.6.0.1\" \/&gt;\n    &lt;PackageReference Include=\"Xamarin.Google.Android.Material\" Version=\"1.7.0.2\" \/&gt;\n    &lt;PackageReference Include=\"Plugin.InAppBilling\" Version=\"6.7.0\" \/&gt;\n    &lt;PackageReference Include=\"MonkeyCache.FileStore\" Version=\"2.0.1\" \/&gt;\n&lt;\/ItemGroup&gt;<\/code><\/pre>\n<h2>Project References<\/h2>\n<p>If you reference any other .NET Standard libraries you can add them as a project reference. Any Xamarin.iOS or Xamarin.Android class library or binding library will also need to be updated to the new formats and then added back as references. You may want to consider updating your .NET Standard libraries to .NET 6 or .NET 7 to take advantage of the latest features of .NET and C#!<\/p>\n<h2>Use New Features<\/h2>\n<p>There are several enhancements to the project system that help you easily manage app settings. Now, you can manage your app versions, supported platform versions, app identifiers, and more!<\/p>\n<p>Here are a few settings you can add to your <code>ProjectGroup<\/code> in your project:<\/p>\n<pre><code class=\"language-xml\">&lt;ApplicationTitle&gt;MyApp&lt;\/ApplicationTitle&gt;\n&lt;SupportedOSPlatformVersion&gt;21&lt;\/SupportedOSPlatformVersion&gt;\n&lt;ApplicationId&gt;com.companyname.myapp&lt;\/ApplicationId&gt;\n&lt;ApplicationVersion&gt;1&lt;\/ApplicationVersion&gt;\n&lt;ApplicationDisplayVersion&gt;1.0&lt;\/ApplicationDisplayVersion&gt;<\/code><\/pre>\n<p>Once these are added you can remove them from your <code>AndroidManifest.xml<\/code> and <code>Info.plist<\/code>. Additionally, you can remove the <code>&lt;uses-sdk\/&gt;<\/code> node from the <code>AndroidManifest.xml<\/code> as these are now outlined with these settings. There are many more options that you can find on <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/migration\/android-projects\">documentation for Android<\/a> and <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/migration\/apple-projects\">documentation for iOS<\/a><\/p>\n<p>Additionally, you may want to consider turning on <a href=\"https:\/\/learn.microsoft.com\/dotnet\/core\/tutorials\/top-level-templates#implicit-using-directives\">implicit usings<\/a> and enabling <a href=\"https:\/\/learn.microsoft.com\/dotnet\/csharp\/tutorials\/nullable-reference-types\">nullable reference types<\/a><\/p>\n<pre><code class=\"language-xml\">    &lt;Nullable&gt;enable&lt;\/Nullable&gt;\n    &lt;ImplicitUsings&gt;enable&lt;\/ImplicitUsings&gt;<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>I hope that you found this upgrade guide nifty on getting your Xamarin.iOS and Xamarin.Android apps updated to the latest version of iOS &amp; Android for .NET. I tried to cover a lot of tips &amp; tricks, but be sure to read the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/maui\/migration\/\">full migration documentation<\/a> for even more.<\/p>\n<p>For more information on the performance improvements be sure to browse through the amazing blog posts from Jonathan Peppers on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-dotnet-maui\/\">.NET 6 improvements<\/a> and <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-7-performance-improvements-in-dotnet-maui\/\">.NET 7 improvements<\/a>.<\/p>\n<p>I also have uploaded full before and after code samples available on my <a href=\"https:\/\/github.com\/jamesmontemagno\/xamarin-migration-to-dotnet\">GitHub<\/a>.<\/p>\n<p>Don&#8217;t forget to read through the <a href=\"https:\/\/dotnet.microsoft.com\/platform\/support\/policy\/xamarin\">Xamarin support policy<\/a> to make sure you plan your upgrade and migration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There has never been a better time to update &amp; migrate your Xamarin.iOS and Xamarin.Android apps to the latest version of .NET to take advantage of the latest features, support, and performance improvements.<\/p>\n","protected":false},"author":544,"featured_media":49313,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,303,1,291],"tags":[9222,9223,9217,9221],"class_list":["post-49312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-ios","category-xamarin","category-xamarin-platform","tag-net-6","tag-net-7","tag-migration","tag-upgrade"],"acf":[],"blog_post_summary":"<p>There has never been a better time to update &amp; migrate your Xamarin.iOS and Xamarin.Android apps to the latest version of .NET to take advantage of the latest features, support, and performance improvements.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/49312","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\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=49312"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/49312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/49313"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=49312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=49312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=49312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}