Skip to content

Commit 3859395

Browse files
Restore "[One .NET] fix GetAndroidDependencies target with --no-restore"
Context: a21d1a7 This reverts commit a84eccb. I've noticed the following sometimes happens when a new .NET MAUI project is created in VS 2022 on Windows: dotnet\sdk\6.0.200-preview.22055.18\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1004: Assets file 'obj\project.assets.json' not found. Run a NuGet package restore to generate this file. I seem to only get this for MAUI projects and only *sometimes*. `dotnet new android` projects seem to always work fine. When this error occurs, I think the dropdown fails to load the device list. What you end up with is the play button that just says `> Android Emulator`. After some amount of "fiddling", you can get the IDE to load the device list. What I think is happening is: 1. NuGet restore takes longer in MAUI projects than `dotnet new android` 2. Sometimes `GetAndroidDependencies` runs *before* NuGet restore, and that triggers the above error. I could validate this hypothesis with: > dotnet new android > dotnet build -t:GetAndroidDependencies --no-restore And I get the above error! I could also reproduce in a test.
1 parent 70f4dd8 commit 3859395

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,18 @@ public abstract class Foo<TVirtualView, TNativeView> : ViewHandler<TVirtualView,
333333
builder.AssertHasNoWarnings ();
334334
}
335335
}
336+
337+
[Test]
338+
public void GetAndroidDependencies()
339+
{
340+
var proj = new XamarinAndroidApplicationProject ();
341+
using var builder = CreateApkBuilder ();
342+
builder.Save (proj);
343+
344+
var dotnet = new DotNetCLI (Path.Combine (Root, builder.ProjectDirectory, proj.ProjectFilePath));
345+
346+
// `dotnet build -t:GetAndroidDependencies --no-restore` should succeed
347+
Assert.IsTrue (dotnet.Build (target: "GetAndroidDependencies", norestore: true), $"{proj.ProjectName} should succeed");
348+
}
336349
}
337350
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ public bool Restore (string target = null, string runtimeIdentifier = null, stri
9393
return Execute (arguments.ToArray ());
9494
}
9595

96-
public bool Build (string target = null, string runtimeIdentifier = null, string [] parameters = null)
96+
public bool Build (string target = null, string runtimeIdentifier = null, string [] parameters = null, bool norestore = false)
9797
{
9898
var arguments = GetDefaultCommandLineArgs ("build", target, runtimeIdentifier, parameters);
99+
if (norestore)
100+
arguments.Add ("--no-restore");
99101
return Execute (arguments.ToArray ());
100102
}
101103

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,31 @@ projects.
3737
</Target>
3838

3939
<PropertyGroup>
40-
<_ResolveSdksDependsOnTargets>ResolveTargetingPackAssets</_ResolveSdksDependsOnTargets>
40+
<_ResolveSdksDependsOnTargets>ProcessFrameworkReferences</_ResolveSdksDependsOnTargets>
4141
</PropertyGroup>
4242

4343
<Target Name="_ResolveSdks" DependsOnTargets="$(_ResolveSdksDependsOnTargets)">
44-
<!-- When using .NET 5, provide a list of paths to the Android and NETCore targeting pack directories, e.g.
45-
`packages\microsoft.android.ref\10.0.100-ci.master.22\ref\net5.0\` and
46-
`C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0-preview.6.20264.1\ref\net5.0\`
47-
See https://github.com/dotnet/sdk/blob/9eeb58e24af894597a534326156d09173d9f0f91/src/Tasks/Microsoft.NET.Build.Tasks/ResolveTargetingPackAssets.cs#L56
48-
-->
4944
<ItemGroup>
5045
<_AndroidApiInfo Include="$(MSBuildThisFileDirectory)..\data\*\AndroidApiInfo.xml" />
5146
<_AndroidApiInfoDirectories Include="@(_AndroidApiInfo->'%(RootDir)%(Directory)')" />
52-
<_ResolveSdksFrameworkRefAssemblyPaths Include="@(Reference->'$([System.String]::Copy('%(RootDir)%(Directory)').TrimEnd('\'))')" Condition=" '%(Reference.FrameworkReferenceName)' != '' " />
47+
<!--
48+
When using .NET 6+, provide the directory containing Mono.Android.dll and System.dll.
49+
Use %(TargetingPack.Path) / %(TargetingPack.PackageDirectory) to get this information.
50+
See: https://github.com/dotnet/sdk/blob/1b4646d2244ffa462e59f10bc752f2ff6f94b569/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets#L79-L93
51+
-->
52+
<_AndroidTargetingPackAssemblyPath
53+
Condition=" '%(TargetingPack.Identity)' == 'Microsoft.NETCore.App' "
54+
Include="$([MSBuild]::ValueOrDefault('%(TargetingPack.Path)', '%(TargetingPack.PackageDirectory)'))\ref\*\System.dll"
55+
/>
56+
<_AndroidTargetingPackAssemblyPath
57+
Condition=" '%(TargetingPack.Identity)' == 'Microsoft.Android' "
58+
Include="$([MSBuild]::ValueOrDefault('%(TargetingPack.Path)', '%(TargetingPack.PackageDirectory)'))\ref\*\Mono.Android.dll"
59+
/>
60+
<_AndroidTargetingPackDirectories Include="@(_AndroidTargetingPackAssemblyPath->'$([System.String]::Copy('%(RootDir)%(Directory)').TrimEnd('\'))')" Condition=" '%(_AndroidTargetingPackAssemblyPath.RootDir)%(_AndroidTargetingPackAssemblyPath.Directory)' != '' " />
5361
</ItemGroup>
5462
<PropertyGroup>
5563
<_AndroidAllowMissingSdkTooling Condition=" '$(_AndroidAllowMissingSdkTooling)' == '' ">False</_AndroidAllowMissingSdkTooling>
56-
<_XATargetFrameworkDirectories>@(_ResolveSdksFrameworkRefAssemblyPaths->Distinct())</_XATargetFrameworkDirectories>
64+
<_XATargetFrameworkDirectories>@(_AndroidTargetingPackDirectories->Distinct())</_XATargetFrameworkDirectories>
5765
</PropertyGroup>
5866
<ResolveSdks
5967
ContinueOnError="$(_AndroidAllowMissingSdkTooling)"

0 commit comments

Comments
 (0)