Skip to content

Commit 974cf7d

Browse files
jonathanpeppersdellis1972
authored andcommitted
[Xamarin.Android.Build.Tasks] _ResolveAssemblies better handle missing assemblies (#2622)
Fixes: #2548 Fixes: http://work.devdiv.io/753650 In the case of the Android designer, a `SetupDependenciesForDesigner` target is run as a way to "build enough" things for the designer to work. The problem is if a `<ProjectReference/>` hasn't built yet, the `_ResolveAssemblies` target can fail: "HelloForms.Android.csproj" (SetupDependenciesForDesigner target) (1:7) -> (_ResolveAssemblies target) -> Xamarin.Android.Common.targets(1898,2): error : Exception while loading assemblies: System.InvalidOperationException: Failed to load assembly HelloForms\bin\Debug\netstandard2.0\HelloForms.dll Xamarin.Android.Common.targets(1898,2): error : at Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver resolver) In this case, I ran: msbuild .\HelloForms\HelloForms.Android\HelloForms.Android.csproj /t:SetupDependenciesForDesigner /p:DesignTimeBuild=true /p:AndroidUseManagedDesignTimeResourceGenerator=false /restore /bl From a fresh checkout of this sample: https://github.com/jonathanpeppers/HelloWorld/ To fix this problem, we can add an `Exists` check in the `Condition` for assemblies passed into the `<ResolveAssemblies/>` MSBuild task. I don't think this will have any negative consequences, since we are already doing a check for `Exists ('$(OutDir)$(TargetFileName)')`. I also setup a new test that emulates what happens with `SetupDependenciesForDesigner`. This should also help us make sure we aren't breaking the designer in this repo. Downstream in monodroid, we should also make sure the designer tests we have there are using a `<ProjectReference/>`.
1 parent 42656af commit 974cf7d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AndroidUpdateResourcesTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,5 +1481,54 @@ public void Issue2205 ([Values (false, true)] bool useAapt2)
14811481
Assert.IsTrue (b.Build (proj), "first build should have succeeded");
14821482
}
14831483
}
1484+
1485+
[Test]
1486+
public void SetupDependenciesForDesigner ()
1487+
{
1488+
var path = Path.Combine ("temp", TestName);
1489+
var lib = new XamarinAndroidLibraryProject {
1490+
ProjectName = "Library1",
1491+
OtherBuildItems = {
1492+
new AndroidItem.AndroidAsset ("Assets\\foo.txt") {
1493+
TextContent = () => "Bar",
1494+
},
1495+
},
1496+
};
1497+
var proj = new XamarinFormsAndroidApplicationProject {
1498+
ProjectName = "App1",
1499+
References = { new BuildItem ("ProjectReference", "..\\Library1\\Library1.csproj") },
1500+
};
1501+
proj.Imports.Add (new Import ("Designer.targets") {
1502+
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-16""?>
1503+
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1504+
<PropertyGroup>
1505+
<AndroidUseManagedDesignTimeResourceGenerator>False</AndroidUseManagedDesignTimeResourceGenerator>
1506+
<SetupDependenciesForDesignerDependsOn>
1507+
UpdateAndroidResources;
1508+
_AdjustJavacVersionArguments;
1509+
_GeneratePackageManagerJavaForDesigner;
1510+
_GetMonoPlatformJarPath;
1511+
_DetermineJavaLibrariesToCompile;
1512+
</SetupDependenciesForDesignerDependsOn>
1513+
</PropertyGroup>
1514+
<Target Name=""SetupDependenciesForDesigner"" DependsOnTargets=""$(SetupDependenciesForDesignerDependsOn)"">
1515+
</Target>
1516+
<Target Name=""_GeneratePackageManagerJavaForDesigner"" DependsOnTargets=""_AddStaticResources;_ResolveAssemblies"">
1517+
</Target>
1518+
</Project>
1519+
",
1520+
});
1521+
using (var libb = CreateDllBuilder (Path.Combine (path, lib.ProjectName)))
1522+
using (var appb = CreateApkBuilder (Path.Combine (path, proj.ProjectName))) {
1523+
libb.Save (lib);
1524+
appb.Target = "SetupDependenciesForDesigner";
1525+
Assert.IsTrue (appb.Build (proj, parameters: new [] { "DesignTimeBuild=True" }), "design-time build should have succeeded.");
1526+
1527+
//Now a full build
1528+
Assert.IsTrue (libb.Build (proj), "library build should have succeeded.");
1529+
appb.Target = "SignAndroidPackage";
1530+
Assert.IsTrue (appb.Build (proj), "app build should have succeeded.");
1531+
}
1532+
}
14841533
}
14851534
}

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,9 +1920,9 @@ because xbuild doesn't support framework reference assemblies.
19201920
<FilteredAssemblies Include="$(OutDir)$(TargetFileName)"
19211921
Condition="Exists ('$(OutDir)$(TargetFileName)')" />
19221922
<FilteredAssemblies Include="@(ReferenceCopyLocalPaths)"
1923-
Condition="'%(ReferenceCopyLocalPaths.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.RelativeDir)' == '' "/>
1923+
Condition="'%(ReferenceCopyLocalPaths.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.RelativeDir)' == '' And Exists('%(ReferenceCopyLocalPaths.Identity)') "/>
19241924
<FilteredAssemblies Include="@(ReferencePath)"
1925-
Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' "/>
1925+
Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And Exists('%(ReferencePath.Identity)') "/>
19261926
</ItemGroup>
19271927
<!-- Find all the assemblies this app requires -->
19281928
<ResolveAssemblies

0 commit comments

Comments
 (0)