From e6bc2aa38f33ccdce158d2083aafc746794b54f2 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Fri, 17 Mar 2023 15:42:08 +0000 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Updating NuGet packages does not update the app Fixes https://github.com/xamarin/xamarin-android/issues/7890 We found and issue where if you upgraded a NuGet Package it did not up date the assemblies in the `$(IntermediateOutputPath)android\assets` folder. We have a unit test which checks this particular scenario, but it was disabled on .net during the port to .net 6. We never got back to enable it. The issue it turns out is that NuGet not longer populates the `ProjectLockFile` property. As a result we never stored the `_NuGetAssetsTimestamp` in the `build.props` file. This causes the `_CleanIntermediateIfNeeded` target to be skipped when it really should run. Switching over to using the `ProjectAssetsFile` property to populate the `_NuGetAssetsTimestamp` property fixes the issue. Also the unit test was updated to work on both Legacy and .net. --- .../Xamarin.Android.Build.Tests/BuildTest.cs | 23 +++++-------------- .../Android/KnownPackages.cs | 5 ++++ .../Xamarin.Android.Common.targets | 1 + 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index 3f1c71828e6..639e05fcb9d 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -983,24 +983,13 @@ public void BuildAfterAddingNuget () //This test validates the _CleanIntermediateIfNeeded target [Test] - [Category ("DotNetIgnore")] // Xamarin.Forms version is too old, uses net45 MSBuild tasks [NonParallelizable] public void BuildAfterUpgradingNuget () { var proj = new XamarinAndroidApplicationProject (); - proj.MainActivity = proj.DefaultMainActivity.Replace ("public class MainActivity : Activity", "public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity"); + proj.MainActivity = proj.DefaultMainActivity.Replace ("public class MainActivity : Activity", "public class MainActivity : AndroidX.AppCompat.App.AppCompatActivity"); - proj.PackageReferences.Add (KnownPackages.XamarinForms_2_3_4_231); - proj.PackageReferences.Add (KnownPackages.AndroidSupportV4_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportCompat_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportCoreUI_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportCoreUtils_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportDesign_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportFragment_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportMediaCompat_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportV7AppCompat_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportV7CardView_27_0_2_1); - proj.PackageReferences.Add (KnownPackages.SupportV7MediaRouter_27_0_2_1); + proj.PackageReferences.Add (KnownPackages.AndroidXAppCompat); using (var b = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) { //[TearDown] will still delete if test outcome successful, I need logs if assertions fail but build passes @@ -1010,7 +999,7 @@ public void BuildAfterUpgradingNuget () if (Directory.Exists (projectDir)) Directory.Delete (projectDir, true); Assert.IsTrue (b.Build (proj), "first build should have succeeded."); - Assert.IsFalse (b.Output.IsTargetSkipped ("_CleanIntermediateIfNeeded"), "`_CleanIntermediateIfNeeded` should have run!"); + Assert.IsFalse (b.Output.IsTargetSkipped ("_CleanIntermediateIfNeeded"), "`_CleanIntermediateIfNeeded` should have run for the first build!"); var nugetStamp = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "stamp", "_CleanIntermediateIfNeeded.stamp"); FileAssert.Exists (nugetStamp, "`_CleanIntermediateIfNeeded` did not create stamp file!"); @@ -1019,12 +1008,12 @@ public void BuildAfterUpgradingNuget () proj.PackageReferences.Clear (); //NOTE: we can get all the other dependencies transitively, yay! - proj.PackageReferences.Add (KnownPackages.XamarinForms_4_0_0_425677); + proj.PackageReferences.Add (KnownPackages.AndroidXAppCompat_1_6_0_1); b.Save (proj, doNotCleanupOnUpdate: true); Assert.IsTrue (b.Build (proj), "second build should have succeeded."); - Assert.IsFalse (b.Output.IsTargetSkipped ("_CleanIntermediateIfNeeded"), "`_CleanIntermediateIfNeeded` should have run!"); + Assert.IsFalse (b.Output.IsTargetSkipped ("_CleanIntermediateIfNeeded"), "`_CleanIntermediateIfNeeded` should have run for the second build!"); FileAssert.Exists (nugetStamp, "`_CleanIntermediateIfNeeded` did not create stamp file!"); - Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "Refreshing Xamarin.Android.Support.v7.AppCompat.dll"), "`ResolveLibraryProjectImports` should not skip `Xamarin.Android.Support.v7.AppCompat.dll`!"); + Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, "Refreshing Xamarin.AndroidX.AppCompat.dll"), "`ResolveLibraryProjectImports` should not skip `Xamarin.AndroidX.AppCompat.dll`!"); FileAssert.Exists (build_props, "build.props should exist after second build."); proj.MainActivity = proj.MainActivity.Replace ("clicks", "CLICKS"); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs index 6fb9b090e2e..007ff8d4172 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs @@ -263,6 +263,11 @@ public static class KnownPackages Version = "1.1.0.1", TargetFramework = "MonoAndroid10", }; + public static Package AndroidXAppCompat_1_6_0_1 = new Package { + Id = "Xamarin.AndroidX.AppCompat", + Version = "1.6.0.1", + TargetFramework = "MonoAndroid10", + }; public static Package AndroidXBrowser = new Package { Id = "Xamarin.AndroidX.Browser", Version = "1.2.0.1", diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 16f631f0d21..6c88e3b8958 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -965,6 +965,7 @@ because xbuild doesn't support framework reference assemblies. True False <_AndroidBuildPropertiesCacheExists Condition=" Exists('$(_AndroidBuildPropertiesCache)') ">True + <_NuGetAssetsFile Condition=" Exists('$(ProjectAssetsFile)') ">$(ProjectAssetsFile) <_NuGetAssetsFile Condition=" Exists('$(ProjectLockFile)') ">$(ProjectLockFile) <_NuGetAssetsFile Condition=" '$(_NuGetAssetsFile)' == '' and Exists('packages.config') ">packages.config <_NuGetAssetsTimestamp Condition=" '$(_NuGetAssetsFile)' != '' ">$([System.IO.File]::GetLastWriteTime('$(_NuGetAssetsFile)').Ticks)