Skip to content

Commit 175f3a0

Browse files
[Xamarin.Android.Build.Tasks] fix Inputs for _Generate*Java* targets (#9174)
Fixes: #8967 Context: #9001 You can cause a build error in .NET 8 by doing: * Start an x86 or x86_64 emulator * `dotnet build -t:Run` * Close the emulator, attach an arm or arm64 device * `dotnet build -t:Run` Emits the error: Xamarin.Android.Common.targets(2063,3): error XA3006: Could not compile native assembly file: typemaps.x86_64.ll stderr | C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.113\tools\binutils\bin\llc.exe: error: C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.113\tools\binutils\bin\llc.exe: typemaps.x86_64.ll: error: Could not open input file: no such file or directory This works fine in Visual Studio, but not at the command-line. The underlying problem is due to `_GeneratePackageManagerJava` target running, while the `_GenerateJavaStubs` target *did not*: Skipping target "_GenerateJavaStubs" because all output files are up-to-date with respect to the input files. ... Input file "obj\Debug\net8.0-android\resolvedassemblies.hash" is newer than output file "obj\Debug\net8.0-android\stamp\_GeneratePackageManagerJava.stamp". These two targets should almost always run together, so we should ensure that they do. This problem also doesn't happen .NET 9, as both targets are invalidated for a different reason: Target Name=_LinkAssembliesNoShrink Project=UnnamedProject.csproj Building target "_LinkAssembliesNoShrink" completely. Output file "obj\Debug\android\assets\x86_64\UnnamedProject.dll" does not exist. Since, we build per-RID in .NET 9, `obj\Debug\android\assets\x86_64\UnnamedProject.dll` has an `x86_64` in the path, which is not present in the .NET 8 build. Reviewing the two sets of `Inputs`: <Target Name="_GenerateJavaStubs" ... Inputs="@(_AndroidMSBuildAllProjects);@(_ResolvedUserMonoAndroidAssemblies);$(_AndroidManifestAbs);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)" ... <Target Name="_GeneratePackageManagerJava" ... Inputs="@(_AndroidMSBuildAllProjects);$(_ResolvedUserAssembliesHashFile);$(MSBuildProjectFile);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)" Let's align them more closely by: * Track assembly changes in the exact same way: $(_ResolvedUserAssembliesHashFile);@(_ResolvedUserMonoAndroidAssemblies); This way if an assembly is added, removed, or timestamp changed: both targets are in sync. * Don't track `$(MSBuildProjectFile)`, this should already be in `@(_AndroidMSBuildAllProjects)`. With this change in place manually, I'm not able to reproduce the problem any longer. PR #9001 may also have fixed this issue, but it could cause targets to run on every incremental build -- a performance issue.
1 parent 7451f5b commit 175f3a0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,5 +1655,36 @@ public void SimilarAndroidXAssemblyNames ([Values(true, false)] bool publishTrim
16551655
using var builder = CreateApkBuilder ();
16561656
Assert.IsTrue (builder.Build (proj), "Build should have succeeded.");
16571657
}
1658+
1659+
[Test]
1660+
public void IncrementalBuildDifferentDevice()
1661+
{
1662+
var proj = new XamarinAndroidApplicationProject {
1663+
Imports = {
1664+
new Import (() => "MockPrimaryCpuAbi.targets") {
1665+
TextContent = () =>
1666+
"""
1667+
<Project>
1668+
<!-- This target "mocks" what _GetPrimaryCpuAbi does -->
1669+
<Target Name="_MockPrimaryCpuAbi" BeforeTargets="_CreatePropertiesCache">
1670+
<PropertyGroup>
1671+
<RuntimeIdentifier>$(_SingleRID)</RuntimeIdentifier>
1672+
<RuntimeIdentifiers></RuntimeIdentifiers>
1673+
<AndroidSupportedAbis>$(_SingleABI)</AndroidSupportedAbis>
1674+
</PropertyGroup>
1675+
</Target>
1676+
</Project>
1677+
"""
1678+
},
1679+
},
1680+
};
1681+
using var builder = CreateApkBuilder ();
1682+
builder.Target = "Build";
1683+
builder.BuildingInsideVisualStudio = false;
1684+
Assert.IsTrue (builder.Build (proj, parameters: [ "_SingleRID=android-arm64", "_SingleABI=arm64-v8a" ]),
1685+
"first build should have succeeded.");
1686+
Assert.IsTrue (builder.Build (proj, parameters: [ "_SingleRID=android-x64", "_SingleABI=x86_64" ], doNotCleanupOnUpdate: true),
1687+
"second build should have succeeded.");
1688+
}
16581689
}
16591690
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ because xbuild doesn't support framework reference assemblies.
14441444

14451445
<Target Name="_GenerateJavaStubs"
14461446
DependsOnTargets="$(_GenerateJavaStubsDependsOnTargets);$(BeforeGenerateAndroidManifest)"
1447-
Inputs="@(_AndroidMSBuildAllProjects);@(_ResolvedUserMonoAndroidAssemblies);$(_AndroidManifestAbs);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)"
1447+
Inputs="@(_AndroidMSBuildAllProjects);$(_ResolvedUserAssembliesHashFile);@(_ResolvedUserMonoAndroidAssemblies);$(_AndroidManifestAbs);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)"
14481448
Outputs="$(_AndroidStampDirectory)_GenerateJavaStubs.stamp">
14491449

14501450
<PropertyGroup>
@@ -1691,7 +1691,7 @@ because xbuild doesn't support framework reference assemblies.
16911691

16921692
<Target Name="_GeneratePackageManagerJava"
16931693
DependsOnTargets="$(_GeneratePackageManagerJavaDependsOn)"
1694-
Inputs="@(_AndroidMSBuildAllProjects);$(_ResolvedUserAssembliesHashFile);$(MSBuildProjectFile);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)"
1694+
Inputs="@(_AndroidMSBuildAllProjects);$(_ResolvedUserAssembliesHashFile);@(_ResolvedUserMonoAndroidAssemblies);$(_AndroidBuildPropertiesCache);@(AndroidEnvironment);@(LibraryEnvironments)"
16951695
Outputs="$(_AndroidStampDirectory)_GeneratePackageManagerJava.stamp">
16961696
<!-- Create java needed for Mono runtime -->
16971697
<GeneratePackageManagerJava

0 commit comments

Comments
 (0)