Skip to content

Commit 5efda9d

Browse files
authored
[Xamarin.Android.Build.Tasks] Strip AOT .sos when linking (#6842)
Fixes: #6840 Context: b21cbf9 Commit b21cbf9 contained a TODO: > TODO: the Resulting `.apk` sizes also increase[d] unexpectedly, with > `Xamarin.Forms_Performance_Integration-Signed-Release-Profiled-Aot.apkdesc` > showing a 1.7MB increase in `.apk` size. We believe that this is > because of more verbose debug symbols. > #6840 will track this. Update the `<GetAotArguments/>` MSBuild task to add the `-s` linker flag to `GetAotArguments.LdFlags`. This will cause the native linker to produce shared AOT libraries without debug symbols. Debug symbols are stripped unless the `$(DebugSymbols)`=True. This fixes the size regression in `Xamarin.Forms_Performance_Integration-Signed-Release-Profiled-Aot.apkdesc`, shrinking PackageSize from 19,475,110 down to 16,061,636. Compare to the pre-b21cbf94 PackageSize of 17,713,830: we're now 1.6MB *smaller* than b21cbf9!
1 parent 83f1084 commit 5efda9d

File tree

9 files changed

+307
-242
lines changed

9 files changed

+307
-242
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
5858
AotOutputDirectory="$(_AndroidAotBinDirectory)"
5959
RuntimeIdentifier="$(RuntimeIdentifier)"
6060
EnableLLVM="$(EnableLLVM)"
61-
Profiles="@(AndroidAotProfile)">
61+
Profiles="@(AndroidAotProfile)"
62+
StripLibraries="$(_AndroidAotStripLibraries)">
6263
<Output PropertyName="_Triple" TaskParameter="Triple" />
6364
<Output PropertyName="_ToolPrefix" TaskParameter="ToolPrefix" />
6465
<Output PropertyName="_MsymPath" TaskParameter="MsymPath" />

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,6 @@ _ResolveAssemblies MSBuild target.
204204
IncludeDebugSymbols="$(AndroidIncludeDebugSymbols)">
205205
<Output TaskParameter="OutputLibraries" ItemName="FrameworkNativeLibrary" />
206206
</ProcessNativeLibraries>
207-
<ItemGroup>
208-
<_StrippedFrameworkNativeLibrary Include="@(FrameworkNativeLibrary->'$(IntermediateOutputPath)native\%(RuntimeIdentifier)\%(Filename)%(Extension)')" />
209-
</ItemGroup>
210-
</Target>
211-
212-
<Target Name="_StripFrameworkNativeLibraries"
213-
Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' "
214-
DependsOnTargets="_IncludeNativeSystemLibraries"
215-
Inputs="@(FrameworkNativeLibrary)"
216-
Outputs="@(_StrippedFrameworkNativeLibrary)">
217-
<StripNativeLibraries
218-
SourceFiles="@(FrameworkNativeLibrary)"
219-
DestinationFiles="@(_StrippedFrameworkNativeLibrary)"
220-
ToolPath="$(AndroidBinUtilsDirectory)"
221-
/>
222-
<ItemGroup Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' ">
223-
<FrameworkNativeLibrary Remove="@(FrameworkNativeLibrary)"/>
224-
<FrameworkNativeLibrary Include="@(_StrippedFrameworkNativeLibrary)"/>
225-
<FileWrites Include="@(_StrippedFrameworkNativeLibrary)" />
226-
</ItemGroup>
227207
</Target>
228208

229209
</Project>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
7474
_CheckApkPerAbiFlag;
7575
_LintChecks;
7676
_IncludeNativeSystemLibraries;
77-
_StripFrameworkNativeLibraries;
7877
_CheckGoogleSdkRequirements;
7978
</_PrepareBuildApkDependsOnTargets>
8079
</PropertyGroup>

src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public abstract class GetAotArguments : AndroidAsyncTask
4747

4848
public bool EnableLLVM { get; set; }
4949

50+
public bool StripLibraries { get; set; }
51+
5052
public string AndroidSequencePointsMode { get; set; } = "";
5153

5254
public ITaskItem [] Profiles { get; set; } = Array.Empty<ITaskItem> ();
@@ -291,7 +293,17 @@ string GetLdFlags(NdkTools ndk, AndroidTargetArch arch, int level, string toolPr
291293

292294
ldFlags = $"\\\"{string.Join ("\\\";\\\"", libs)}\\\"";
293295
}
294-
return ldFlags;
296+
297+
if (!StripLibraries) {
298+
return ldFlags;
299+
}
300+
301+
const string StripFlag = "-s";
302+
if (ldFlags.Length == 0) {
303+
return StripFlag;
304+
}
305+
306+
return $"{ldFlags} {StripFlag}";
295307
}
296308

297309
static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,6 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease, bool aot, bo
598598
}
599599

600600
var rids = runtimeIdentifiers.Split (';');
601-
if (isRelease) {
602-
// Check for stripped native libraries
603-
foreach (var rid in rids) {
604-
FileAssert.Exists (Path.Combine (intermediateOutputPath, "native", rid, "libmono-android.release.so"));
605-
FileAssert.Exists (Path.Combine (intermediateOutputPath, "native", rid, "libmonosgen-2.0.so"));
606-
}
607-
}
608601

609602
// Check AndroidManifest.xml
610603
var manifestPath = Path.Combine (intermediateOutputPath, "android", "AndroidManifest.xml");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
329329
</Choose>
330330

331331
<PropertyGroup>
332+
<_AndroidAotStripLibraries Condition=" '$(_AndroidAotStripLibraries)' == '' And '$(AndroidIncludeDebugSymbols)' != 'true' ">True</_AndroidAotStripLibraries>
332333
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' and ('$(EmbedAssembliesIntoApk)' != 'true' or '$(AndroidIncludeDebugSymbols)' == 'true' or '$(BundleAssemblies)' == 'true') ">false</AndroidUseAssemblyStore>
333334
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' ">true</AndroidUseAssemblyStore>
334335
</PropertyGroup>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ projects. .NET 5 projects will not import this file.
708708
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
709709
YieldDuringToolExecution="$(YieldDuringToolExecution)"
710710
EnableLLVM="$(EnableLLVM)"
711-
Profiles="@(_AotProfiles)">
711+
Profiles="@(_AotProfiles)"
712+
StripLibraries="$(_AndroidAotStripLibraries)">
712713
<Output TaskParameter="NativeLibrariesReferences" ItemName="_AdditionalNativeLibraryReferences" />
713714
</Aot>
714715

0 commit comments

Comments
 (0)