Skip to content

Commit 46df03f

Browse files
[One .NET] "greenfield" projects opt into trimming
This is WIP depends on #8778. As we have solved all trimming warnings in the Android workload, we can now go "all in" on trimming. Early in .NET 6 (maybe even 5?) we "hid" many trimming warnings as we did not yet plan to solve them: <SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings> These warnings were not *actionable* at the time for customers, as many warnings were in `Mono.Android.dll`, `Java.Interop.dll`, etc. Going forward, let's stop suppressing these warnings if `TrimMode=full`. We can also enable trimming for new projects: * `dotnet new android` * `dotnet new android-wear` These will have the `TrimMode` property set to `Full` by default: <!-- Enable full trimming in Release mode. To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity --> <PropertyGroup Condition="'$(Configuration)' == 'Release'"> <TrimMode>full</TrimMode> </PropertyGroup> We wouldn't want to do this for existing projects *yet*, as they might have existing code, NuGet packages, etc. where trimming warnings might be present. We can also improve the templates for Android class libraries: * `dotnet new androidlib` * `dotnet new android-bindinglib` <!-- Enable trim analyzers for Android class libraries. To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming --> <IsTrimmable>true</IsTrimmable> This way, new class libraries will be "trimmable" by default and be able to react to trimming warnings. We can also use `TrimMode=full` in many of our existing tests: * MSBuild tests that assert 0 warnings can use `TrimMode=full`. * On-device tests can import `trim-analyzers.props` and use `TrimMode=full`.
1 parent f6139e7 commit 46df03f

File tree

11 files changed

+50
-3
lines changed

11 files changed

+50
-3
lines changed

src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
8+
<!--
9+
Enable trim analyzers for Android class libraries.
10+
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
11+
-->
12+
<IsTrimmable>true</IsTrimmable>
813
<!--
914
NOTE: you can simply add .aar or .jar files in this directory to be included in the project.
1015
To learn more, see: https://learn.microsoft.com/dotnet/maui/migration/android-binding-projects

src/Microsoft.Android.Templates/android-wear/AndroidApp1.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
1212
<MSBuildWarningsAsMessages>$(MSBuildWarningsAsMessages);XA4218</MSBuildWarningsAsMessages>
1313
</PropertyGroup>
14+
<!--
15+
Enable full trimming in Release mode.
16+
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
17+
-->
18+
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
19+
<TrimMode>full</TrimMode>
20+
</PropertyGroup>
1421
<ItemGroup>
1522
<PackageReference Include="Xamarin.AndroidX.Wear" Version="1.2.0.5" />
1623
</ItemGroup>

src/Microsoft.Android.Templates/android/AndroidApp1.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
<ApplicationVersion>1</ApplicationVersion>
1111
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
1212
</PropertyGroup>
13+
<!--
14+
Enable full trimming in Release mode.
15+
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
16+
-->
17+
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
18+
<TrimMode>full</TrimMode>
19+
</PropertyGroup>
1320
</Project>

src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
8+
<!--
9+
Enable trim analyzers for Android class libraries.
10+
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
11+
-->
12+
<IsTrimmable>true</IsTrimmable>
813
</PropertyGroup>
914
</Project>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@
7474
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
7575
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>
7676
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
77-
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">link</TrimMode>
77+
<TrimMode Condition=" '$(TrimMode)' == '' and '$(AndroidLinkMode)' == 'Full' ">full</TrimMode>
7878
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
79+
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and '$(TrimMode)' == 'full' ">false</SuppressTrimAnalysisWarnings>
7980
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
8081
<!-- Prefer $(RuntimeIdentifiers) plural -->
8182
<RuntimeIdentifiers Condition=" '$(RuntimeIdentifier)' == '' And '$(RuntimeIdentifiers)' == '' ">android-arm;android-arm64;android-x86;android-x64</RuntimeIdentifiers>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ public void BuildHasNoWarnings (bool isRelease, bool xamarinForms, bool multidex
232232
new XamarinFormsAndroidApplicationProject () :
233233
new XamarinAndroidApplicationProject ();
234234
proj.IsRelease = isRelease;
235+
// Enable full trimming
236+
if (!xamarinForms && isRelease) {
237+
proj.TrimModeRelease = TrimMode.Full;
238+
}
235239
if (multidex) {
236240
proj.SetProperty ("AndroidEnableMultiDex", "True");
237241
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ public enum AndroidLinkMode
99
SdkOnly,
1010
Full,
1111
}
12+
13+
public enum TrimMode
14+
{
15+
Partial,
16+
Full,
17+
}
1218
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace Xamarin.ProjectTools
44
{
@@ -18,6 +18,7 @@ public static class KnownProperties
1818
public const string RuntimeIdentifiers = "RuntimeIdentifiers";
1919
public const string RunAOTCompilation = "RunAOTCompilation";
2020
public const string PublishTrimmed = "PublishTrimmed";
21+
public const string TrimMode = "TrimMode";
2122
public const string SupportedOSPlatformVersion = "SupportedOSPlatformVersion";
2223

2324
public const string Deterministic = "Deterministic";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ public AndroidLinkMode AndroidLinkModeRelease {
155155
set { SetProperty (ReleaseProperties, KnownProperties.AndroidLinkMode, value.ToString ()); }
156156
}
157157

158+
public TrimMode TrimModeRelease {
159+
get => Enum.TryParse (GetProperty (ReleaseProperties, KnownProperties.TrimMode), out TrimMode trimMode) ? trimMode : TrimMode.Partial;
160+
set => SetProperty (ReleaseProperties, KnownProperties.TrimMode, value.ToString ().ToLowerInvariant ());
161+
}
162+
158163
public bool EnableMarshalMethods {
159164
get { return string.Equals (GetProperty (KnownProperties.AndroidEnableMarshalMethods), "True", StringComparison.OrdinalIgnoreCase); }
160165
set { SetProperty (KnownProperties.AndroidEnableMarshalMethods, value.ToString ()); }

tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
</PropertyGroup>
1414

1515
<Import Project="..\..\..\Configuration.props" />
16+
<Import Project="$(XamarinAndroidSourcePath)\build-tools\trim-analyzers\trim-analyzers.props" />
1617

1718
<PropertyGroup>
1819
<OutputPath>..\..\..\bin\Test$(Configuration)</OutputPath>
1920
</PropertyGroup>
2021

22+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
23+
<TrimMode Condition=" '$(TrimMode)' == '' ">full</TrimMode>
24+
</PropertyGroup>
25+
2126
<ItemGroup>
2227
<AndroidJavaSource Include="../Xamarin.Android.McwGen-Tests/java/**/*.java" />
2328
<Compile Remove="TimingTests.cs" />

tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<Import Project="..\..\..\Configuration.props" />
44
<Import Project="..\Mono.Android-Test.Shared.projitems" Label="Shared" Condition="Exists('..\Mono.Android-Test.Shared.projitems')" />
5+
<Import Project="$(XamarinAndroidSourcePath)\build-tools\trim-analyzers\trim-analyzers.props" />
56

67
<PropertyGroup>
78
<TargetFramework>$(DotNetAndroidTargetFramework)</TargetFramework>
@@ -19,7 +20,6 @@
1920
<EnableDefaultAndroidAssetItems>false</EnableDefaultAndroidAssetItems>
2021
<_MonoAndroidTestPackage>Mono.Android.NET_Tests</_MonoAndroidTestPackage>
2122
<PlotDataLabelSuffix>-$(TestsFlavor)NET6</PlotDataLabelSuffix>
22-
<WarningsAsErrors>IL2037</WarningsAsErrors>
2323
<AndroidUseNegotiateAuthentication>true</AndroidUseNegotiateAuthentication>
2424
<AndroidNdkDirectory></AndroidNdkDirectory>
2525
<!--
@@ -36,6 +36,7 @@
3636

3737
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
3838
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == '' ">r8</AndroidLinkTool>
39+
<TrimMode Condition=" '$(TrimMode)' == '' ">full</TrimMode>
3940
</PropertyGroup>
4041

4142
<ItemGroup>

0 commit comments

Comments
 (0)