Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Updating NuGet packages does not update…
Browse files Browse the repository at this point in the history
… the app

Fixes #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.
  • Loading branch information
dellis1972 committed Mar 17, 2023
1 parent 43dc9ba commit eb155d0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ because xbuild doesn't support framework reference assemblies.
<AndroidAddKeepAlives Condition="'$(AndroidAddKeepAlives)' == '' And '$(AndroidIncludeDebugSymbols)' != 'True'">True</AndroidAddKeepAlives>
<AndroidLinkResources Condition="'$(AndroidLinkResources)' == '' And '$(AndroidIncludeDebugSymbols)' != 'True'">False</AndroidLinkResources>
<_AndroidBuildPropertiesCacheExists Condition=" Exists('$(_AndroidBuildPropertiesCache)') ">True</_AndroidBuildPropertiesCacheExists>
<_NuGetAssetsFile Condition=" Exists('$(ProjectAssetsFile)') ">$(ProjectAssetsFile)</_NuGetAssetsFile>
<_NuGetAssetsFile Condition=" Exists('$(ProjectLockFile)') ">$(ProjectLockFile)</_NuGetAssetsFile>
<_NuGetAssetsFile Condition=" '$(_NuGetAssetsFile)' == '' and Exists('packages.config') ">packages.config</_NuGetAssetsFile>
<_NuGetAssetsTimestamp Condition=" '$(_NuGetAssetsFile)' != '' ">$([System.IO.File]::GetLastWriteTime('$(_NuGetAssetsFile)').Ticks)</_NuGetAssetsTimestamp>
Expand Down

0 comments on commit eb155d0

Please sign in to comment.