From f1cee97a564439eee8164b4f39f5c4777d0d1902 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Tue, 26 Jan 2021 16:09:48 -0600 Subject: [PATCH] [Xamarin.Android.Build.Tasks] _AddMultiDexDependencyJars only runs for apps Context: https://github.com/xamarin/AndroidX/pull/247 When porting AndroidX to .NET 6, I noticed something odd... I was getting `.aar` files in the build output that appeared to contain the contents of `android-support-multidex.jar`. Then I noticed: https://github.com/xamarin/AndroidX/blob/16b02ae51980f7bab7be395d44f9998eea9bf32f/source/AndroidXProject.cshtml#L18 The AndroidX binding projects are setting `$(AndroidEnableMultiDex)`? This is surely not intentional to be set in a class library, but this causes `_AddMultiDexDependencyJars` to run: If this was added to `@(AndroidJavaLibrary)`, then the `` MSBuild task would happily package it in a .NET 6 class library! I think the solution here is that the `_AddMultiDexDependencyJars` MSBuild target shouldn't actually run unless `$(AndroidApplication)` is `true`. I added updated an existing test for this scenario. --- .../Xamarin.Android.Build.Tests/BuildTest.cs | 21 +++++++++++++++++++ .../Xamarin.Android.Common.targets | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) 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 66b9c8cef5b..93814e1c6fd 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 @@ -175,9 +175,30 @@ public void ClassLibraryHasNoWarnings () //NOTE: these properties should not affect class libraries at all proj.SetProperty ("AndroidPackageFormat", "aab"); proj.SetProperty ("AotAssemblies", "true"); + proj.SetProperty ("AndroidEnableMultiDex", "true"); using (var b = CreateDllBuilder ()) { Assert.IsTrue (b.Build (proj), "Build should have succeeded."); Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, " 0 Warning(s)"), "Should have no MSBuild warnings."); + + // $(AndroidEnableMultiDex) should not add android-support-multidex.jar! + if (Builder.UseDotNet) { + var aarPath = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.aar"); + using var zip = Xamarin.Tools.Zip.ZipArchive.Open (aarPath, FileMode.Open); + Assert.IsFalse (zip.Any (e => e.FullName.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)), + $"{aarPath} should not contain a .jar file!"); + } else { + var assemblyPath = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll"); + using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath); + const string libraryProjects = "__AndroidLibraryProjects__.zip"; + var resource = assembly.MainModule.Resources.OfType () + .FirstOrDefault (e => e.Name == libraryProjects); + Assert.IsNotNull (resource, $"{assemblyPath} should contain {libraryProjects}"); + + using var stream = resource.GetResourceStream (); + using var zip = Xamarin.Tools.Zip.ZipArchive.Open (stream); + Assert.IsFalse (zip.Any (e => e.FullName.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)), + $"{resource.Name} should not contain a .jar file!"); + } } } diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index d90315b758d..d6d64205270 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -1021,7 +1021,8 @@ because xbuild doesn't support framework reference assemblies. - +