Skip to content

Commit 43f82b7

Browse files
[NativeAOT] fix .resx files in apps (#9905)
`LocalizationTests.EmbeddedResources_ShouldBeLocalized()` was failing under a NativeAOT context: Embedded string resource did not contain expected value after changing CultureInfo. String lengths are both 1. Strings differ at index 0. Expected: "b" But was: "a" -----------^ After reviewing a `.binlog`, it appears the `_ComputeIlcCompileInputs` [MSBuild target][0] was not working as expected: <Target Name="_ComputeIlcCompileInputs" BeforeTargets="ComputeIlcCompileInputs" DependsOnTargets="$(IlcDynamicBuildPropertyDependencies)"> <ItemGroup> <IlcReference Include="@(_ManagedResolvedAssembliesToPublish)" /> <IlcSatelliteAssembly Include="@(_SatelliteAssembliesToPublish)" /> <IlcSatelliteAssembly Include="@(IntermediateSatelliteAssembliesWithTargetPath)" /> </ItemGroup> </Target> `@(IntermediateSatelliteAssembliesWithTargetPath)` was blank! The problem being the way Android has an "inner" build *per RID*: * `@(IntermediateSatelliteAssembliesWithTargetPath)` is set in the "outer" build. * `@(IntermediateSatelliteAssembliesWithTargetPath)` is blank in the "inner" build per RID. * The "inner" build is where the ILC compiler runs, and so it doesn't see the current project's `*.resource.dll` file. To fix this, we can pass `$(_OuterIntermediateSatelliteAssembliesWithTargetPath)` into the inner build. I also updated `PackagingTest.MissingSatelliteAssemblyInApp()` to use both MonoVM and NativeAOT. [0]: https://github.com/dotnet/runtime/blob/cc803458ab4dabf020b462873be5bf56f1640b1e/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets#L3-L11
1 parent ec0cbcf commit 43f82b7

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ _ResolveAssemblies MSBuild target.
9696
;ResolveAssemblyReferencesFindRelatedSatellites=false
9797
;SkipCompilerExecution=true
9898
;_OuterIntermediateAssembly=@(IntermediateAssembly)
99+
;_OuterIntermediateSatelliteAssembliesWithTargetPath=@(IntermediateSatelliteAssembliesWithTargetPath)
99100
;_OuterOutputPath=$(OutputPath)
100101
;_OuterIntermediateOutputPath=$(IntermediateOutputPath)
101102
;_OuterCustomViewMapFile=$(_CustomViewMapFile)

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
9898
<ResolvedFileToPublish Include="@(IlcCompileInput);@(_AndroidILLinkAssemblies)" RuntimeIdentifier="$(_OriginalRuntimeIdentifier)" />
9999
<!-- Include libc++ -->
100100
<ResolvedFileToPublish Include="$(_NdkSysrootDir)libc++_shared.so" RuntimeIdentifier="$(_OriginalRuntimeIdentifier)" />
101+
<!-- Satellite assemblies -->
102+
<IlcSatelliteAssembly Include="$(_OuterIntermediateSatelliteAssembliesWithTargetPath)" />
101103
</ItemGroup>
102104
</Target>
103105

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public void MissingSatelliteAssemblyInLibrary ()
523523
}
524524

525525
[Test]
526-
public void MissingSatelliteAssemblyInApp ()
526+
public void MissingSatelliteAssemblyInApp ([Values (false, true)] bool publishAot)
527527
{
528528
var proj = new XamarinAndroidApplicationProject {
529529
IsRelease = true,
@@ -536,18 +536,26 @@ public void MissingSatelliteAssemblyInApp ()
536536
}
537537
}
538538
};
539+
proj.SetPublishAot (publishAot, AndroidNdkPath);
539540

540541
using (var b = CreateApkBuilder ()) {
542+
b.Verbosity = LoggerVerbosity.Diagnostic; // Needed for --satellite switch to appear in the log
541543
b.Target = "Build";
542544
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
543545
b.Target = "SignAndroidPackage";
544546
Assert.IsTrue (b.Build (proj), "SignAndroidPackage should have succeeded.");
545547

546-
var apk = Path.Combine (Root, b.ProjectDirectory,
547-
proj.OutputPath, $"{proj.PackageName}-Signed.apk");
548-
var helper = new ArchiveAssemblyHelper (apk);
549-
foreach (string abi in proj.GetRuntimeIdentifiersAsAbis ()) {
550-
Assert.IsTrue (helper.Exists ($"assemblies/{abi}/es/{proj.ProjectName}.resources.dll"), "Apk should contain satellite assemblies!");
548+
if (publishAot) {
549+
// Best idea thus far is to assert ILC's --satellite switch
550+
var regex = $"--satellite:.+{proj.ProjectName}.resources.dll";
551+
StringAssertEx.ContainsRegex (regex, b.LastBuildOutput, $"Build log should contain the pattern: {regex}");
552+
} else {
553+
var apk = Path.Combine (Root, b.ProjectDirectory,
554+
proj.OutputPath, $"{proj.PackageName}-Signed.apk");
555+
var helper = new ArchiveAssemblyHelper (apk);
556+
foreach (string abi in proj.GetRuntimeIdentifiersAsAbis ()) {
557+
Assert.IsTrue (helper.Exists ($"assemblies/{abi}/es/{proj.ProjectName}.resources.dll"), "Apk should contain satellite assemblies!");
558+
}
551559
}
552560
}
553561
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ private bool PublishAot {
175175
/// </summary>
176176
public void SetPublishAot (bool value, string androidNdkPath)
177177
{
178-
IsRelease = value;
178+
// Only toggle IsRelease=true when value is true
179+
if (value)
180+
IsRelease = true;
179181
PublishAot = value;
180182
SetProperty ("AndroidNdkDirectory", androidNdkPath);
181183

0 commit comments

Comments
 (0)