Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Strip AOT .sos when linking (#6842)
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
grendello authored Mar 22, 2022
1 parent 83f1084 commit 5efda9d
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
AotOutputDirectory="$(_AndroidAotBinDirectory)"
RuntimeIdentifier="$(RuntimeIdentifier)"
EnableLLVM="$(EnableLLVM)"
Profiles="@(AndroidAotProfile)">
Profiles="@(AndroidAotProfile)"
StripLibraries="$(_AndroidAotStripLibraries)">
<Output PropertyName="_Triple" TaskParameter="Triple" />
<Output PropertyName="_ToolPrefix" TaskParameter="ToolPrefix" />
<Output PropertyName="_MsymPath" TaskParameter="MsymPath" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,6 @@ _ResolveAssemblies MSBuild target.
IncludeDebugSymbols="$(AndroidIncludeDebugSymbols)">
<Output TaskParameter="OutputLibraries" ItemName="FrameworkNativeLibrary" />
</ProcessNativeLibraries>
<ItemGroup>
<_StrippedFrameworkNativeLibrary Include="@(FrameworkNativeLibrary->'$(IntermediateOutputPath)native\%(RuntimeIdentifier)\%(Filename)%(Extension)')" />
</ItemGroup>
</Target>

<Target Name="_StripFrameworkNativeLibraries"
Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' "
DependsOnTargets="_IncludeNativeSystemLibraries"
Inputs="@(FrameworkNativeLibrary)"
Outputs="@(_StrippedFrameworkNativeLibrary)">
<StripNativeLibraries
SourceFiles="@(FrameworkNativeLibrary)"
DestinationFiles="@(_StrippedFrameworkNativeLibrary)"
ToolPath="$(AndroidBinUtilsDirectory)"
/>
<ItemGroup Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' ">
<FrameworkNativeLibrary Remove="@(FrameworkNativeLibrary)"/>
<FrameworkNativeLibrary Include="@(_StrippedFrameworkNativeLibrary)"/>
<FileWrites Include="@(_StrippedFrameworkNativeLibrary)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_CheckApkPerAbiFlag;
_LintChecks;
_IncludeNativeSystemLibraries;
_StripFrameworkNativeLibraries;
_CheckGoogleSdkRequirements;
</_PrepareBuildApkDependsOnTargets>
</PropertyGroup>
Expand Down
14 changes: 13 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class GetAotArguments : AndroidAsyncTask

public bool EnableLLVM { get; set; }

public bool StripLibraries { get; set; }

public string AndroidSequencePointsMode { get; set; } = "";

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

ldFlags = $"\\\"{string.Join ("\\\";\\\"", libs)}\\\"";
}
return ldFlags;

if (!StripLibraries) {
return ldFlags;
}

const string StripFlag = "-s";
if (ldFlags.Length == 0) {
return StripFlag;
}

return $"{ldFlags} {StripFlag}";
}

static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,6 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease, bool aot, bo
}

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

// Check AndroidManifest.xml
var manifestPath = Path.Combine (intermediateOutputPath, "android", "AndroidManifest.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
</Choose>

<PropertyGroup>
<_AndroidAotStripLibraries Condition=" '$(_AndroidAotStripLibraries)' == '' And '$(AndroidIncludeDebugSymbols)' != 'true' ">True</_AndroidAotStripLibraries>
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' and ('$(EmbedAssembliesIntoApk)' != 'true' or '$(AndroidIncludeDebugSymbols)' == 'true' or '$(BundleAssemblies)' == 'true') ">false</AndroidUseAssemblyStore>
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' ">true</AndroidUseAssemblyStore>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ projects. .NET 5 projects will not import this file.
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
YieldDuringToolExecution="$(YieldDuringToolExecution)"
EnableLLVM="$(EnableLLVM)"
Profiles="@(_AotProfiles)">
Profiles="@(_AotProfiles)"
StripLibraries="$(_AndroidAotStripLibraries)">
<Output TaskParameter="NativeLibrariesReferences" ItemName="_AdditionalNativeLibraryReferences" />
</Aot>

Expand Down
Loading

0 comments on commit 5efda9d

Please sign in to comment.